From aeac217138968d89029e0af1365f1dbd0c39c7fa Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Wed, 6 Sep 2023 23:49:20 +0800 Subject: [PATCH] fix: version update. rss add bugs. --- backend/src/module/api/auth.py | 2 +- backend/src/module/api/rss.py | 2 +- backend/src/module/checker/checker.py | 13 ++------- backend/src/module/conf/__init__.py | 1 + backend/src/module/core/program.py | 4 +-- backend/src/module/core/status.py | 2 +- backend/src/module/database/bangumi.py | 4 +-- .../module/parser/analyser/mikan_parser.py | 5 +++- backend/src/module/rss/analyser.py | 3 +- backend/src/module/rss/engine.py | 4 +-- backend/src/module/update/__init__.py | 4 ++- backend/src/module/update/data_migration.py | 2 +- backend/src/module/update/startup.py | 1 - backend/src/module/update/version_check.py | 29 +++++++++++++------ 14 files changed, 42 insertions(+), 34 deletions(-) diff --git a/backend/src/module/api/auth.py b/backend/src/module/api/auth.py index 80b81bfc..613a3d1d 100644 --- a/backend/src/module/api/auth.py +++ b/backend/src/module/api/auth.py @@ -31,7 +31,7 @@ async def login(response: Response, form_data=Depends(OAuth2PasswordRequestForm) @router.get("/refresh_token", response_model=dict, dependencies=[Depends(get_current_user)]) async def refresh(response: Response): token = create_access_token( - data={"sub": get_current_user()}, expires_delta=timedelta(days=1) + data={"sub": active_user[0]}, expires_delta=timedelta(days=1) ) response.set_cookie(key="token", value=token, httponly=True, max_age=86400) return {"access_token": token, "token_type": "bearer"} diff --git a/backend/src/module/api/rss.py b/backend/src/module/api/rss.py index 8c4926cb..6458e5ad 100644 --- a/backend/src/module/api/rss.py +++ b/backend/src/module/api/rss.py @@ -22,7 +22,7 @@ async def get_rss(): @router.post(path="/add", response_model=APIResponse, dependencies=[Depends(get_current_user)]) async def add_rss(rss: RSSItem, ): with RSSEngine() as engine: - result = engine.add_rss(rss.url, rss.name, rss.aggregate) + result = engine.add_rss(rss.url, rss.name, rss.aggregate, rss.parser) return u_response(result) diff --git a/backend/src/module/checker/checker.py b/backend/src/module/checker/checker.py index 1b5c8ea6..c67ed0c5 100644 --- a/backend/src/module/checker/checker.py +++ b/backend/src/module/checker/checker.py @@ -5,6 +5,7 @@ from module.conf import settings, VERSION from module.downloader import DownloadClient from module.models import Config from module.network import RequestContent +from module.update import version_check class Checker: @@ -42,17 +43,7 @@ class Checker: @staticmethod def check_version() -> bool: - if not os.path.exists("config/version.info"): - with open("config/version.info", "w+") as f: - f.writelines(VERSION) - return True - with open("config/version.info", "r+") as f: - legacy_version = f.readlines()[-1] - if VERSION == legacy_version: - return False - else: - f.writelines(VERSION) - return True + return version_check() @staticmethod def check_database() -> bool: diff --git a/backend/src/module/conf/__init__.py b/backend/src/module/conf/__init__.py index 61963a52..9a1b7aef 100644 --- a/backend/src/module/conf/__init__.py +++ b/backend/src/module/conf/__init__.py @@ -7,5 +7,6 @@ from .search_provider import SEARCH_CONFIG TMDB_API = "32b19d6a05b512190a056fa4e747cbbc" DATA_PATH = "sqlite:///data/data.db" LEGACY_DATA_PATH = Path("data/data.json") +VERSION_PATH = Path("config/version.info") PLATFORM = "Windows" if "\\" in settings.downloader.path else "Unix" diff --git a/backend/src/module/core/program.py b/backend/src/module/core/program.py index 8e8b45b3..e17401ce 100644 --- a/backend/src/module/core/program.py +++ b/backend/src/module/core/program.py @@ -1,7 +1,7 @@ import logging from module.conf import VERSION, settings -from module.update import data_migration, database_migration, start_up, first_run +from module.update import data_migration, from_30_to_31, start_up, first_run from .sub_thread import RenameThread, RSSThread @@ -43,7 +43,7 @@ class Program(RenameThread, RSSThread): data_migration() elif self.version_update: # Update database - database_migration() + from_30_to_31() logger.info("Database updated.") self.start() diff --git a/backend/src/module/core/status.py b/backend/src/module/core/status.py index d939786c..5e55e42a 100644 --- a/backend/src/module/core/status.py +++ b/backend/src/module/core/status.py @@ -49,7 +49,7 @@ class ProgramStatus(Checker): @property def version_update(self): - return self.check_version() + return not self.check_version() @property def database(self): diff --git a/backend/src/module/database/bangumi.py b/backend/src/module/database/bangumi.py index 1a758e1c..72950d27 100644 --- a/backend/src/module/database/bangumi.py +++ b/backend/src/module/database/bangumi.py @@ -111,8 +111,8 @@ class BangumiDatabase: if rss_link not in match_data.rss_link: match_data.rss_link += f",{rss_link}" self.update_rss(match_data.title_raw, match_data.rss_link) - if not match_data.poster_link: - self.update_poster(match_data.title_raw, torrent.poster_link) + # if not match_data.poster_link: + # self.update_poster(match_data.title_raw, torrent.poster_link) torrent_list.pop(i) break else: diff --git a/backend/src/module/parser/analyser/mikan_parser.py b/backend/src/module/parser/analyser/mikan_parser.py index 503c1af8..7cc30d20 100644 --- a/backend/src/module/parser/analyser/mikan_parser.py +++ b/backend/src/module/parser/analyser/mikan_parser.py @@ -1,9 +1,11 @@ from bs4 import BeautifulSoup +from urllib3.util import parse_url from module.network import RequestContent def mikan_parser(homepage: str): + root_path = parse_url(homepage).host with RequestContent() as req: content = req.get_html(homepage) soup = BeautifulSoup(content, "html.parser") @@ -14,5 +16,6 @@ def mikan_parser(homepage: str): ).text if poster_style: poster_path = poster_style.split("url('")[1].split("')")[0] - return poster_path, official_title + poster_link = f"https://{root_path}{poster_path}" + return poster_link, official_title return "", "" diff --git a/backend/src/module/rss/analyser.py b/backend/src/module/rss/analyser.py index f73b7d3a..7dcdd355 100644 --- a/backend/src/module/rss/analyser.py +++ b/backend/src/module/rss/analyser.py @@ -18,12 +18,13 @@ class RSSAnalyser(TitleParser): torrent.homepage ) elif rss.parser == "tmdb": - tmdb_title, season, year = self.tmdb_parser( + tmdb_title, season, year, poster_link = self.tmdb_parser( bangumi.official_title, bangumi.season, settings.rss_parser.language ) bangumi.official_title = tmdb_title bangumi.year = year bangumi.season = season + bangumi.poster_link = poster_link else: pass bangumi.official_title = re.sub(r"[/:.\\]", " ", bangumi.official_title) diff --git a/backend/src/module/rss/engine.py b/backend/src/module/rss/engine.py index b5731230..e6932453 100644 --- a/backend/src/module/rss/engine.py +++ b/backend/src/module/rss/engine.py @@ -32,7 +32,7 @@ class RSSEngine(Database): else: return [] - def add_rss(self, rss_link: str, name: str | None = None, aggregate: bool = True): + def add_rss(self, rss_link: str, name: str | None = None, aggregate: bool = True, parser: str = "mikan"): if not name: with RequestContent() as req: name = req.get_rss_title(rss_link) @@ -43,7 +43,7 @@ class RSSEngine(Database): msg_en="Failed to get RSS title.", msg_zh="无法获取 RSS 标题。", ) - rss_data = RSSItem(name=name, url=rss_link, aggregate=aggregate) + rss_data = RSSItem(name=name, url=rss_link, aggregate=aggregate, parser=parser) if self.rss.add(rss_data): return ResponseModel( status=True, diff --git a/backend/src/module/update/__init__.py b/backend/src/module/update/__init__.py index 1c991151..ca1f94c5 100644 --- a/backend/src/module/update/__init__.py +++ b/backend/src/module/update/__init__.py @@ -1,2 +1,4 @@ -from .data_migration import data_migration, database_migration +from .data_migration import data_migration from .startup import start_up, first_run +from .version_check import version_check +from .cross_version import from_30_to_31 diff --git a/backend/src/module/update/data_migration.py b/backend/src/module/update/data_migration.py index e007e94a..f9775579 100644 --- a/backend/src/module/update/data_migration.py +++ b/backend/src/module/update/data_migration.py @@ -12,7 +12,7 @@ def data_migration(): rss_link = old_data["rss_link"] new_data = [] for info in infos: - new_data.append(Bangumi(**info, rss_link=[rss_link])) + new_data.append(Bangumi(**info, rss_link=rss_link)) with RSSEngine() as engine: engine.bangumi.add_all(new_data) engine.add_rss(rss_link) diff --git a/backend/src/module/update/startup.py b/backend/src/module/update/startup.py index b0f60c38..a80120c0 100644 --- a/backend/src/module/update/startup.py +++ b/backend/src/module/update/startup.py @@ -1,7 +1,6 @@ import logging from module.rss import RSSEngine -from module.conf import settings logger = logging.getLogger(__name__) diff --git a/backend/src/module/update/version_check.py b/backend/src/module/update/version_check.py index 91550bdf..f146a248 100644 --- a/backend/src/module/update/version_check.py +++ b/backend/src/module/update/version_check.py @@ -1,12 +1,23 @@ -from module.conf import VERSION + + +from module.conf import VERSION, VERSION_PATH def version_check() -> bool: - with open("config/version.txt", "rw") as f: - # Read last version - versions = f.readlines() - if VERSION[:3] > versions[-1][:3]: - f.write(VERSION[:3] + "\n") - return False - else: - return True + if not VERSION_PATH.exists(): + with open(VERSION_PATH, "w") as f: + f.write(VERSION + "\n") + return False + else: + with open(VERSION_PATH, "r+") as f: + # Read last version + versions = f.readlines() + last_version = versions[-1] + if VERSION == last_version: + return True + else: + if VERSION[:3] > versions[-1][:3]: + f.write(VERSION[:3] + "\n") + return False + else: + return True