From 5416f0f248e1c8072c69b4a272af07bd4c0d8e5e Mon Sep 17 00:00:00 2001 From: potato Date: Tue, 6 Sep 2016 12:50:56 +0200 Subject: [enh] dictionary engine renamed to dictzone --- searx/engines/dictionary.py | 73 --------------------------------------------- searx/engines/dictzone.py | 73 +++++++++++++++++++++++++++++++++++++++++++++ searx/settings.yml | 4 +-- 3 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 searx/engines/dictionary.py create mode 100644 searx/engines/dictzone.py diff --git a/searx/engines/dictionary.py b/searx/engines/dictionary.py deleted file mode 100644 index b255463aa..000000000 --- a/searx/engines/dictionary.py +++ /dev/null @@ -1,73 +0,0 @@ -import re -from urlparse import urljoin -from lxml import html -from searx.engines.xpath import extract_text -from searx.languages import language_codes - -categories = ['general'] -url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}' -weight = 100 - -parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.+)', re.I) -results_xpath = './/table[@id="r"]/tr' - - -def request(query, params): - m = parser_re.match(unicode(query, 'utf8')) - if not m: - return params - - from_lang, to_lang, query = m.groups() - - if len(from_lang) == 2: - lan = filter(lambda x: x[0][:2] == from_lang, language_codes) - if lan: - from_lang = lan[0][1].lower() - else: - return params - elif from_lang.lower() not in [x[1].lower() for x in language_codes]: - return params - - - if len(to_lang) == 2: - lan = filter(lambda x: x[0][:2] == to_lang, language_codes) - if lan: - to_lang = lan[0][1].lower() - else: - return params - elif to_lang.lower() not in [x[1].lower() for x in language_codes]: - return params - - params['url'] = url.format(from_lang=from_lang, to_lang=to_lang,query=query) - params['from_lang'] = from_lang - params['to_lang'] = to_lang - params['query'] = query - - return params - -def response(resp): - results = [] - - dom = html.fromstring(resp.text) - - for k, result in enumerate(dom.xpath(results_xpath)[1:]): - try: - from_result, to_results_raw = result.xpath('./td') - except: - continue - - to_results = [] - for to_result in to_results_raw.xpath('./p/a'): - t = to_result.text_content() - if t.strip(): - to_results.append(to_result.text_content()) - - results.append({ - 'url': urljoin(resp.url, '?%d' % k), - 'title': from_result.text_content(), - 'content': '; '.join(to_results) - }) - - return results - - diff --git a/searx/engines/dictzone.py b/searx/engines/dictzone.py new file mode 100644 index 000000000..b255463aa --- /dev/null +++ b/searx/engines/dictzone.py @@ -0,0 +1,73 @@ +import re +from urlparse import urljoin +from lxml import html +from searx.engines.xpath import extract_text +from searx.languages import language_codes + +categories = ['general'] +url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}' +weight = 100 + +parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.+)', re.I) +results_xpath = './/table[@id="r"]/tr' + + +def request(query, params): + m = parser_re.match(unicode(query, 'utf8')) + if not m: + return params + + from_lang, to_lang, query = m.groups() + + if len(from_lang) == 2: + lan = filter(lambda x: x[0][:2] == from_lang, language_codes) + if lan: + from_lang = lan[0][1].lower() + else: + return params + elif from_lang.lower() not in [x[1].lower() for x in language_codes]: + return params + + + if len(to_lang) == 2: + lan = filter(lambda x: x[0][:2] == to_lang, language_codes) + if lan: + to_lang = lan[0][1].lower() + else: + return params + elif to_lang.lower() not in [x[1].lower() for x in language_codes]: + return params + + params['url'] = url.format(from_lang=from_lang, to_lang=to_lang,query=query) + params['from_lang'] = from_lang + params['to_lang'] = to_lang + params['query'] = query + + return params + +def response(resp): + results = [] + + dom = html.fromstring(resp.text) + + for k, result in enumerate(dom.xpath(results_xpath)[1:]): + try: + from_result, to_results_raw = result.xpath('./td') + except: + continue + + to_results = [] + for to_result in to_results_raw.xpath('./p/a'): + t = to_result.text_content() + if t.strip(): + to_results.append(to_result.text_content()) + + results.append({ + 'url': urljoin(resp.url, '?%d' % k), + 'title': from_result.text_content(), + 'content': '; '.join(to_results) + }) + + return results + + diff --git a/searx/settings.yml b/searx/settings.yml index 8c552eb02..ce768ce81 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -495,8 +495,8 @@ engines: timeout: 6.0 categories : science - - name : dictionary - engine : dictionary + - name : dictzone + engine : dictzone shortcut : dc #The blekko technology and team have joined IBM Watson! -> https://blekko.com/ -- cgit v1.2.3