feat: add webapi, lower python version requirement and add a Dockerfile
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import json
|
||||
import requests
|
||||
from typing import Annotated
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse
|
||||
from pydantic import BaseModel
|
||||
from . import myice
|
||||
|
||||
|
||||
origins = ["*"]
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
class AuthHeaders(BaseModel):
|
||||
Authorization: str
|
||||
|
||||
def token(self) -> str:
|
||||
return self.Authorization.split(" ")[1]
|
||||
|
||||
def authorized(self) -> bool:
|
||||
if self.token() == "abc":
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def home():
|
||||
with open("index.html") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
@app.get("/schedule")
|
||||
async def schedule(
|
||||
headers: Annotated[AuthHeaders, Header()],
|
||||
num_days: int = 7,
|
||||
):
|
||||
if not headers.authorized():
|
||||
raise HTTPException(401, detail="get out")
|
||||
return json.loads(myice.get_schedule(num_days))
|
||||
|
||||
|
||||
@app.get("/game/{game_id}")
|
||||
async def game(
|
||||
headers: Annotated[AuthHeaders, Header()],
|
||||
game_id: int,
|
||||
):
|
||||
username, password, userid, existing_token = myice.get_login()
|
||||
if existing_token:
|
||||
myice.userdata = {
|
||||
"id": userid,
|
||||
"id_club": 186,
|
||||
"token": existing_token,
|
||||
}
|
||||
else:
|
||||
myice.userdata = myice.mobile_login()
|
||||
|
||||
# data = refresh_data()
|
||||
with requests.post(
|
||||
"https://app.myice.hockey/api/mobilerest/getevent",
|
||||
headers=myice.mobile_headers,
|
||||
data="&".join(
|
||||
[
|
||||
f"token={myice.userdata['token']}",
|
||||
f"id_event={game_id}",
|
||||
"type=games",
|
||||
f"id_player={myice.userdata['id']}",
|
||||
f"id_club={myice.userdata['id_club']}",
|
||||
"language=FR",
|
||||
]
|
||||
),
|
||||
# verify=False,
|
||||
) as r:
|
||||
data = r.json()["eventData"]
|
||||
return data
|
||||
Reference in New Issue
Block a user