From c82b6e7265d4e12b0bad07bd848fd7e5d4397161 Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Wed, 17 Apr 2024 16:48:27 +0500 Subject: [PATCH] split to modules. ADD index page. --- .gitignore | 1 + backend/main.py | 62 +++++------------------------ backend/modules/__init__.py | 0 backend/modules/models/__init__.py | 0 backend/modules/pages/__init__.py | 0 backend/modules/pages/index.py | 37 +++++++++++++++++ backend/modules/proxy.py | 41 +++++++++++++++++++ backend/modules/release/__init__.py | 0 backend/modules/release/release.py | 11 +++++ backend/modules/user/__init__.py | 0 backend/modules/user/profile.py | 21 ++++++++++ 11 files changed, 120 insertions(+), 53 deletions(-) create mode 100644 backend/modules/__init__.py create mode 100644 backend/modules/models/__init__.py create mode 100644 backend/modules/pages/__init__.py create mode 100644 backend/modules/pages/index.py create mode 100644 backend/modules/proxy.py create mode 100644 backend/modules/release/__init__.py create mode 100644 backend/modules/release/release.py create mode 100644 backend/modules/user/__init__.py create mode 100644 backend/modules/user/profile.py diff --git a/.gitignore b/.gitignore index 10f0a09..0da70a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Python __pycache__ venv +.mypy_cache # VSCode .VSCode diff --git a/backend/main.py b/backend/main.py index 5da3e37..4767212 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,60 +1,16 @@ -import requests +import uvicorn from fastapi import FastAPI -from fastapi import Request +from modules.pages import index +from modules.release import release +from modules.user import profile app = FastAPI() -API_URL = "https://api.anixart.tv" -ENDPOINTS = { - "release": f"{API_URL}/release", - "profile": f"{API_URL}/profile", -} -USER_AGENT = "AnixartApp/8.2.1-23121216 (Android 11; SDK 30; arm64-v8a;)" +app.include_router(profile.router, prefix="/profile") +app.include_router(release.router, prefix="/release") -async def apiRequest( - # noqa: E501 - request: Request = None, - endpoint: str = "", - path: str = "", - query: str = "", -): +app.include_router(index.router, prefix="/page") - headers = { - "User-Agent": USER_AGENT, - "Content-Type": "application/json; charset=UTF-8", - } - - if request.method == "POST": - r = requests.post( - # noqa: E501 - f"{endpoint}/{path}{query}", - headers=headers, - data=await request.body(), - ) - else: - r = requests.get(f"{endpoint}/{path}{query}", headers=headers) - - if r.status_code != 200: - return {"error": r.text} - return r.json() - - -@app.get("/profile/{user_id}") -async def getUserById(request: Request, user_id: str, short: bool = False): - res = await apiRequest(request, ENDPOINTS["profile"], user_id) - if short is False: - return res - return { - "code": res["code"], - "profile": { - "id": res["profile"]["id"], - "login": res["profile"]["login"], - "avatar": res["profile"]["avatar"], - }, - } - - -@app.get("/release/{release_id}") -async def GetReleaseById(request: Request, release_id: str): - return await apiRequest(request, ENDPOINTS["release"], release_id) +if __name__ == "__main__": + uvicorn.run("main:app", host="0.0.0.0", port=8000) diff --git a/backend/modules/__init__.py b/backend/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/modules/models/__init__.py b/backend/modules/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/modules/pages/__init__.py b/backend/modules/pages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/modules/pages/index.py b/backend/modules/pages/index.py new file mode 100644 index 0000000..04f6875 --- /dev/null +++ b/backend/modules/pages/index.py @@ -0,0 +1,37 @@ +import json + +from fastapi import APIRouter +from fastapi import Request +from modules.proxy import apiRequest +from modules.proxy import ENDPOINTS + +router = APIRouter() + + +# TODO: Add filters? +@router.post("/index", summary="Get main page") +async def GetMainPage(request: Request, page: int = 0): + + data = json.dumps( + { + "country": None, + "season": None, + "sort": 0, + "studio": None, + "age_ratings": [], + "category_id": None, + "end_year": None, + "episode_duration_from": None, + "episode_duration_to": None, + "episodes_from": None, + "episodes_to": None, + "genres": [], + "profile_list_exclusions": [], + "start_year": None, + "status_id": None, + "types": [], + "is_genres_exclude_mode_enabled": False, + } + ) + + return await apiRequest(request, ENDPOINTS["filter"], page, data=data) diff --git a/backend/modules/proxy.py b/backend/modules/proxy.py new file mode 100644 index 0000000..14746b3 --- /dev/null +++ b/backend/modules/proxy.py @@ -0,0 +1,41 @@ +from typing import Union + +import requests +from fastapi import Request + +API_URL = "https://api.anixart.tv" +ENDPOINTS = { + "release": f"{API_URL}/release", + "profile": f"{API_URL}/profile", + "filter": f"{API_URL}/filter", +} +USER_AGENT = "AnixartApp/8.2.1-23121216 (Android 11; SDK 30; arm64-v8a;)" + + +async def apiRequest( + # noqa: E501 + request: Request = None, + endpoint: str = "", + path: str = "", + query: str = "", + data: Union[None, str, dict] = None, +): + + headers = { + "User-Agent": USER_AGENT, + "Content-Type": "application/json; charset=UTF-8", + } + + if request.method == "POST": + r = requests.post( + # noqa: E501 + f"{endpoint}/{path}{query}", + headers=headers, + data=data, + ) + else: + r = requests.get(f"{endpoint}/{path}{query}", headers=headers) + + if r.status_code != 200: + return {"error": r.text} + return r.json() diff --git a/backend/modules/release/__init__.py b/backend/modules/release/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/modules/release/release.py b/backend/modules/release/release.py new file mode 100644 index 0000000..45009fb --- /dev/null +++ b/backend/modules/release/release.py @@ -0,0 +1,11 @@ +from fastapi import APIRouter +from fastapi import Request +from modules.proxy import apiRequest +from modules.proxy import ENDPOINTS + +router = APIRouter() + + +@router.get("/{release_id}", summary="Get release info by release ID") +async def GetReleaseById(request: Request, release_id: str): + return await apiRequest(request, ENDPOINTS["release"], release_id) diff --git a/backend/modules/user/__init__.py b/backend/modules/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/modules/user/profile.py b/backend/modules/user/profile.py new file mode 100644 index 0000000..bb66f74 --- /dev/null +++ b/backend/modules/user/profile.py @@ -0,0 +1,21 @@ +from fastapi import APIRouter +from fastapi import Request +from modules.proxy import apiRequest +from modules.proxy import ENDPOINTS + +router = APIRouter() + + +@router.get("/{user_id}", summary="Get user profile by user ID") +async def getUserById(request: Request, user_id: str, short: bool = False): + res = await apiRequest(request, ENDPOINTS["profile"], user_id) + if short is False: + return res + return { + "code": res["code"], + "profile": { + "id": res["profile"]["id"], + "login": res["profile"]["login"], + "avatar": res["profile"]["avatar"], + }, + }