From d447b7a0641707db6360bcb6bd8144a2b498f3b4 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Tue, 15 Aug 2023 20:18:16 +0800 Subject: [PATCH] feat: optimize searcher, fix some type bug. --- .../src/module/searcher/plugin/__init__.py | 14 ------------- backend/src/module/searcher/plugin/dmhy.py | 8 ------- backend/src/module/searcher/plugin/mikan.py | 8 ------- backend/src/module/searcher/plugin/nyaa.py | 8 ------- backend/src/module/searcher/provider.py | 21 +++++++++++++++++++ backend/src/module/searcher/searcher.py | 19 ++++++++--------- 6 files changed, 30 insertions(+), 48 deletions(-) delete mode 100644 backend/src/module/searcher/plugin/__init__.py delete mode 100644 backend/src/module/searcher/plugin/dmhy.py delete mode 100644 backend/src/module/searcher/plugin/mikan.py delete mode 100644 backend/src/module/searcher/plugin/nyaa.py create mode 100644 backend/src/module/searcher/provider.py diff --git a/backend/src/module/searcher/plugin/__init__.py b/backend/src/module/searcher/plugin/__init__.py deleted file mode 100644 index 49ca7df0..00000000 --- a/backend/src/module/searcher/plugin/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -from .mikan import mikan_url -from .nyaa import nyaa_url -from .dmhy import dmhy_url - - -def search_url(site: str, keywords: list[str]): - if site == "mikan": - return mikan_url(keywords) - elif site == "nyaa": - return nyaa_url(keywords) - elif site == "dmhy": - return dmhy_url(keywords) - else: - raise NotImplementedError(f"site {site} is not supported") diff --git a/backend/src/module/searcher/plugin/dmhy.py b/backend/src/module/searcher/plugin/dmhy.py deleted file mode 100644 index c8323feb..00000000 --- a/backend/src/module/searcher/plugin/dmhy.py +++ /dev/null @@ -1,8 +0,0 @@ -import re - - -def dmhy_url(keywords: list[str]): - keyword = "+".join(keywords) - search_str = re.sub(r"[\W_ ]", "+", keyword) - url = f"http://dmhy.org/topics/rss/rss.xml?keyword={search_str}" - return url diff --git a/backend/src/module/searcher/plugin/mikan.py b/backend/src/module/searcher/plugin/mikan.py deleted file mode 100644 index 0c2b7b3a..00000000 --- a/backend/src/module/searcher/plugin/mikan.py +++ /dev/null @@ -1,8 +0,0 @@ -import re - - -def mikan_url(keywords: list[str]): - keyword = "+".join(keywords) - search_str = re.sub(r"[\W_ ]", "+", keyword) - url = f"https://mikanani.me/RSS/Search?searchstr={search_str}" - return url diff --git a/backend/src/module/searcher/plugin/nyaa.py b/backend/src/module/searcher/plugin/nyaa.py deleted file mode 100644 index 83686192..00000000 --- a/backend/src/module/searcher/plugin/nyaa.py +++ /dev/null @@ -1,8 +0,0 @@ -import re - - -def nyaa_url(keywords: list[str]): - keyword = "+".join(keywords) - search_str = re.sub(r"[\W_ ]", "+", keyword) - url = f"https://nyaa.si/?page=rss&q={search_str}&c=0_0&f=0" - return url diff --git a/backend/src/module/searcher/provider.py b/backend/src/module/searcher/provider.py new file mode 100644 index 00000000..af749db3 --- /dev/null +++ b/backend/src/module/searcher/provider.py @@ -0,0 +1,21 @@ +import re + +from module.utils import json_config +from module.models import RSSItem + + +SEARCH_CONFIG = json_config.load("config/search_provider.json") + + +def search_url(site: str, keywords: list[str]) -> RSSItem: + keyword = "+".join(keywords) + search_str = re.sub(r"[\W_ ]", "+", keyword) + if site in SEARCH_CONFIG.keys(): + url = re.sub(r"%s", search_str, SEARCH_CONFIG[site].get("url")) + rss_item = RSSItem( + url=url, + parser="mikan" + ) + return rss_item + else: + raise ValueError(f"Site {site} is not supported") \ No newline at end of file diff --git a/backend/src/module/searcher/searcher.py b/backend/src/module/searcher/searcher.py index 4a4c388e..363e97f4 100644 --- a/backend/src/module/searcher/searcher.py +++ b/backend/src/module/searcher/searcher.py @@ -1,10 +1,10 @@ import json -from module.models import Bangumi, Torrent +from module.models import Bangumi, Torrent, RSSItem from module.network import RequestContent from module.rss import RSSAnalyser -from module.searcher.plugin import search_url +from .provider import search_url SEARCH_KEY = [ "group_name", @@ -18,29 +18,28 @@ SEARCH_KEY = [ class SearchTorrent(RequestContent, RSSAnalyser): def search_torrents( - self, keywords: list[str], site: str = "mikan", limit: int = 5 + self, rss_item: RSSItem, limit: int = 5 ) -> list[Torrent]: - url = search_url(site, keywords) - torrents = self.get_torrents(url, limit=limit) + torrents = self.get_torrents(rss_item.url, limit=limit) return torrents def analyse_keyword(self, keywords: list[str], site: str = "mikan"): - bangumis = [] - torrents = self.search_torrents(keywords, site) + rss_item = search_url(site, keywords) + torrents = self.search_torrents(rss_item) # Generate a list of json yield "[" for idx, torrent in enumerate(torrents): - bangumi = self.torrent_to_data(torrent) + bangumi = self.torrent_to_data(torrent=torrent, rss=rss_item) if bangumi: yield json.dumps(bangumi.dict()) if idx != len(torrents) - 1: yield "," yield "]" - # Analyse bangumis def search_season(self, data: Bangumi): keywords = [getattr(data, key) for key in SEARCH_KEY if getattr(data, key)] - torrents = self.search_torrents(keywords) + url = search_url("mikan", keywords) + torrents = self.search_torrents(url) return [torrent for torrent in torrents if data.title_raw in torrent.name]