瀏覽代碼

Refactor search engines.

tags/v0.3
Ben Kurtovic 8 年之前
父節點
當前提交
04ed5257c7
共有 2 個文件被更改,包括 15 次插入17 次删除
  1. +3
    -9
      earwigbot/wiki/copyvios/__init__.py
  2. +12
    -8
      earwigbot/wiki/copyvios/search.py

+ 3
- 9
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


+ 12
- 8
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
}

Loading…
取消
儲存