Skip to content

Commit

Permalink
update download client: could auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
shininome committed Jan 21, 2025
1 parent 3829b98 commit 8c27576
Show file tree
Hide file tree
Showing 42 changed files with 786 additions and 677 deletions.
11 changes: 3 additions & 8 deletions backend/src/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import logging
import os
import sys
from pathlib import Path

import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from module.api import lifespan, v1
from module.conf import VERSION, settings, setup_logger
from module.utils import load_image

if sys.platform == "win32":
from pathlib import PureWindowsPath as Path
else:
from pathlib import Path

setup_logger(reset=True)
logger = logging.getLogger(__name__)
uvicorn_logging_config = {
Expand Down Expand Up @@ -47,6 +43,7 @@ def create_app() -> FastAPI:

@app.get("/posters/{path:path}", tags=["posters"])
async def posters(path: str):
# TODO: 肯定在这里是不合适的
post_path = Path("data/posters") / path
if not post_path.exists():
await load_image(path)
Expand All @@ -67,9 +64,7 @@ def html(request: Request, path: str):
else:
context = {"request": request}
return templates.TemplateResponse("index.html", context)

else:

@app.get("/", status_code=302, tags=["html"])
def index():
return RedirectResponse("/docs")
Expand Down
1 change: 0 additions & 1 deletion backend/src/module/api/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ async def analysis(rss: RSSItem):
"/collect", response_model=APIResponse, dependencies=[Depends(get_current_user)]
)
async def download_collection(data: Bangumi):
# resp = await collector.collect_season(data, data.rss_link)
await engine.refresh_rss(bangumi=data)
# TODO: resp 要等后面统一改
resp = True
Expand Down
29 changes: 8 additions & 21 deletions backend/src/module/api/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio

from fastapi import APIRouter, Depends, Query
from sse_starlette.sse import EventSourceResponse

Expand All @@ -17,29 +15,18 @@ async def search_torrents(site: str = "mikan", keywords: str = Query(None)):
"""
Server Send Event for per Bangumi item
"""
if not keywords:
# TODO: 是否要大于一定才开始
if not keywords or len(keywords) < 2:
return []
keywords:list[str] = keywords.split(" ")
keywords: list[str] = keywords.split(" ")

# return await SearchTorrent().analyse_keyword(keywords=keywords, site=site)
return EventSourceResponse(
await SearchTorrent().analyse_keyword(keywords=keywords, site=site)
SearchTorrent().analyse_keyword(keywords=keywords, site=site)
)


@router.get(
"/provider", response_model=list[str], dependencies=[Depends(get_current_user)]
)
async def search_provider():
@router.get( "/provider", response_model=list[str], dependencies=[Depends(get_current_user)])
async def search_provider()->list[str]:
"""从配置文件中获取支持的搜索引擎
"""
return list(SEARCH_CONFIG.keys())


if __name__ == "__main__":
import json

ans = asyncio.run(search_torrents(keywords="败北 ANI"))
for i in list(ans):
i = json.loads(i)
print(i)
# decoded_objects = []
# for json_str in next(ans):
26 changes: 9 additions & 17 deletions backend/src/module/checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import httpx

from module.conf import VERSION, settings
from module.downloader import DownloadClient
from module.downloader import Client
from module.models import Config
from module.network import RequestContent
from module.update import version_check
Expand Down Expand Up @@ -51,22 +51,12 @@ def check_database() -> bool:

@staticmethod
async def check_downloader() -> bool:
# 改动说明: 之前是要能连上, 现在只要检测到 host 就好了
try:
url = (
f"http://{settings.downloader.host_}"
if "://" not in settings.downloader.host_
else f"{settings.downloader.host_}"
)

# response = await RequestContent().get(url, timeout=2)
# if settings.downloader.type in response.text.lower():
# 能auth 就算正确, 类型判断无用
async with DownloadClient() as client:
if await client.auth():
return True
client = Client
if await client.downloader.check_host():
return True
return False
# else:
# return False
except httpx.ReadTimeout:
logger.error("[Checker] Downloader connect timeout.")
return False
Expand All @@ -86,6 +76,8 @@ def check_img_cache() -> bool:
img_path.mkdir()
return False

if __name__ =="__main__":

if __name__ == "__main__":
import asyncio
# print(asyncio.run(Checker().check_downloader()))

print(asyncio.run(Checker().check_downloader()))
1 change: 1 addition & 0 deletions backend/src/module/core/aiocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ async def run(self):
async def download_task_loop(self):
while True:
await self.download_task()
await asyncio.sleep(10)

async def download_task(self):
try:
Expand Down
22 changes: 16 additions & 6 deletions backend/src/module/database/bangumi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@


class BangumiDatabase:
"""
TODO: 对 Bangumi 的一些新想法
主键为 official_title, link, 季度
判断一个 torrent name 是不是在 bangumi 里面
先通过 name, link 来找一下, 然后看看季度有没有
有一点问题是 现在有了一个 bangumi, 但是季度是改过的(如物语系列)
当有俩个的时候, 一个已经加进去了, 另外一个没有,
这时候会去解析, 由于季度不一样, 会被认为是多个 bangumi
所以默认同一个 link 内的季度是一样的
#FIXME :
这就有了新问题, 在一个动漫连续多个季度的时候, 会被认为是同一个动漫
一个暂时的解决方案是, 通过 season_raw 来判断, 如果当前存在一个季度更大的, 就不添加
#ERROR : 这就会导致一个sb 的动漫是 S2 但是 TMDB 分到了 S1, 导致多加一个
有大选大, 没有大再看看 mikan/tmdb 给的是什么
"""
def __init__(self, session: Session):
self.session = session

def add(self, data: Bangumi):
"""link 相同, official_title相同,就只补充,主要是为了
一些会改名的
Args:
data: [TODO:description]
Returns:
[TODO:return]
"""
# 如果official_title 一致,将title_raw,group,更新
statement = select(Bangumi).where(
Expand Down
3 changes: 3 additions & 0 deletions backend/src/module/database/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@


class Database(Session):
"""
要提供几个交插的方法
"""
def __init__(self, engine=e):
self.engine = engine
super().__init__(engine)
Expand Down
5 changes: 5 additions & 0 deletions backend/src/module/database/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@


class RSSDatabase:
"""
RSS 是一切的开始, bangumi 和 torrent 都以 rss 为外键
rss 的主码是 rss_link, id
现在是 rss 的 id 变动时, bangumi 和 torrent 的 rss_id 也要变动
"""
def __init__(self, session: Session):
self.session = session

Expand Down
5 changes: 5 additions & 0 deletions backend/src/module/database/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ def check_new(self, torrents_list: list[Torrent]) -> list[Torrent]:
return new_torrents

def delete(self, _id: int) -> bool:
# 思考什么时候删除种子
# 1. 当bangumi已经删除时, 如果删除,会在重命名的时候再次添加
# 2. 当种子也删除时, 不会再次添加
# 3. bangumi 删除有几种情况: 1. 有一个全清, 会刷新一次 2. 用户自已删除, 如果是聚合的, 会在下次
# 刷新时再次添加, 如果是单独的, 会连着rss 一起删除
condition = delete(Torrent).where(Torrent.id == _id)
try:
self.session.exec(condition)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/module/downloader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .download_client import DownloadClient
from .download_client import Client, DownloadClient
from .download_queue import AsyncDownloadController, DownloadQueue

__all__ = ["DownloadClient", "DownloadQueue","AsyncDownloadController"]
__all__ = ["DownloadClient", "DownloadQueue","AsyncDownloadController","Client"]
5 changes: 2 additions & 3 deletions backend/src/module/downloader/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

from module.conf import settings
from module.downloader.client.expection import AuthorizationError

logger = logging.getLogger(__name__)
from .base_downloader import BaseDownloader

download_type = settings.downloader.type
package_path = f"module.downloader.client.{download_type}"
downloader = importlib.import_module(package_path)
Downloader = downloader.Downloader
__all__ = ["Downloader", "AuthorizationError"]
__all__ = ["Downloader", "AuthorizationError", "BaseDownloader"]
46 changes: 46 additions & 0 deletions backend/src/module/downloader/client/base_downloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from abc import ABCMeta, abstractmethod


# 主要实现以下 个功能
# 1. renamer,用以实现重命名功能 hash, old name, new name
# 2. move, 用以实现改数据后的转移
# 3. add, 用以加种子
# 4. get_info, 获取当前所有的有关种子
# 5. auth, 用以登陆
# 6. check_host, 用以检查连通性
# 7. logout,用以登出
#
class BaseDownloader(metaclass=ABCMeta):
@abstractmethod
async def auth(self):
pass

@abstractmethod
async def check_host(self):
pass

@abstractmethod
async def logout(self):
pass

@abstractmethod
async def get_torrent_files(self, hash):
pass
@abstractmethod
async def torrents_info(self, status_filter , category , tag, limit):
pass

@abstractmethod
async def rename(self, torrent_hash,old_path, new_path):
pass
@abstractmethod
async def move(self, hashes, new_location):
pass
@abstractmethod
async def add(self, torrent_urls, torrent_files, save_path, category):
pass
@abstractmethod
async def delete(self, hashes):
pass


10 changes: 0 additions & 10 deletions backend/src/module/downloader/client/qbittorrent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class Config(BaseModel):
type: str = Field(default="qbittorrent", description="Downloader type")
host: str = Field(
default="http://172.17.0.1:8080", alias="host", description="Downloader host"
)
Expand All @@ -12,15 +11,6 @@ class Config(BaseModel):
password: str = Field(
default="adminadmin", alias="password", description="Downloader password"
)
path: str = Field(default="/Downloads/Bangumi", description="Downloader path")
ssl: bool = Field(default=False, description="Downloader ssl")

@validator("host", pre=True)
def validate_host(cls, value: str) -> str:
# 如果输入值没有以 http:// 或 https:// 开头,自动加上 http://
if not value.startswith(("http://", "https://")):
value = f"http://{value}"
return value


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 8c27576

Please sign in to comment.