|
| 1 | +import logging |
| 2 | + |
| 3 | +from fastapi import APIRouter, Depends, HTTPException |
| 4 | +from fastapi.responses import JSONResponse |
| 5 | +from sqlalchemy.util.concurrency import asyncio |
| 6 | + |
| 7 | +from module.database import Database, engine |
| 8 | +from module.manager import BangumiManager, TorrentManager |
| 9 | +from module.models import APIResponse, Bangumi, BangumiUpdate, ResponseModel, Torrent |
| 10 | +from module.security.api import get_current_user |
| 11 | + |
| 12 | +from .response import u_response |
| 13 | + |
| 14 | +router = APIRouter(prefix="/torrent", tags=["torrent"]) |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +@router.get( |
| 19 | + "/get_all", |
| 20 | + response_model=list[Torrent], |
| 21 | + dependencies=[Depends(get_current_user)], |
| 22 | +) |
| 23 | +async def manage_bangumi(_id: int): |
| 24 | + """ |
| 25 | + 管理对应 Bangumi 的规则 |
| 26 | + """ |
| 27 | + try: |
| 28 | + resp = await TorrentManager().fetch_all_bangumi_torrents(_id) |
| 29 | + return resp |
| 30 | + except Exception as e: |
| 31 | + logger.error(f"[Bangumi] Error managing bangumi: {e}") |
| 32 | + return [] |
| 33 | + |
| 34 | + |
| 35 | +@router.post( |
| 36 | + "/delete", |
| 37 | + response_model=APIResponse, |
| 38 | + dependencies=[Depends(get_current_user)], |
| 39 | +) |
| 40 | +async def delete_torrent(url: str): |
| 41 | + """ |
| 42 | + 删除对应的种子 |
| 43 | + """ |
| 44 | + try: |
| 45 | + resp = await TorrentManager().delete_torrent(url) |
| 46 | + if resp: |
| 47 | + resp = ResponseModel( |
| 48 | + status_code=200, |
| 49 | + status=True, |
| 50 | + msg_en=f"Delete torrent for {url}", |
| 51 | + msg_zh=f"删除 {url} 的种子", |
| 52 | + ) |
| 53 | + else: |
| 54 | + resp = ResponseModel( |
| 55 | + status_code=406, |
| 56 | + status=False, |
| 57 | + msg_en=f"Can't find torrent with url {url}", |
| 58 | + msg_zh=f"无法找到 url 为 {url} 的种子", |
| 59 | + ) |
| 60 | + return resp |
| 61 | + except Exception as e: |
| 62 | + logger.error(f"[Bangumi] Error deleting torrent: {e}") |
| 63 | + return ResponseModel( |
| 64 | + status_code=500, |
| 65 | + status=False, |
| 66 | + msg_en="Internal server error", |
| 67 | + msg_zh="服务器内部错误", |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@router.post("/disable",response_model=APIResponse,dependencies=[Depends(get_current_user)]) |
| 72 | +async def disable_torrent(url:str,name,_id:int): |
| 73 | + """ |
| 74 | + 禁用对应的种子 |
| 75 | + """ |
| 76 | + try: |
| 77 | + await TorrentManager().disable_torrent(url,name, _id) |
| 78 | + return ResponseModel( |
| 79 | + status_code=200, |
| 80 | + status=True, |
| 81 | + msg_en=f"Successfully disabled torrent with url {url}", |
| 82 | + msg_zh=f"成功禁用 url 为 {url} 的种子", |
| 83 | + ) |
| 84 | + except Exception as e: |
| 85 | + logger.error(f"[Bangumi] Error disabling torrent: {e}") |
| 86 | + return ResponseModel( |
| 87 | + status_code=500, |
| 88 | + status=False, |
| 89 | + msg_en="Internal server error", |
| 90 | + msg_zh="服务器内部错误", |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@router.post( |
| 95 | + "/download", |
| 96 | + response_model=APIResponse, |
| 97 | + dependencies=[Depends(get_current_user)], |
| 98 | +) |
| 99 | +async def download_bangumi(_id: int, torrent: Torrent): |
| 100 | + """ |
| 101 | + 手动下载对应 Bangumi 的种子 |
| 102 | + """ |
| 103 | + try: |
| 104 | + resp = TorrentManager().download_torrent(_id, torrent) |
| 105 | + if resp: |
| 106 | + resp = ResponseModel( |
| 107 | + status_code=200, |
| 108 | + status=True, |
| 109 | + msg_en=f"Download torrent for {_id}", |
| 110 | + msg_zh=f"下载 {_id} 的种子", |
| 111 | + ) |
| 112 | + else: |
| 113 | + resp = ResponseModel( |
| 114 | + status_code=406, |
| 115 | + status=False, |
| 116 | + msg_en=f"Can't find id {_id}", |
| 117 | + msg_zh=f"无法找到 id {_id}", |
| 118 | + ) |
| 119 | + return resp |
| 120 | + except Exception as e: |
| 121 | + logger.error(f"[Bangumi] Error downloading bangumi: {e}") |
| 122 | + return ResponseModel( |
| 123 | + status_code=500, |
| 124 | + status=False, |
| 125 | + msg_en="Internal server error", |
| 126 | + msg_zh="服务器内部错误", |
| 127 | + ) |
0 commit comments