-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
36 lines (26 loc) · 980 Bytes
/
main.py
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
import os
import json
from fastapi import FastAPI
from utils.get_meteo import get_tomorrow_date
from utils import ask_toobo
app = FastAPI()
@app.get("/API/toobo")
@app.head("/API/toobo")
async def resume_weather(date: str = ""):
date = date.replace("/", "-") or get_tomorrow_date()
file_path = f"data/{date}.json"
if os.path.exists(file_path):
with open(file_path, "r", encoding="utf-8") as file:
try:
return {"code": 200, "data": json.load(file)}
except json.decoder.JSONDecodeError:
pass
os.remove(file_path)
if date != get_tomorrow_date():
return {"code": 400, "data": {"text": "Error: Weather data for the requested date is not available"}}
resume = ask_toobo.resume_weather()
ask_toobo.save_resume(resume, date)
return {"code": 200, "data": {"text": resume}}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)