diff --git a/main.py b/main.py index 3fd220c..b7cd002 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ from plugin.engine.minebbs import Minebbs from plugin.engine.klpbbs import Klpbbs from plugin.engine.spigotmc import Spigot -from plugin.utils.translate import translate, EN +from plugin.engine.tinksp import Tinksp +from concurrent.futures import ThreadPoolExecutor, wait def print_result(result): @@ -18,20 +19,13 @@ def print_result(result): if keyword == "exit": break - engine = Spigot() - - for result in engine.search(keyword): - print_result(result) - - engine = Minebbs() - - for result in engine.search(keyword): - print_result(result) - - engine = Klpbbs() - - for result in engine.search(keyword): - print_result(result) - - - + engine = [Spigot, Tinksp, Minebbs, Klpbbs, ] + with ThreadPoolExecutor(max_workers=6) as executor: + future_list = [] + for e in engine: + future = executor.submit(e().search, keyword) + future_list.append(future) + wait(future_list) + for future in future_list: + for result in future.result(): + print_result(result) diff --git a/plugin/engine/bing.py b/plugin/engine/bing.py index 9a9a3aa..4758f3d 100644 --- a/plugin/engine/bing.py +++ b/plugin/engine/bing.py @@ -8,9 +8,7 @@ from plugin.utils.translate import translate, EN -class Bing(SearchEngine): - def search(self, keywords) -> typing.List[SearchResult]: - pass +class Bing: def get_bing_url(self, keywords): keywords = keywords.strip('\n') @@ -18,8 +16,8 @@ def get_bing_url(self, keywords): bing_url = re.sub(r'\s', '+', bing_url) return bing_url - def _search(self, keywords, site="minebbs.com") -> typing.List[SearchResult]: - keywords = f'"{translate(keywords)}" ”插件“ site:{site}' + def search(self, keywords, site) -> typing.List[SearchResult]: + keywords = f'"{translate(keywords)}" 插件 site:{site}' url = self.get_bing_url(keywords) result: typing.List[SearchResult] = [] headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0', @@ -37,13 +35,17 @@ def _search(self, keywords, site="minebbs.com") -> typing.List[SearchResult]: li_list = tree.xpath('//ol[@id="b_results"]//li[@class="b_algo"]') for li in li_list: try: - h3: str = li.xpath('./h2/a')[0].xpath('string(.)') + h3 = li.xpath('./h2/a')[0] p = li.xpath('.//p')[0].xpath('string(.)') - url = li.xpath('./div[1]/a/div[2]/div[2]/div/cite')[0].xpath('string(.)') + try: + url = li.xpath('./div[1]/a/div[2]/div[2]/div/cite')[0].xpath('string(.)') + except IndexError: + url = h3.get('href') + h3 = h3.xpath('string(.)') if h3.find("模组") != -1 or h3.find("地图") != -1 or h3.find("插件") == -1: continue result.append(SearchResult(url=url, title=h3, summary=p)) - except Exception as pri: - print(pri) + except IndexError: + pass return result diff --git a/plugin/engine/klpbbs.py b/plugin/engine/klpbbs.py index 9300dde..f0f9566 100644 --- a/plugin/engine/klpbbs.py +++ b/plugin/engine/klpbbs.py @@ -4,7 +4,7 @@ from plugin.engine.bing import Bing -class Klpbbs(Bing): +class Klpbbs: - def search(self, keywords) -> typing.List[SearchResult]: - return super()._search(keywords, site="klpbbs.com")[:3] # 一般来将,前五个才有价值 # 更加fw,3个 \ No newline at end of file + def search(self, keywords,engine=Bing()) -> typing.List[SearchResult]: + return engine.search(keywords, site="klpbbs.com")[:3] # 一般来将,前五个才有价值 # 更加fw,3个 \ No newline at end of file diff --git a/plugin/engine/minebbs.py b/plugin/engine/minebbs.py index 5b59821..b2375cc 100644 --- a/plugin/engine/minebbs.py +++ b/plugin/engine/minebbs.py @@ -4,7 +4,7 @@ from plugin.engine.bing import Bing -class Minebbs(Bing): +class Minebbs(): - def search(self, keywords) -> typing.List[SearchResult]: - return super()._search(keywords, site="minebbs.com")[:4] # 一般来将,前五个才有价值 # 四个吧 + def search(self, keywords, engine=Bing()) -> typing.List[SearchResult]: + return engine.search(keywords, site="minebbs.com")[:4] # 一般来将,前五个才有价值 # 四个吧 diff --git a/plugin/engine/tinksp.py b/plugin/engine/tinksp.py new file mode 100644 index 0000000..e9f799c --- /dev/null +++ b/plugin/engine/tinksp.py @@ -0,0 +1,10 @@ +import typing + +from plugin.engine.base import SearchResult +from plugin.engine.bing import Bing + + +class Tinksp: + + def search(self, keywords, engine=Bing()) -> typing.List[SearchResult]: + return engine.search(keywords, site="tinksp.com")[:5]