Skip to content

Commit

Permalink
Merge pull request #282 from EstrellaXD/3.0-dev
Browse files Browse the repository at this point in the history
3.0.1
  • Loading branch information
EstrellaXD authored Jun 3, 2023
2 parents ac2f7e0 + 7676b12 commit 085fb35
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cython_debug/

test.*
.run
/dev.sh
/src/templates/
/src/config/
/src/debuger.py
/src/dist.zip
24 changes: 24 additions & 0 deletions dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash


# This script is used to run the development environment.

python3 -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple install -r requirements.txt

cd src || exit

CONFIG_DIR="config"

if [ ! -d "$CONFIG_DIR" ]; then
echo "The directory '$CONFIG_DIR' is missing."
mkdir config
fi

VERSION_FILE="module/__version__.py"

if [ ! -f "$VERSION_FILE" ]; then
echo "The file '$VERSION_FILE' is missing."
echo "VERSION='DEV_VERSION'" >> "$VERSION_FILE"
fi

python3 main.py
14 changes: 7 additions & 7 deletions src/module/api/bangumi.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -33,7 +33,7 @@ async def get_data(bangumi_id: str, current_user=Depends(get_current_user)):


@router.post("/api/v1/bangumi/updateRule", tags=["bangumi"])
async def update_data(data: BangumiData, current_user=Depends(get_current_user)):
async def update_rule(data: BangumiData, current_user=Depends(get_current_user)):
if not current_user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token"
Expand All @@ -43,7 +43,7 @@ async def update_data(data: BangumiData, current_user=Depends(get_current_user))


@router.delete("/api/v1/bangumi/deleteRule/{bangumi_id}", tags=["bangumi"])
async def delete_data(bangumi_id: str, file:bool = False, current_user=Depends(get_current_user)):
async def delete_rule(bangumi_id: str, file: bool = False, current_user=Depends(get_current_user)):
if not current_user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token"
Expand All @@ -53,7 +53,7 @@ async def delete_data(bangumi_id: str, file:bool = False, current_user=Depends(g


@router.delete("/api/v1/bangumi/disableRule/{bangumi_id}", tags=["bangumi"])
async def delete_rule(
async def disable_rule(
bangumi_id: str, file: bool = False, current_user=Depends(get_current_user)
):
if not current_user:
Expand All @@ -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"})
4 changes: 2 additions & 2 deletions src/module/api/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ async def get_search_result(searchstr: str):


@router.get("/RSS/Bangumi", tags=["proxy"])
async def get_bangumi(bangumiId: str, groupid: str):
full_path = "Bangumi?bangumiId=" + bangumiId + "&groupid=" + groupid
async def get_bangumi(bangumiId: str, subgroupid: str):
full_path = "Bangumi?bangumiId=" + bangumiId + "&subgroupid=" + subgroupid
content = get_rss_content(full_path)
return Response(content, media_type="application/xml")

Expand Down
10 changes: 6 additions & 4 deletions src/module/checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class Checker:
def __init__(self):
pass

def check_renamer(self) -> bool:
if self.check_downloader() and settings.bangumi_manage.enable:
@staticmethod
def check_renamer() -> bool:
if settings.bangumi_manage.enable:
return True
else:
return False

def check_analyser(self) -> bool:
if self.check_downloader() and settings.rss_parser.enable:
@staticmethod
def check_analyser() -> bool:
if settings.rss_parser.enable:
return True
else:
return False
Expand Down
17 changes: 10 additions & 7 deletions src/module/core/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ def startup(self):
"Legacy data detected, starting data migration, please wait patiently."
)
data_migration()
add_rss_feed()
self.start()

def start(self):
if self.first_run:
return {"status": "Not ready to start."}
self.stop_event.clear()
settings.load()
if self.enable_renamer:
self.rename_start()
if self.enable_rss:
self.rss_start()
logger.info("Program running.")
return {"status": "Program started."}
if self.downloader_status:
if self.enable_renamer:
self.rename_start()
if self.enable_rss:
add_rss_feed()
self.rss_start()
logger.info("Program running.")
return {"status": "Program started."}
else:
return {"status": "Can't connect to downloader. Program not paused."}

def stop(self):
if self.is_running:
Expand Down
29 changes: 20 additions & 9 deletions src/module/downloader/client/qb_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
)

from module.ab_decorator import qb_connect_failed_wait
from module.downloader.exceptions import ConflictError

logger = logging.getLogger(__name__)

Expand All @@ -27,9 +26,9 @@ def __init__(self, host: str, username: str, password: str, ssl: bool):
self.host = host
self.username = username

def auth(self):
def auth(self, retry=3):
times = 0
while times < 3:
while times < retry:
try:
self._client.auth_log_in()
return True
Expand All @@ -46,7 +45,8 @@ def auth(self):
except APIConnectionError:
logger.error(f"Cannot connect to qBittorrent Server")
logger.info(f"Please check the IP and port in WebUI settings")
time.sleep(30)
time.sleep(10)
times += 1
except Exception as e:
logger.error(f"Unknown error: {e}")
break
Expand All @@ -55,6 +55,16 @@ def auth(self):
def logout(self):
self._client.auth_log_out()

def check_host(self):
try:
self._client.app_version()
return True
except APIConnectionError:
return False

def check_rss(self, rss_link: str):
pass

@qb_connect_failed_wait
def prefs_init(self, prefs):
return self._client.app_set_preferences(prefs=prefs)
Expand Down Expand Up @@ -93,15 +103,16 @@ def torrents_rename_file(self, torrent_hash, old_path, new_path) -> bool:
return False

def rss_add_feed(self, url, item_path):
self._client.rss_add_feed(url, item_path)
try:
self._client.rss_add_feed(url, item_path)
except Conflict409Error:
logger.warning(f"[Downloader] RSS feed {url} already exists")

def rss_remove_item(self, item_path):
try:
self._client.rss_remove_item(item_path)
except Conflict409Error as e:
logger.debug(e)
logger.info("Add new RSS")
raise ConflictError()
except Conflict409Error:
logger.warning(f"[Downloader] RSS item {item_path} does not exist")

def rss_get_feeds(self):
return self._client.rss_items()
Expand Down
10 changes: 9 additions & 1 deletion src/module/downloader/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def __getClient():
def __enter__(self):
if not self.authed:
self.auth()
else:
logger.error("[Downloader] Already authed.")
return self

def __exit__(self, exc_type, exc_val, exc_tb):
Expand All @@ -42,7 +44,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):

def auth(self):
self.authed = self.client.auth()
logger.debug("Authed.")
if self.authed:
logger.info("[Downloader] Authed.")
else:
logger.error("[Downloader] Auth failed.")

def check_host(self):
return self.client.check_host()

def init_downloader(self):
prefs = {
Expand Down
12 changes: 6 additions & 6 deletions src/module/downloader/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class TorrentPath:
def __init__(self):
self.download_path = settings.downloader.path
def __init__(self, download_path: str = settings.downloader.path):
self.download_path = download_path

@staticmethod
def check_files(info):
Expand Down Expand Up @@ -44,11 +44,11 @@ def _path_to_bangumi(self, save_path):
return bangumi_name, season

@staticmethod
def _file_depth(path):
return len(path.split(path.sep))
def _file_depth(file_path):
return len(file_path.split(path.sep))

def is_ep(self, path):
return self._file_depth(path) <= 2
def is_ep(self, file_path):
return self._file_depth(file_path) <= 2

def _gen_save_path(self, data: BangumiData):
folder = (
Expand Down
4 changes: 3 additions & 1 deletion src/module/manager/collector.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,9 +33,9 @@ def subscribe_season(self, data: BangumiData):
with BangumiDatabase() as db:
data.added = True
data.eps_collect = True
self.set_rule(data)
db.insert(data)
self.add_rss_feed(data.rss_link[0], item_path=data.official_title)
self.set_rule(data)


def eps_complete():
Expand Down
Loading

0 comments on commit 085fb35

Please sign in to comment.