diff --git a/.dockerignore b/.dockerignore index 94a64c67a..595cb0a55 100644 --- a/.dockerignore +++ b/.dockerignore @@ -19,8 +19,8 @@ coverage.xml ../.pytest_cache .hypothesis -src/module/tests -src/module/conf/const_dev.py +backend/src/module/tests +backend/src/module/conf/const_dev.py config/bangumi.json/config/bangumi.json /docs /.github @@ -33,8 +33,8 @@ config/bangumi.json/config/bangumi.json dist.zip data config -/src/config -/src/data +/backend/src/config +/backend/src/data .pytest_cache test .env diff --git a/.gitignore b/.gitignore index f6222a65c..2fba0a131 100644 --- a/.gitignore +++ b/.gitignore @@ -162,18 +162,18 @@ cython_debug/ #.idea/ # Custom -/src/test.py +/backend/src/test.py -/src/module/run_debug.sh -/src/module/debug_run.sh -/src/module/__version__.py -/src/data/ +/backend/src/module/run_debug.sh +/backend/src/module/debug_run.sh +/backend/src/module/__version__.py +/backend/src/data/ -/src/module/conf/config_dev.ini +/backend/src/module/conf/config_dev.ini test.* .run -/src/templates/ -/src/config/ -/src/debuger.py -/src/dist.zip +/backend/src/templates/ +/backend/src/config/ +/backend/src/debuger.py +/backend/src/dist.zip diff --git a/Dockerfile b/Dockerfile index 7f58121f7..dd4d75340 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,8 +45,8 @@ RUN apk add --no-cache \ /root/.cache \ /tmp/* -COPY --chmod=755 src/. . -COPY --chmod=755 src/docker / +COPY --chmod=755 backend/src/. . +COPY --chmod=755 backend/src/docker / ENTRYPOINT [ "/init" ] diff --git a/backend/.pre-commit-config.yaml b/backend/.pre-commit-config.yaml new file mode 100644 index 000000000..daecff1d1 --- /dev/null +++ b/backend/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: +- repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + language: python + +# TODO: add ruff lint check before committing \ No newline at end of file diff --git a/backend/pyproject.toml b/backend/pyproject.toml new file mode 100644 index 000000000..8342c4b9f --- /dev/null +++ b/backend/pyproject.toml @@ -0,0 +1,63 @@ +[tool.ruff] +select = [ + # pycodestyle(E): https://beta.ruff.rs/docs/rules/#pycodestyle-e-w + "E", + # Pyflakes(F): https://beta.ruff.rs/docs/rules/#pyflakes-f + "F", + # isort(I): https://beta.ruff.rs/docs/rules/#isort-i + "I" +] +ignore = [ + # E501: https://beta.ruff.rs/docs/rules/line-too-long/ + 'E501', + # F401: https://beta.ruff.rs/docs/rules/unused-import/ + # avoid unused imports lint in `__init__.py` + 'F401', +] + +# Allow autofix for all enabled rules (when `--fix`) is provided. +fixable = ["A", "B", "C", "D", "E", "F", "G", "I", "N", "Q", "S", "T", "W", "ANN", "ARG", "BLE", "COM", "DJ", "DTZ", "EM", "ERA", "EXE", "FBT", "ICN", "INP", "ISC", "NPY", "PD", "PGH", "PIE", "PL", "PT", "PTH", "PYI", "RET", "RSE", "RUF", "SIM", "SLF", "TCH", "TID", "TRY", "UP", "YTT"] +unfixable = [] + +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".mypy_cache", + ".nox", + ".pants.d", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "venv", +] +per-file-ignores = {} + +# Same as Black. +line-length = 88 + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +# Assume Python 3.10. +target-version = "py310" + +[tool.ruff.mccabe] +# Unlike Flake8, default to a complexity level of 10. +max-complexity = 10 + +[tool.black] +line-length = 88 +target-version = ['py310', 'py311'] diff --git a/src/docker/etc/cont-init.d/010-old-compatible b/backend/src/docker/etc/cont-init.d/010-old-compatible similarity index 100% rename from src/docker/etc/cont-init.d/010-old-compatible rename to backend/src/docker/etc/cont-init.d/010-old-compatible diff --git a/src/docker/etc/cont-init.d/020-fixuser b/backend/src/docker/etc/cont-init.d/020-fixuser similarity index 100% rename from src/docker/etc/cont-init.d/020-fixuser rename to backend/src/docker/etc/cont-init.d/020-fixuser diff --git a/src/docker/etc/services.d/auto_bangumi/finish b/backend/src/docker/etc/services.d/auto_bangumi/finish similarity index 100% rename from src/docker/etc/services.d/auto_bangumi/finish rename to backend/src/docker/etc/services.d/auto_bangumi/finish diff --git a/src/docker/etc/services.d/auto_bangumi/notification-fd b/backend/src/docker/etc/services.d/auto_bangumi/notification-fd similarity index 100% rename from src/docker/etc/services.d/auto_bangumi/notification-fd rename to backend/src/docker/etc/services.d/auto_bangumi/notification-fd diff --git a/src/docker/etc/services.d/auto_bangumi/run b/backend/src/docker/etc/services.d/auto_bangumi/run similarity index 100% rename from src/docker/etc/services.d/auto_bangumi/run rename to backend/src/docker/etc/services.d/auto_bangumi/run diff --git a/src/icon b/backend/src/icon similarity index 100% rename from src/icon rename to backend/src/icon diff --git a/src/main.py b/backend/src/main.py similarity index 100% rename from src/main.py rename to backend/src/main.py diff --git a/src/module/__init__.py b/backend/src/module/__init__.py similarity index 100% rename from src/module/__init__.py rename to backend/src/module/__init__.py diff --git a/src/module/ab_decorator/__init__.py b/backend/src/module/ab_decorator/__init__.py similarity index 97% rename from src/module/ab_decorator/__init__.py rename to backend/src/module/ab_decorator/__init__.py index f25a083b8..3c29c227a 100644 --- a/src/module/ab_decorator/__init__.py +++ b/backend/src/module/ab_decorator/__init__.py @@ -38,4 +38,5 @@ def locked(func): def wrapper(*args, **kwargs): with lock: return func(*args, **kwargs) - return wrapper \ No newline at end of file + + return wrapper diff --git a/src/module/api/__init__.py b/backend/src/module/api/__init__.py similarity index 100% rename from src/module/api/__init__.py rename to backend/src/module/api/__init__.py diff --git a/src/module/api/auth.py b/backend/src/module/api/auth.py similarity index 90% rename from src/module/api/auth.py rename to backend/src/module/api/auth.py index bbea2090b..de255dd3c 100644 --- a/src/module/api/auth.py +++ b/backend/src/module/api/auth.py @@ -19,9 +19,7 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()): username = form_data.username password = form_data.password auth_user(username, password) - token = create_access_token( - data={"sub": username}, expires_delta=timedelta(days=1) - ) + token = create_access_token(data={"sub": username}, expires_delta=timedelta(days=1)) return {"access_token": token, "token_type": "bearer", "expire": 86400} @@ -32,10 +30,7 @@ async def refresh(current_user: User = Depends(get_current_user)): raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="invalid token" ) - token = create_access_token( - data = {"sub": current_user.username} - - ) + token = create_access_token(data={"sub": current_user.username}) return {"access_token": token, "token_type": "bearer", "expire": 86400} diff --git a/src/module/api/bangumi.py b/backend/src/module/api/bangumi.py similarity index 96% rename from src/module/api/bangumi.py rename to backend/src/module/api/bangumi.py index 94c77c7f5..611e9c43e 100644 --- a/src/module/api/bangumi.py +++ b/backend/src/module/api/bangumi.py @@ -11,6 +11,7 @@ logger = logging.getLogger(__name__) + @router.get( "/api/v1/bangumi/getAll", tags=["bangumi"], response_model=list[BangumiData] ) @@ -50,7 +51,9 @@ async def update_rule(data: BangumiData, current_user=Depends(get_current_user)) @router.delete("/api/v1/bangumi/deleteRule/{bangumi_id}", tags=["bangumi"]) -async def delete_rule(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" diff --git a/src/module/api/config.py b/backend/src/module/api/config.py similarity index 100% rename from src/module/api/config.py rename to backend/src/module/api/config.py diff --git a/src/module/api/download.py b/backend/src/module/api/download.py similarity index 100% rename from src/module/api/download.py rename to backend/src/module/api/download.py diff --git a/src/module/api/log.py b/backend/src/module/api/log.py similarity index 100% rename from src/module/api/log.py rename to backend/src/module/api/log.py diff --git a/src/module/api/program.py b/backend/src/module/api/program.py similarity index 100% rename from src/module/api/program.py rename to backend/src/module/api/program.py diff --git a/src/module/api/proxy.py b/backend/src/module/api/proxy.py similarity index 100% rename from src/module/api/proxy.py rename to backend/src/module/api/proxy.py diff --git a/src/module/api/web.py b/backend/src/module/api/web.py similarity index 100% rename from src/module/api/web.py rename to backend/src/module/api/web.py diff --git a/src/module/checker/__init__.py b/backend/src/module/checker/__init__.py similarity index 100% rename from src/module/checker/__init__.py rename to backend/src/module/checker/__init__.py diff --git a/src/module/checker/checker.py b/backend/src/module/checker/checker.py similarity index 100% rename from src/module/checker/checker.py rename to backend/src/module/checker/checker.py diff --git a/src/module/conf/__init__.py b/backend/src/module/conf/__init__.py similarity index 100% rename from src/module/conf/__init__.py rename to backend/src/module/conf/__init__.py diff --git a/src/module/conf/config.py b/backend/src/module/conf/config.py similarity index 100% rename from src/module/conf/config.py rename to backend/src/module/conf/config.py diff --git a/src/module/conf/const.py b/backend/src/module/conf/const.py similarity index 100% rename from src/module/conf/const.py rename to backend/src/module/conf/const.py diff --git a/src/module/conf/log.py b/backend/src/module/conf/log.py similarity index 100% rename from src/module/conf/log.py rename to backend/src/module/conf/log.py diff --git a/src/module/conf/parse.py b/backend/src/module/conf/parse.py similarity index 100% rename from src/module/conf/parse.py rename to backend/src/module/conf/parse.py diff --git a/src/module/conf/uvicorn_logging.py b/backend/src/module/conf/uvicorn_logging.py similarity index 100% rename from src/module/conf/uvicorn_logging.py rename to backend/src/module/conf/uvicorn_logging.py diff --git a/src/module/core/__init__.py b/backend/src/module/core/__init__.py similarity index 100% rename from src/module/core/__init__.py rename to backend/src/module/core/__init__.py diff --git a/src/module/core/program.py b/backend/src/module/core/program.py similarity index 100% rename from src/module/core/program.py rename to backend/src/module/core/program.py diff --git a/src/module/core/rss_feed.py b/backend/src/module/core/rss_feed.py similarity index 100% rename from src/module/core/rss_feed.py rename to backend/src/module/core/rss_feed.py diff --git a/src/module/core/status.py b/backend/src/module/core/status.py similarity index 100% rename from src/module/core/status.py rename to backend/src/module/core/status.py diff --git a/src/module/core/sub_thread.py b/backend/src/module/core/sub_thread.py similarity index 100% rename from src/module/core/sub_thread.py rename to backend/src/module/core/sub_thread.py diff --git a/src/module/database/__init__.py b/backend/src/module/database/__init__.py similarity index 100% rename from src/module/database/__init__.py rename to backend/src/module/database/__init__.py diff --git a/src/module/database/bangumi.py b/backend/src/module/database/bangumi.py similarity index 95% rename from src/module/database/bangumi.py rename to backend/src/module/database/bangumi.py index 1c49938b8..0ba1ce92e 100644 --- a/src/module/database/bangumi.py +++ b/backend/src/module/database/bangumi.py @@ -160,9 +160,13 @@ def match_list(self, torrent_list: list, rss_link: str) -> list: if match_data.get("title_raw") in torrent.name: if rss_link not in match_data.get("rss_link"): match_data["rss_link"] += f",{rss_link}" - self.update_rss(match_data.get("title_raw"), match_data.get("rss_link")) + self.update_rss( + match_data.get("title_raw"), match_data.get("rss_link") + ) if not match_data.get("poster_link"): - self.update_poster(match_data.get("title_raw"), torrent.poster_link) + self.update_poster( + match_data.get("title_raw"), torrent.poster_link + ) torrent_list.pop(i) break else: @@ -217,7 +221,7 @@ def __check_list_exist(self, data_list: list[BangumiData]): return False -if __name__ == '__main__': +if __name__ == "__main__": with BangumiDatabase() as db: name = "久保" - print(db.match_poster(name)) \ No newline at end of file + print(db.match_poster(name)) diff --git a/src/module/database/connector.py b/backend/src/module/database/connector.py similarity index 89% rename from src/module/database/connector.py rename to backend/src/module/database/connector.py index 751a63557..d4dbea561 100644 --- a/src/module/database/connector.py +++ b/backend/src/module/database/connector.py @@ -100,10 +100,14 @@ def _delete_all(self, table_name: str): def _delete(self, table_name: str, condition: dict): condition_sql = " AND ".join([f"{key} = :{key}" for key in condition.keys()]) - self._cursor.execute(f"DELETE FROM {table_name} WHERE {condition_sql}", condition) + self._cursor.execute( + f"DELETE FROM {table_name} WHERE {condition_sql}", condition + ) self._conn.commit() - def _search(self, table_name: str, keys: list[str] | None = None, condition: dict = None): + def _search( + self, table_name: str, keys: list[str] | None = None, condition: dict = None + ): if keys is None: select_sql = "*" else: @@ -112,20 +116,25 @@ def _search(self, table_name: str, keys: list[str] | None = None, condition: dic self._cursor.execute(f"SELECT {select_sql} FROM {table_name}") else: custom_condition = condition.pop("_custom_condition", None) - condition_sql = " AND ".join([f"{key} = :{key}" for key in condition.keys()]) + ( - f" AND {custom_condition}" if custom_condition else "" - ) + condition_sql = " AND ".join( + [f"{key} = :{key}" for key in condition.keys()] + ) + (f" AND {custom_condition}" if custom_condition else "") self._cursor.execute( - f"SELECT {select_sql} FROM {table_name} WHERE {condition_sql}", condition + f"SELECT {select_sql} FROM {table_name} WHERE {condition_sql}", + condition, ) - def _search_data(self, table_name: str, keys: list[str] | None = None, condition: dict = None) -> dict: + def _search_data( + self, table_name: str, keys: list[str] | None = None, condition: dict = None + ) -> dict: if keys is None: keys = self.__get_table_columns(table_name) self._search(table_name, keys, condition) return dict(zip(keys, self._cursor.fetchone())) - def _search_datas(self, table_name: str, keys: list[str] | None = None, condition: dict = None) -> list[dict]: + def _search_datas( + self, table_name: str, keys: list[str] | None = None, condition: dict = None + ) -> list[dict]: if keys is None: keys = self.__get_table_columns(table_name) self._search(table_name, keys, condition) diff --git a/src/module/database/torrent.py b/backend/src/module/database/torrent.py similarity index 100% rename from src/module/database/torrent.py rename to backend/src/module/database/torrent.py diff --git a/src/module/database/user.py b/backend/src/module/database/user.py similarity index 100% rename from src/module/database/user.py rename to backend/src/module/database/user.py diff --git a/src/module/downloader/__init__.py b/backend/src/module/downloader/__init__.py similarity index 100% rename from src/module/downloader/__init__.py rename to backend/src/module/downloader/__init__.py diff --git a/src/module/downloader/client/__init__.py b/backend/src/module/downloader/client/__init__.py similarity index 100% rename from src/module/downloader/client/__init__.py rename to backend/src/module/downloader/client/__init__.py diff --git a/src/module/downloader/client/aria2_downloader.py b/backend/src/module/downloader/client/aria2_downloader.py similarity index 100% rename from src/module/downloader/client/aria2_downloader.py rename to backend/src/module/downloader/client/aria2_downloader.py diff --git a/src/module/downloader/client/qb_downloader.py b/backend/src/module/downloader/client/qb_downloader.py similarity index 96% rename from src/module/downloader/client/qb_downloader.py rename to backend/src/module/downloader/client/qb_downloader.py index fc5b622e5..5ddedec29 100644 --- a/src/module/downloader/client/qb_downloader.py +++ b/backend/src/module/downloader/client/qb_downloader.py @@ -78,7 +78,9 @@ def add_category(self, category): @qb_connect_failed_wait def torrents_info(self, status_filter, category, tag=None): - return self._client.torrents_info(status_filter=status_filter, category=category, tag=tag) + return self._client.torrents_info( + status_filter=status_filter, category=category, tag=tag + ) def torrents_add(self, urls, save_path, category, torrent_files=None): resp = self._client.torrents_add( @@ -87,7 +89,7 @@ def torrents_add(self, urls, save_path, category, torrent_files=None): torrent_files=torrent_files, save_path=save_path, category=category, - use_auto_torrent_management=False + use_auto_torrent_management=False, ) return resp == "Ok." diff --git a/src/module/downloader/client/tr_downloader.py b/backend/src/module/downloader/client/tr_downloader.py similarity index 100% rename from src/module/downloader/client/tr_downloader.py rename to backend/src/module/downloader/client/tr_downloader.py diff --git a/src/module/downloader/exceptions.py b/backend/src/module/downloader/exceptions.py similarity index 100% rename from src/module/downloader/exceptions.py rename to backend/src/module/downloader/exceptions.py diff --git a/src/module/downloader/path.py b/backend/src/module/downloader/path.py similarity index 100% rename from src/module/downloader/path.py rename to backend/src/module/downloader/path.py diff --git a/src/module/manager/__init__.py b/backend/src/module/manager/__init__.py similarity index 100% rename from src/module/manager/__init__.py rename to backend/src/module/manager/__init__.py diff --git a/src/module/manager/collector.py b/backend/src/module/manager/collector.py similarity index 88% rename from src/module/manager/collector.py rename to backend/src/module/manager/collector.py index cff45d3f1..86524221d 100644 --- a/src/module/manager/collector.py +++ b/backend/src/module/manager/collector.py @@ -34,8 +34,12 @@ def collect_season(self, data: BangumiData, link: str = None, proxy: bool = Fals torrents = st.get_torrents(link, _filter="|".join(data.filter)) torrent_files = None if proxy: - torrent_files = [st.get_content(torrent.torrent_link) for torrent in torrents] - return self.add_season_torrents(data=data, torrents=torrents, torrent_files=torrent_files) + torrent_files = [ + st.get_content(torrent.torrent_link) for torrent in torrents + ] + return self.add_season_torrents( + data=data, torrents=torrents, torrent_files=torrent_files + ) def subscribe_season(self, data: BangumiData): with BangumiDatabase() as db: @@ -57,5 +61,3 @@ def eps_complete(): sc.collect_season(data) data.eps_collect = True bd.update_list(datas) - - diff --git a/src/module/manager/renamer.py b/backend/src/module/manager/renamer.py similarity index 95% rename from src/module/manager/renamer.py rename to backend/src/module/manager/renamer.py index 6cfc9b8b2..9b04d1459 100644 --- a/src/module/manager/renamer.py +++ b/backend/src/module/manager/renamer.py @@ -38,9 +38,7 @@ def gen_path( elif method == "advance": return f"{bangumi_name} S{season}E{episode}{file_info.suffix}" elif method == "normal": - logger.warning( - "[Renamer] Normal rename method is deprecated." - ) + logger.warning("[Renamer] Normal rename method is deprecated.") return file_info.media_path elif method == "subtitle_pn": return f"{file_info.title} S{season}E{episode}.{file_info.language}{file_info.suffix}" @@ -73,10 +71,10 @@ def rename_file( ) if renamed: n = Notification( - official_title=bangumi_name, - season=ep.season, - episode=ep.episode, - ) + official_title=bangumi_name, + season=ep.season, + episode=ep.episode, + ) return n else: logger.warning(f"[Renamer] {media_path} parse failed") @@ -178,6 +176,7 @@ def rename(self) -> list[Notification]: if __name__ == "__main__": from module.conf import setup_logger + settings.log.debug_enable = True setup_logger() with Renamer() as renamer: diff --git a/src/module/manager/torrent.py b/backend/src/module/manager/torrent.py similarity index 65% rename from src/module/manager/torrent.py rename to backend/src/module/manager/torrent.py index 6d43be6fb..a11bb8998 100644 --- a/src/module/manager/torrent.py +++ b/backend/src/module/manager/torrent.py @@ -14,7 +14,9 @@ class TorrentManager(BangumiDatabase): def __match_torrents_list(data: BangumiData) -> list: with DownloadClient() as client: torrents = client.get_torrent_info(status_filter=None) - return [torrent.hash for torrent in torrents if torrent.save_path == data.save_path] + return [ + torrent.hash for torrent in torrents if torrent.save_path == data.save_path + ] def delete_torrents(self, data: BangumiData, client: DownloadClient): hash_list = self.__match_torrents_list(data) @@ -34,17 +36,21 @@ def delete_rule(self, _id: int | str, file: bool = False): self.delete_one(int(_id)) if file: torrent_message = self.delete_torrents(data, client) - return JSONResponse(status_code=200, content={ - "msg": f"Delete {data.official_title} rule. {torrent_message}" - }) + return JSONResponse( + status_code=200, + content={ + "msg": f"Delete {data.official_title} rule. {torrent_message}" + }, + ) logger.info(f"[Manager] Delete rule for {data.official_title}") - return JSONResponse(status_code=200, content={ - "msg": f"Delete rule for {data.official_title}" - }) + return JSONResponse( + status_code=200, + content={"msg": f"Delete rule for {data.official_title}"}, + ) else: - return JSONResponse(status_code=406, content={ - "msg": f"Can't find id {_id}" - }) + return JSONResponse( + status_code=406, content={"msg": f"Can't find id {_id}"} + ) def disable_rule(self, _id: str | int, file: bool = False): data = self.search_id(int(_id)) @@ -55,17 +61,23 @@ def disable_rule(self, _id: str | int, file: bool = False): self.update_one(data) if file: torrent_message = self.delete_torrents(data, client) - return JSONResponse(status_code=200, content={ - "msg": f"Disable {data.official_title} rule. {torrent_message}" - }) + return JSONResponse( + status_code=200, + content={ + "msg": f"Disable {data.official_title} rule. {torrent_message}" + }, + ) logger.info(f"[Manager] Disable rule for {data.official_title}") - return JSONResponse(status_code=200, content={ - "msg": f"Disable {data.official_title} rule.", - }) + return JSONResponse( + status_code=200, + content={ + "msg": f"Disable {data.official_title} rule.", + }, + ) else: - return JSONResponse(status_code=406, content={ - "msg": f"Can't find id {_id}" - }) + return JSONResponse( + status_code=406, content={"msg": f"Can't find id {_id}"} + ) def enable_rule(self, _id: str | int): data = self.search_id(int(_id)) @@ -75,21 +87,24 @@ def enable_rule(self, _id: str | int): with DownloadClient() as client: client.set_rule(data) logger.info(f"[Manager] Enable rule for {data.official_title}") - return JSONResponse(status_code=200, content={ - "msg": f"Enable {data.official_title} rule.", - }) + return JSONResponse( + status_code=200, + content={ + "msg": f"Enable {data.official_title} rule.", + }, + ) else: - return JSONResponse(status_code=406, content={ - "msg": f"Can't find bangumi id {_id}" - }) + 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 {data.id}") - return JSONResponse(status_code=406, content={ - "msg": f"Can't find data with {data.id}" - }) + return JSONResponse( + status_code=406, content={"msg": f"Can't find data with {data.id}"} + ) else: # Move torrent match_list = self.__match_torrents_list(data) @@ -101,9 +116,12 @@ def update_rule(self, data: BangumiData): client.remove_rule(data.rule_name) client.set_rule(data) self.update_one(data) - return JSONResponse(status_code=200, content={ - "msg": f"Set new path for {data.official_title}", - }) + return JSONResponse( + status_code=200, + content={ + "msg": f"Set new path for {data.official_title}", + }, + ) def search_all_bangumi(self): datas = self.search_all() diff --git a/src/module/models/__init__.py b/backend/src/module/models/__init__.py similarity index 100% rename from src/module/models/__init__.py rename to backend/src/module/models/__init__.py diff --git a/src/module/models/api.py b/backend/src/module/models/api.py similarity index 100% rename from src/module/models/api.py rename to backend/src/module/models/api.py diff --git a/src/module/models/bangumi.py b/backend/src/module/models/bangumi.py similarity index 100% rename from src/module/models/bangumi.py rename to backend/src/module/models/bangumi.py diff --git a/src/module/models/config.py b/backend/src/module/models/config.py similarity index 100% rename from src/module/models/config.py rename to backend/src/module/models/config.py diff --git a/src/module/models/torrent.py b/backend/src/module/models/torrent.py similarity index 100% rename from src/module/models/torrent.py rename to backend/src/module/models/torrent.py diff --git a/src/module/models/user.py b/backend/src/module/models/user.py similarity index 100% rename from src/module/models/user.py rename to backend/src/module/models/user.py diff --git a/src/module/network/__init__.py b/backend/src/module/network/__init__.py similarity index 100% rename from src/module/network/__init__.py rename to backend/src/module/network/__init__.py diff --git a/src/module/network/request_contents.py b/backend/src/module/network/request_contents.py similarity index 93% rename from src/module/network/request_contents.py rename to backend/src/module/network/request_contents.py index 0c2096b6a..77c074dfb 100644 --- a/src/module/network/request_contents.py +++ b/backend/src/module/network/request_contents.py @@ -51,7 +51,11 @@ def get_torrents( torrent_titles, torrent_urls, torrent_homepage ): if re.search(_filter, _title) is None: - torrents.append(TorrentInfo(name=_title, torrent_link=torrent_url, homepage=homepage)) + torrents.append( + TorrentInfo( + name=_title, torrent_link=torrent_url, homepage=homepage + ) + ) return torrents except ConnectionError: return [] diff --git a/src/module/network/request_url.py b/backend/src/module/network/request_url.py similarity index 92% rename from src/module/network/request_url.py rename to backend/src/module/network/request_url.py index 16da3d4cb..f4b65e7d7 100644 --- a/src/module/network/request_url.py +++ b/backend/src/module/network/request_url.py @@ -23,7 +23,9 @@ def get_url(self, url, retry=3): req.raise_for_status() return req except requests.RequestException: - logger.warning(f"[Network] Cannot connect to {url}. Wait for 5 seconds.") + logger.warning( + f"[Network] Cannot connect to {url}. Wait for 5 seconds." + ) try_time += 1 if try_time >= retry: break @@ -45,7 +47,9 @@ def post_url(self, url: str, data: dict, retry=3): req.raise_for_status() return req except requests.RequestException: - logger.warning(f"[Network] Cannot connect to {url}. Wait for 5 seconds.") + logger.warning( + f"[Network] Cannot connect to {url}. Wait for 5 seconds." + ) try_time += 1 if try_time >= retry: break diff --git a/backend/src/module/network/site/__init__.py b/backend/src/module/network/site/__init__.py new file mode 100644 index 000000000..70f16f741 --- /dev/null +++ b/backend/src/module/network/site/__init__.py @@ -0,0 +1 @@ +from .mikan import mikan_parser diff --git a/src/module/network/site/mikan.py b/backend/src/module/network/site/mikan.py similarity index 84% rename from src/module/network/site/mikan.py rename to backend/src/module/network/site/mikan.py index 5a326925d..0ad4314b3 100644 --- a/src/module/network/site/mikan.py +++ b/backend/src/module/network/site/mikan.py @@ -6,4 +6,4 @@ def mikan_parser(soup): torrent_titles.append(item.find("title").text) torrent_urls.append(item.find("enclosure").attrib["url"]) torrent_homepage.append(item.find("link").text) - return torrent_titles, torrent_urls, torrent_homepage \ No newline at end of file + return torrent_titles, torrent_urls, torrent_homepage diff --git a/backend/src/module/notification/__init__.py b/backend/src/module/notification/__init__.py new file mode 100644 index 000000000..e6a713ae9 --- /dev/null +++ b/backend/src/module/notification/__init__.py @@ -0,0 +1 @@ +from .notification import PostNotification diff --git a/src/module/notification/notification.py b/backend/src/module/notification/notification.py similarity index 95% rename from src/module/notification/notification.py rename to backend/src/module/notification/notification.py index a59230b77..a4bd7c726 100644 --- a/src/module/notification/notification.py +++ b/backend/src/module/notification/notification.py @@ -27,8 +27,7 @@ class PostNotification: def __init__(self): Notifier = getClient(settings.notification.type) self.notifier = Notifier( - token=settings.notification.token, - chat_id=settings.notification.chat_id + token=settings.notification.token, chat_id=settings.notification.chat_id ) @staticmethod diff --git a/src/module/notification/plugin/__init__.py b/backend/src/module/notification/plugin/__init__.py similarity index 77% rename from src/module/notification/plugin/__init__.py rename to backend/src/module/notification/plugin/__init__.py index ece03d588..e9acda8f1 100644 --- a/src/module/notification/plugin/__init__.py +++ b/backend/src/module/notification/plugin/__init__.py @@ -1,4 +1,4 @@ from .bark import BarkNotification from .server_chan import ServerChanNotification from .telegram import TelegramNotification -from .wecom import WecomNotification \ No newline at end of file +from .wecom import WecomNotification diff --git a/src/module/notification/plugin/bark.py b/backend/src/module/notification/plugin/bark.py similarity index 100% rename from src/module/notification/plugin/bark.py rename to backend/src/module/notification/plugin/bark.py diff --git a/src/module/notification/plugin/server_chan.py b/backend/src/module/notification/plugin/server_chan.py similarity index 100% rename from src/module/notification/plugin/server_chan.py rename to backend/src/module/notification/plugin/server_chan.py diff --git a/src/module/notification/plugin/slack.py b/backend/src/module/notification/plugin/slack.py similarity index 95% rename from src/module/notification/plugin/slack.py rename to backend/src/module/notification/plugin/slack.py index af4a0d836..f4da8a2df 100644 --- a/src/module/notification/plugin/slack.py +++ b/backend/src/module/notification/plugin/slack.py @@ -24,4 +24,4 @@ def post_msg(self, notify: Notification) -> bool: data = {"title": notify.official_title, "body": text, "device_key": self.token} resp = self.post_data(self.notification_url, data) logger.debug(f"Bark notification: {resp.status_code}") - return resp.status_code == 200 \ No newline at end of file + return resp.status_code == 200 diff --git a/src/module/notification/plugin/telegram.py b/backend/src/module/notification/plugin/telegram.py similarity index 100% rename from src/module/notification/plugin/telegram.py rename to backend/src/module/notification/plugin/telegram.py diff --git a/src/module/notification/plugin/wecom.py b/backend/src/module/notification/plugin/wecom.py similarity index 87% rename from src/module/notification/plugin/wecom.py rename to backend/src/module/notification/plugin/wecom.py index 11a63a751..dbd906c49 100644 --- a/src/module/notification/plugin/wecom.py +++ b/backend/src/module/notification/plugin/wecom.py @@ -11,7 +11,7 @@ class WecomNotification(RequestContent): def __init__(self, token, chat_id, **kwargs): super().__init__() - #Chat_id is used as noti_url in this push tunnel + # Chat_id is used as noti_url in this push tunnel self.notification_url = f"{chat_id}" self.token = token @@ -27,15 +27,15 @@ def post_msg(self, notify: Notification) -> bool: title = "【番剧更新】" + notify.official_title msg = self.gen_message(notify) picurl = notify.poster_path - #Default pic to avoid blank in message. Resolution:1068*455 + # Default pic to avoid blank in message. Resolution:1068*455 if picurl == "https://mikanani.me": picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png" data = { - "key":self.token, + "key": self.token, "type": "news", "title": title, "msg": msg, - "picurl":picurl + "picurl": picurl, } resp = self.post_data(self.notification_url, data) logger.debug(f"Wecom notification: {resp.status_code}") diff --git a/src/module/parser/__init__.py b/backend/src/module/parser/__init__.py similarity index 100% rename from src/module/parser/__init__.py rename to backend/src/module/parser/__init__.py diff --git a/src/module/parser/analyser/__init__.py b/backend/src/module/parser/analyser/__init__.py similarity index 100% rename from src/module/parser/analyser/__init__.py rename to backend/src/module/parser/analyser/__init__.py diff --git a/src/module/parser/analyser/bgm_parser.py b/backend/src/module/parser/analyser/bgm_parser.py similarity index 100% rename from src/module/parser/analyser/bgm_parser.py rename to backend/src/module/parser/analyser/bgm_parser.py diff --git a/src/module/parser/analyser/tmdb_parser.py b/backend/src/module/parser/analyser/tmdb_parser.py similarity index 69% rename from src/module/parser/analyser/tmdb_parser.py rename to backend/src/module/parser/analyser/tmdb_parser.py index b3d206c3d..7762c95a8 100644 --- a/src/module/parser/analyser/tmdb_parser.py +++ b/backend/src/module/parser/analyser/tmdb_parser.py @@ -16,16 +16,14 @@ class TMDBInfo: year: str -LANGUAGE = { - "zh": "zh-CN", - "jp": "ja-JP", - "en": "en-US" -} +LANGUAGE = {"zh": "zh-CN", "jp": "ja-JP", "en": "en-US"} -search_url = lambda e: \ - f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API}&page=1&query={e}&include_adult=false" -info_url = lambda e, key: \ - f"https://api.themoviedb.org/3/tv/{e}?api_key={TMDB_API}&language={LANGUAGE[key]}" +search_url = ( + lambda e: f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API}&page=1&query={e}&include_adult=false" +) +info_url = ( + lambda e, key: f"https://api.themoviedb.org/3/tv/{e}?api_key={TMDB_API}&language={LANGUAGE[key]}" +) def is_animation(tv_id, language) -> bool: @@ -43,7 +41,7 @@ def get_season(seasons: list) -> int: for season in ss: if re.search(r"第 \d 季", season.get("season")) is not None: date = season.get("air_date").split("-") - [year, _ , _] = date + [year, _, _] = date now_year = time.localtime().tm_year if int(year) <= now_year: return int(re.findall(r"\d", season.get("season"))[0]) @@ -64,16 +62,30 @@ def tmdb_parser(title, language) -> TMDBInfo | None: break url_info = info_url(id, language) info_content = req.get_json(url_info) - season = [{"season": s.get("name"), "air_date": s.get("air_date"), "poster_path": s.get("poster_path")} for s in info_content.get("seasons")] + season = [ + { + "season": s.get("name"), + "air_date": s.get("air_date"), + "poster_path": s.get("poster_path"), + } + for s in info_content.get("seasons") + ] last_season = get_season(season) original_title = info_content.get("original_name") official_title = info_content.get("name") year_number = info_content.get("first_air_date").split("-")[0] - return TMDBInfo(id, official_title, original_title, season, last_season, str(year_number)) + return TMDBInfo( + id, + official_title, + original_title, + season, + last_season, + str(year_number), + ) else: return None -if __name__ == '__main__': +if __name__ == "__main__": title = "海盗战记" print(tmdb_parser(title, "zh").last_season) diff --git a/src/module/parser/analyser/torrent_parser.py b/backend/src/module/parser/analyser/torrent_parser.py similarity index 100% rename from src/module/parser/analyser/torrent_parser.py rename to backend/src/module/parser/analyser/torrent_parser.py diff --git a/src/module/parser/openai.py b/backend/src/module/parser/openai.py similarity index 100% rename from src/module/parser/openai.py rename to backend/src/module/parser/openai.py diff --git a/src/module/parser/title_parser.py b/backend/src/module/parser/title_parser.py similarity index 100% rename from src/module/parser/title_parser.py rename to backend/src/module/parser/title_parser.py diff --git a/src/module/rss/__init__.py b/backend/src/module/rss/__init__.py similarity index 100% rename from src/module/rss/__init__.py rename to backend/src/module/rss/__init__.py diff --git a/src/module/rss/analyser.py b/backend/src/module/rss/analyser.py similarity index 93% rename from src/module/rss/analyser.py rename to backend/src/module/rss/analyser.py index 9ae35b377..7cc520835 100644 --- a/src/module/rss/analyser.py +++ b/backend/src/module/rss/analyser.py @@ -61,7 +61,9 @@ def torrents_to_data( logger.debug(f"[RSS] New title found: {data.official_title}") return new_data - def torrent_to_data(self, torrent: TorrentInfo, rss_link: str | None = None) -> BangumiData: + def torrent_to_data( + self, torrent: TorrentInfo, rss_link: str | None = None + ) -> BangumiData: data = self._title_analyser.raw_parser(raw=torrent.name, rss_link=rss_link) if data: try: @@ -75,7 +77,9 @@ def torrent_to_data(self, torrent: TorrentInfo, rss_link: str | None = None) -> self.official_title_parser(data, mikan_title) return data - def rss_to_data(self, rss_link: str, database: BangumiDatabase, full_parse: bool = True) -> list[BangumiData]: + def rss_to_data( + self, rss_link: str, database: BangumiDatabase, full_parse: bool = True + ) -> list[BangumiData]: rss_torrents = self.get_rss_torrents(rss_link, full_parse) torrents_to_add = database.match_list(rss_torrents, rss_link) if not torrents_to_add: diff --git a/src/module/rss/filter.py b/backend/src/module/rss/filter.py similarity index 100% rename from src/module/rss/filter.py rename to backend/src/module/rss/filter.py diff --git a/src/module/rss/searcher.py b/backend/src/module/rss/searcher.py similarity index 100% rename from src/module/rss/searcher.py rename to backend/src/module/rss/searcher.py diff --git a/src/module/searcher/__init__.py b/backend/src/module/searcher/__init__.py similarity index 100% rename from src/module/searcher/__init__.py rename to backend/src/module/searcher/__init__.py diff --git a/src/module/searcher/plugin/__init__.py b/backend/src/module/searcher/plugin/__init__.py similarity index 100% rename from src/module/searcher/plugin/__init__.py rename to backend/src/module/searcher/plugin/__init__.py diff --git a/src/module/searcher/plugin/mikan.py b/backend/src/module/searcher/plugin/mikan.py similarity index 100% rename from src/module/searcher/plugin/mikan.py rename to backend/src/module/searcher/plugin/mikan.py diff --git a/src/module/searcher/searcher.py b/backend/src/module/searcher/searcher.py similarity index 87% rename from src/module/searcher/searcher.py rename to backend/src/module/searcher/searcher.py index 621022eb0..ad46f6641 100644 --- a/src/module/searcher/searcher.py +++ b/backend/src/module/searcher/searcher.py @@ -13,7 +13,9 @@ class SearchTorrent(RequestContent): - def search_torrents(self, keywords: list[str], site: str = "mikan") -> list[TorrentBase]: + def search_torrents( + self, keywords: list[str], site: str = "mikan" + ) -> list[TorrentBase]: url = search_url(site, keywords) # TorrentInfo to TorrentBase torrents = self.get_torrents(url) @@ -25,6 +27,7 @@ def to_dict(): "torrent_link": torrent.torrent_link, "homepage": torrent.homepage, } + return [TorrentBase(**d) for d in to_dict()] def search_season(self, data: BangumiData): @@ -33,7 +36,7 @@ def search_season(self, data: BangumiData): return [torrent for torrent in torrents if data.title_raw in torrent.name] -if __name__ == '__main__': +if __name__ == "__main__": with SearchTorrent() as st: for t in st.search_torrents(["魔法科高校の劣等生"]): - print(t) \ No newline at end of file + print(t) diff --git a/src/module/security/__init__.py b/backend/src/module/security/__init__.py similarity index 100% rename from src/module/security/__init__.py rename to backend/src/module/security/__init__.py diff --git a/src/module/security/api.py b/backend/src/module/security/api.py similarity index 100% rename from src/module/security/api.py rename to backend/src/module/security/api.py diff --git a/src/module/security/jwt.py b/backend/src/module/security/jwt.py similarity index 100% rename from src/module/security/jwt.py rename to backend/src/module/security/jwt.py diff --git a/src/module/update/__init__.py b/backend/src/module/update/__init__.py similarity index 100% rename from src/module/update/__init__.py rename to backend/src/module/update/__init__.py diff --git a/src/module/update/data_migration.py b/backend/src/module/update/data_migration.py similarity index 100% rename from src/module/update/data_migration.py rename to backend/src/module/update/data_migration.py diff --git a/src/module/utils/__init__.py b/backend/src/module/utils/__init__.py similarity index 100% rename from src/module/utils/__init__.py rename to backend/src/module/utils/__init__.py diff --git a/src/module/utils/bangumi_data.py b/backend/src/module/utils/bangumi_data.py similarity index 100% rename from src/module/utils/bangumi_data.py rename to backend/src/module/utils/bangumi_data.py diff --git a/src/module/utils/json_config.py b/backend/src/module/utils/json_config.py similarity index 100% rename from src/module/utils/json_config.py rename to backend/src/module/utils/json_config.py diff --git a/src/test/__init__.py b/backend/src/test/__init__.py similarity index 100% rename from src/test/__init__.py rename to backend/src/test/__init__.py diff --git a/src/test/test_raw_parser.py b/backend/src/test/test_raw_parser.py similarity index 99% rename from src/test/test_raw_parser.py rename to backend/src/test/test_raw_parser.py index f09f1aae6..b4c3b446a 100644 --- a/src/test/test_raw_parser.py +++ b/backend/src/test/test_raw_parser.py @@ -69,4 +69,3 @@ def test_raw_parser(): assert info.resolution == "720P" assert info.episode == 5 assert info.season == 1 - diff --git a/src/test/test_torrent_parser.py b/backend/src/test/test_torrent_parser.py similarity index 100% rename from src/test/test_torrent_parser.py rename to backend/src/test/test_torrent_parser.py diff --git a/docs/wiki b/docs/wiki index d0bb98f00..519e381e8 160000 --- a/docs/wiki +++ b/docs/wiki @@ -1 +1 @@ -Subproject commit d0bb98f004fb292519dd56c42238ecb2f034eac9 +Subproject commit 519e381e8a1add62e76a39181ee61bad02816035 diff --git a/pyrightconfig.json b/pyrightconfig.json new file mode 100644 index 000000000..4521b494f --- /dev/null +++ b/pyrightconfig.json @@ -0,0 +1,8 @@ +{ + "pythonPath": "/opt/homebrew/Caskroom/miniforge/base/envs/auto_bangumi/bin/python", + "root": "backend/src", + "venvPath": "/opt/homebrew/Caskroom/miniforge/base/envs", + "venv": "auto_bangumi", + "typeCheckingMode": "basic", + "reportMissingImports": true +} diff --git a/src/module/network/site/__init__.py b/src/module/network/site/__init__.py deleted file mode 100644 index 04b8a6152..000000000 --- a/src/module/network/site/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .mikan import mikan_parser \ No newline at end of file diff --git a/src/module/notification/__init__.py b/src/module/notification/__init__.py deleted file mode 100644 index dbd1ac934..000000000 --- a/src/module/notification/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .notification import PostNotification \ No newline at end of file