-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
55 lines (39 loc) · 1.35 KB
/
Copy pathserver.py
File metadata and controls
55 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from datetime import date, timedelta
from typing import Optional
from fastapi.applications import FastAPI
from pydantic.main import BaseModel
import crawl
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
class LoginRequest(BaseModel):
username: str
password: str
@app.post("/api/login")
def login(req: LoginRequest):
resp = crawl.login(req.username, req.password)
if "login" in resp.url:
print("---------Login failed----------")
print(resp.headers)
print(resp.url)
return {"message": "login failed"}
else:
print("---------Login successful----------")
return {"login success"}
@app.get("/api/schedule")
def schedule(monday: Optional[str] = None, sunday: Optional[str] = None):
if not monday or not sunday:
monday, sunday = week_monday_sunday()
schedule = crawl.get_schedule(monday, sunday)
return schedule
@app.get("/api/logout")
def logout():
crawl.logout()
return {"message": "logout success"}
def week_monday_sunday(today: date | None = None) -> tuple[str, str]:
"""获取本周的周一和周日的日期字符串,格式为 'YYYY-MM-DD'"""
today = today or date.today()
monday = today - timedelta(days=today.weekday())
sunday = monday + timedelta(days=6)
return monday.strftime("%Y-%m-%d"), sunday.strftime("%Y-%m-%d")