mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-05-11 18:45:58 +08:00
feat: 优化 API 返回值
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from .log import router
|
||||
|
||||
from module.models import BangumiData
|
||||
from module.database import BangumiDatabase
|
||||
from module.manager import TorrentManager
|
||||
from module.security import get_current_user
|
||||
|
||||
@@ -80,6 +80,6 @@ async def reset_all(current_user=Depends(get_current_user)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token"
|
||||
)
|
||||
with BangumiDatabase() as database:
|
||||
database.delete_all()
|
||||
return {"status": "ok"}
|
||||
with TorrentManager() as torrent:
|
||||
torrent.delete_all()
|
||||
return JSONResponse(status_code=200, content={"message": "OK"})
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import logging
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from module.downloader import DownloadClient
|
||||
from module.models import BangumiData
|
||||
from module.database import BangumiDatabase
|
||||
|
||||
@@ -21,15 +21,9 @@ class TorrentManager(BangumiDatabase):
|
||||
if hash_list:
|
||||
client.delete_torrent(hash_list)
|
||||
logger.info(f"Delete rule and torrents for {data.official_title}")
|
||||
return {
|
||||
"status": "success",
|
||||
"msg": f"Delete torrents for {data.official_title}",
|
||||
}
|
||||
return f"Delete torrents for {data.official_title}"
|
||||
else:
|
||||
return {
|
||||
"status": "error",
|
||||
"msg": f"Can't find torrents for {data.official_title}",
|
||||
}
|
||||
return f"Can't find torrents for {data.official_title}"
|
||||
|
||||
def delete_rule(self, _id: int | str, file: bool = False):
|
||||
data = self.search_id(int(_id))
|
||||
@@ -39,34 +33,19 @@ class TorrentManager(BangumiDatabase):
|
||||
client.remove_rss_feed(data.official_title)
|
||||
self.delete_one(int(_id))
|
||||
if file:
|
||||
self.delete_torrents(data, client)
|
||||
torrent_message = self.delete_torrents(data, client)
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Delete rule and torrents for {data.official_title}",
|
||||
"msg": f"Delete rule and torrents for {data.official_title}, "
|
||||
f"{torrent_message}",
|
||||
})
|
||||
logger.info(f"[Manager] Delete rule for {data.official_title}")
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Delete rule for {data.official_title}",
|
||||
"msg": f"Delete rule for {data.official_title}"
|
||||
})
|
||||
else:
|
||||
return JSONResponse(status_code=406, content={
|
||||
{"status": "error", "msg": f"Can't find id {_id}"}
|
||||
"msg": f"Can't find id {_id}"
|
||||
})
|
||||
# data = self.search_id(int(_id))
|
||||
# if isinstance(data, BangumiData):
|
||||
# self.delete_one(int(_id))
|
||||
# if file:
|
||||
# self.delete_torrents(data)
|
||||
# logger.info(f"Delete {data.official_title} and torrents.")
|
||||
# return {
|
||||
# "status": "success",
|
||||
# "msg": f"Delete {data.official_title} and torrents.",
|
||||
# }
|
||||
# logger.info(f"Delete {data.official_title}")
|
||||
# return {"status": "success", "msg": f"Delete {data.official_title}"}
|
||||
# else:
|
||||
# return data
|
||||
|
||||
def disable_rule(self, _id: str | int, file: bool = False):
|
||||
data = self.search_id(int(_id))
|
||||
@@ -76,19 +55,18 @@ class TorrentManager(BangumiDatabase):
|
||||
data.deleted = True
|
||||
self.update_one(data)
|
||||
if file:
|
||||
self.delete_torrents(data, client)
|
||||
torrent_message = self.delete_torrents(data, client)
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Disable rule and delete torrents for {data.official_title}",
|
||||
"msg": f"Disable rule and delete torrents for {data.official_title}"
|
||||
f"{torrent_message}",
|
||||
})
|
||||
logger.info(f"[Manager] Disable rule for {data.official_title}")
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Disable rule for {data.official_title}",
|
||||
})
|
||||
else:
|
||||
return JSONResponse(status_code=406, content={
|
||||
{"status": "error", "msg": f"Can't find id {_id}"}
|
||||
"msg": f"Can't find id {_id}"
|
||||
})
|
||||
|
||||
def enable_rule(self, _id: str | int):
|
||||
@@ -100,20 +78,22 @@ class TorrentManager(BangumiDatabase):
|
||||
client.set_rule(data)
|
||||
logger.info(f"[Manager] Enable rule for {data.official_title}")
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Enable rule for {data.official_title}",
|
||||
})
|
||||
else:
|
||||
return JSONResponse(status_code=406, content={
|
||||
"msg": f"Can't find bangumi id {_id}"
|
||||
})
|
||||
|
||||
def update_rule(self, data: BangumiData):
|
||||
old_data = self.search_id(data.id)
|
||||
if not old_data:
|
||||
logger.error(f"[Manager] Can't find data with id {data.id}")
|
||||
return JSONResponse(status_code=406, content={
|
||||
"status": "error",
|
||||
"msg": f"Can't find data with id {data.id}"
|
||||
})
|
||||
else:
|
||||
# Set torrent path
|
||||
# Move torrent
|
||||
match_list = self.__match_torrents_list(data)
|
||||
with DownloadClient() as client:
|
||||
path = client._gen_save_path(data)
|
||||
@@ -124,7 +104,6 @@ class TorrentManager(BangumiDatabase):
|
||||
client.set_rule(data)
|
||||
self.update_one(data)
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "success",
|
||||
"msg": f"Set new path for {data.official_title}",
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user