Browse Source

Refactor search engines.

tags/v0.3
Ben Kurtovic 8 years ago
parent
commit
04ed5257c7
2 changed files with 15 additions and 17 deletions
  1. +3
    -9
      earwigbot/wiki/copyvios/__init__.py
  2. +12
    -8
      earwigbot/wiki/copyvios/search.py

+ 3
- 9
earwigbot/wiki/copyvios/__init__.py View File

@@ -26,8 +26,7 @@ from urllib2 import build_opener
from earwigbot import exceptions from earwigbot import exceptions
from earwigbot.wiki.copyvios.markov import MarkovChain from earwigbot.wiki.copyvios.markov import MarkovChain
from earwigbot.wiki.copyvios.parsers import ArticleTextParser 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 ( from earwigbot.wiki.copyvios.workers import (
globalize, localize, CopyvioWorkspace) globalize, localize, CopyvioWorkspace)


@@ -61,16 +60,11 @@ class CopyvioMixIn(object):
unknown to us, and UnsupportedSearchEngineError if we are missing a unknown to us, and UnsupportedSearchEngineError if we are missing a
required package or module, like oauth2 for "Yahoo! BOSS". required package or module, like oauth2 for "Yahoo! BOSS".
""" """
engines = {
"Bing": BingSearchEngine,
"Yahoo! BOSS": YahooBOSSSearchEngine
}

engine = self._search_config["engine"] engine = self._search_config["engine"]
if engine not in engines:
if engine not in SEARCH_ENGINES:
raise exceptions.UnknownSearchEngineError(engine) raise exceptions.UnknownSearchEngineError(engine)


klass = engines[engine]
klass = SEARCH_ENGINES[engine]
credentials = self._search_config["credentials"] credentials = self._search_config["credentials"]
opener = build_opener() opener = build_opener()
opener.addheaders = self._addheaders opener.addheaders = self._addheaders


+ 12
- 8
earwigbot/wiki/copyvios/search.py View File

@@ -32,9 +32,9 @@ from earwigbot.exceptions import SearchQueryError


oauth = importer.new("oauth2") 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.""" """Base class for a simple search engine interface."""
name = "Base" name = "Base"


@@ -64,7 +64,7 @@ class BaseSearchEngine(object):
raise NotImplementedError() raise NotImplementedError()




class BingSearchEngine(BaseSearchEngine):
class BingSearchEngine(_BaseSearchEngine):
"""A search engine interface with Bing Search (via Azure Marketplace).""" """A search engine interface with Bing Search (via Azure Marketplace)."""
name = "Bing" name = "Bing"


@@ -78,8 +78,7 @@ class BingSearchEngine(BaseSearchEngine):
def search(self, query): def search(self, query):
"""Do a Bing web search for *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. Raises :py:exc:`~earwigbot.exceptions.SearchQueryError` on errors.
""" """
service = "SearchWeb" if self.cred["type"] == "searchweb" else "Search" service = "SearchWeb" if self.cred["type"] == "searchweb" else "Search"
@@ -121,7 +120,7 @@ class BingSearchEngine(BaseSearchEngine):
return [result["Url"] for result in results] return [result["Url"] for result in results]




class YahooBOSSSearchEngine(BaseSearchEngine):
class YahooBOSSSearchEngine(_BaseSearchEngine):
"""A search engine interface with Yahoo! BOSS.""" """A search engine interface with Yahoo! BOSS."""
name = "Yahoo! BOSS" name = "Yahoo! BOSS"


@@ -139,8 +138,7 @@ class YahooBOSSSearchEngine(BaseSearchEngine):
def search(self, query): def search(self, query):
"""Do a Yahoo! BOSS web search for *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. Raises :py:exc:`~earwigbot.exceptions.SearchQueryError` on errors.
""" """
key, secret = self.cred["key"], self.cred["secret"] key, secret = self.cred["key"], self.cred["secret"]
@@ -183,3 +181,9 @@ class YahooBOSSSearchEngine(BaseSearchEngine):
except KeyError: except KeyError:
return [] return []
return [result["url"] for result in results] return [result["url"] for result in results]


SEARCH_ENGINES = {
"Bing": BingSearchEngine,
"Yahoo! BOSS": YahooBOSSSearchEngine
}

Loading…
Cancel
Save