From 04ed5257c751c512d77a5725254065445daf1956 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 4 May 2016 12:00:55 -0500 Subject: [PATCH] Refactor search engines. --- earwigbot/wiki/copyvios/__init__.py | 12 +++--------- earwigbot/wiki/copyvios/search.py | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/earwigbot/wiki/copyvios/__init__.py b/earwigbot/wiki/copyvios/__init__.py index b129941..d63f384 100644 --- a/earwigbot/wiki/copyvios/__init__.py +++ b/earwigbot/wiki/copyvios/__init__.py @@ -26,8 +26,7 @@ from urllib2 import build_opener from earwigbot import exceptions from earwigbot.wiki.copyvios.markov import MarkovChain from earwigbot.wiki.copyvios.parsers import ArticleTextParser -from earwigbot.wiki.copyvios.search import ( - BingSearchEngine, YahooBOSSSearchEngine) +from earwigbot.wiki.copyvios.search import SEARCH_ENGINES from earwigbot.wiki.copyvios.workers import ( globalize, localize, CopyvioWorkspace) @@ -61,16 +60,11 @@ class CopyvioMixIn(object): unknown to us, and UnsupportedSearchEngineError if we are missing a required package or module, like oauth2 for "Yahoo! BOSS". """ - engines = { - "Bing": BingSearchEngine, - "Yahoo! BOSS": YahooBOSSSearchEngine - } - engine = self._search_config["engine"] - if engine not in engines: + if engine not in SEARCH_ENGINES: raise exceptions.UnknownSearchEngineError(engine) - klass = engines[engine] + klass = SEARCH_ENGINES[engine] credentials = self._search_config["credentials"] opener = build_opener() opener.addheaders = self._addheaders diff --git a/earwigbot/wiki/copyvios/search.py b/earwigbot/wiki/copyvios/search.py index 8f16c66..139a8eb 100644 --- a/earwigbot/wiki/copyvios/search.py +++ b/earwigbot/wiki/copyvios/search.py @@ -32,9 +32,9 @@ from earwigbot.exceptions import SearchQueryError oauth = importer.new("oauth2") -__all__ = ["BaseSearchEngine", "BingSearchEngine", "YahooBOSSSearchEngine"] +__all__ = ["BingSearchEngine", "YahooBOSSSearchEngine", "SEARCH_ENGINES"] -class BaseSearchEngine(object): +class _BaseSearchEngine(object): """Base class for a simple search engine interface.""" name = "Base" @@ -64,7 +64,7 @@ class BaseSearchEngine(object): raise NotImplementedError() -class BingSearchEngine(BaseSearchEngine): +class BingSearchEngine(_BaseSearchEngine): """A search engine interface with Bing Search (via Azure Marketplace).""" name = "Bing" @@ -78,8 +78,7 @@ class BingSearchEngine(BaseSearchEngine): def search(self, query): """Do a Bing web search for *query*. - Returns a list of URLs, no more than five, ranked by relevance - (as determined by Bing). + Returns a list of URLs ranked by relevance (as determined by Bing). Raises :py:exc:`~earwigbot.exceptions.SearchQueryError` on errors. """ service = "SearchWeb" if self.cred["type"] == "searchweb" else "Search" @@ -121,7 +120,7 @@ class BingSearchEngine(BaseSearchEngine): return [result["Url"] for result in results] -class YahooBOSSSearchEngine(BaseSearchEngine): +class YahooBOSSSearchEngine(_BaseSearchEngine): """A search engine interface with Yahoo! BOSS.""" name = "Yahoo! BOSS" @@ -139,8 +138,7 @@ class YahooBOSSSearchEngine(BaseSearchEngine): def search(self, query): """Do a Yahoo! BOSS web search for *query*. - Returns a list of URLs, no more than five, ranked by relevance - (as determined by Yahoo). + Returns a list of URLs ranked by relevance (as determined by Yahoo). Raises :py:exc:`~earwigbot.exceptions.SearchQueryError` on errors. """ key, secret = self.cred["key"], self.cred["secret"] @@ -183,3 +181,9 @@ class YahooBOSSSearchEngine(BaseSearchEngine): except KeyError: return [] return [result["url"] for result in results] + + +SEARCH_ENGINES = { + "Bing": BingSearchEngine, + "Yahoo! BOSS": YahooBOSSSearchEngine +}