Finish repath, fix api

This commit is contained in:
EstrellaXD
2023-05-10 20:43:34 +08:00
parent 2ca65f115b
commit e00757f1d7
17 changed files with 302 additions and 277 deletions

View File

@@ -0,0 +1 @@
from .program import router

29
src/module/api/api.py Normal file
View File

@@ -0,0 +1,29 @@
import logging
import os
from fastapi import FastAPI
from fastapi.responses import FileResponse, Response
from module.conf import LOG_PATH
logger = logging.getLogger(__name__)
router = FastAPI()
@router.get("/api/v1/log", tags=["log"])
async def get_log():
if os.path.isfile(LOG_PATH):
return FileResponse(LOG_PATH)
else:
return Response("Log file not found", status_code=404)
@router.get("/api/v1/resetRule")
def reset_rule():
pass

28
src/module/api/bangumi.py Normal file
View File

@@ -0,0 +1,28 @@
from .api import router
from module.models import BangumiData
from module.database import BangumiDatabase
from module.manager import set_new_path
@router.get("/api/v1/bangumi/getData", tags=["bangumi"])
async def get_all_data():
with BangumiDatabase() as database:
return database.search_all()
@router.get("/api/v1/bangumi/getData/{bangumi_id}", tags=["bangumi"])
async def get_data(bangumi_id: str):
with BangumiDatabase() as database:
data = database.search_id(int(bangumi_id))
if data:
return data
else:
return {"": "data not exist"}
@router.post("/api/v1/bangumi/UpdateData", tags=["bangumi"])
async def update_data(data: BangumiData):
with BangumiDatabase() as database:
database.update(data)
set_new_path(data)

16
src/module/api/config.py Normal file
View File

@@ -0,0 +1,16 @@
from .bangumi import router
from module.conf import settings
from module.models import Config
@router.get("/api/v1/getConfig", tags=["config"], response_model=Config)
async def get_config():
return settings
# Reverse proxy
@router.post("/api/v1/updateConfig", tags=["config"], response_model=Config)
async def update_config(config: Config):
settings.save(config_dict=config.dict())
return settings

View File

@@ -0,0 +1,31 @@
from .config import router
from module.models.api import *
from module.manager import FullSeasonGet
from module.rss import RSSAnalyser
def link_process(link):
return RSSAnalyser().rss_to_data(link, full_parse=False)
@router.post("/api/v1/collection", tags=["download"])
async def collection(link: RssLink):
data = link_process(link)
if data:
with FullSeasonGet() as season:
season.download_collection(data[0], link)
return data[0]
else:
return {"status": "Failed to parse link"}
@router.post("/api/v1/subscribe", tags=["download"])
async def subscribe(link: RssLink):
data = link_process(link)
if data:
with FullSeasonGet() as season:
season.add_subscribe(data[0], link)
return data[0]
else:
return {"status": "Failed to parse link"}

79
src/module/api/program.py Normal file
View File

@@ -0,0 +1,79 @@
import os
import signal
import logging
from fastapi import Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from .proxy import router
from module.core import start_thread, start_program, stop_thread, stop_event
from module.conf import VERSION, settings, setup_logger
logger = logging.getLogger(__name__)
@router.on_event("startup")
async def startup():
log_level = logging.DEBUG if settings.log.debug_enable else logging.INFO
setup_logger(log_level)
start_program()
@router.on_event("shutdown")
async def shutdown():
stop_event.set()
logger.info("Stopping program...")
@router.get("/api/v1/restart", tags=["program"])
async def restart():
stop_thread()
start_thread()
return {"status": "ok"}
@router.get("/api/v1/start", tags=["program"])
async def start():
start_thread()
return {"status": "ok"}
@router.get("/api/v1/stop", tags=["program"])
async def stop():
stop_thread()
return {"status": "ok"}
@router.get("/api/v1/status", tags=["program"])
async def status():
if stop_event.is_set():
return {"status": "stop"}
else:
return {"status": "running"}
@router.get("/api/v1/shutdown", tags=["program"])
async def shutdown_program():
stop_thread()
logger.info("Shutting down program...")
os.kill(os.getpid(), signal.SIGINT)
return {"status": "ok"}
if VERSION != "DEV_VERSION":
router.mount("/assets", StaticFiles(directory="templates/assets"), name="assets")
templates = Jinja2Templates(directory="templates")
# HTML Response
@router.get("/{full_path:path}", response_class=HTMLResponse, tags=["html"])
def index(request: Request):
context = {"request": request}
return templates.TemplateResponse("index.html", context)
else:
@router.get("/", status_code=302, tags=["html"])
def index():
return RedirectResponse("/docs")

61
src/module/api/proxy.py Normal file
View File

@@ -0,0 +1,61 @@
import re
import logging
from fastapi.responses import Response
from .download import router
from module.conf import settings
from module.network import RequestContent
logger = logging.getLogger(__name__)
def get_rss(full_path):
url = f"https://mikanani.me/RSS/{full_path}"
custom_url = settings.rss_parser.custom_url
if "://" not in custom_url:
custom_url = f"https://{custom_url}"
with RequestContent() as request:
content = request.get_html(url)
return re.sub(r"https://mikanani.me", custom_url, content)
def get_torrent(full_path):
url = f"https://mikanani.me/Download/{full_path}"
with RequestContent() as request:
return request.get_content(url)
@router.get("/RSS/MyBangumi", tags=["proxy"])
async def get_my_bangumi(token: str):
full_path = "MyBangumi?token=" + token
content = get_rss(full_path)
return Response(content, media_type="application/xml")
@router.get("/RSS/Search", tags=["proxy"])
async def get_search_result(searchstr: str):
full_path = "Search?searchstr=" + searchstr
content = get_rss(full_path)
return Response(content, media_type="application/xml")
@router.get("/RSS/Bangumi", tags=["proxy"])
async def get_bangumi(bangumiId: str, groupid: str):
full_path = "Bangumi?bangumiId=" + bangumiId + "&groupid=" + groupid
content = get_rss(full_path)
return Response(content, media_type="application/xml")
@router.get("/RSS/{full_path:path}", tags=["proxy"])
async def get_rss(full_path: str):
content = get_rss(full_path)
return Response(content, media_type="application/xml")
@router.get("/Download/{full_path:path}", tags=["proxy"])
async def download(full_path: str):
torrent = get_torrent(full_path)
return Response(torrent, media_type="application/x-bittorrent")