From 983415bc38937a637e9b2aae191f2e087765800b Mon Sep 17 00:00:00 2001 From: potato Date: Tue, 6 Sep 2016 16:43:48 +0200 Subject: [PATCH] [enh] is_valid_lang moved to utils --- searx/engines/dictzone.py | 20 +++----------------- searx/engines/translated.py | 16 +--------------- searx/utils.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/searx/engines/dictzone.py b/searx/engines/dictzone.py index 2c2ec3abc..5de6c5b98 100644 --- a/searx/engines/dictzone.py +++ b/searx/engines/dictzone.py @@ -14,7 +14,7 @@ from urlparse import urljoin from lxml import html from cgi import escape from searx.engines.xpath import extract_text -from searx.languages import language_codes +from searx.utils import is_valid_lang categories = ['general'] url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}' @@ -24,20 +24,6 @@ parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I) results_xpath = './/table[@id="r"]/tr' -def is_valid_lang(lang): - is_abbr = (len(lang) == 2) - if is_abbr: - for l in language_codes: - if l[0][:2] == lang.lower(): - return (True, l[1].lower()) - return False - else: - for l in language_codes: - if l[1].lower() == lang.lower(): - return (True, l[1].lower()) - return False - - def request(query, params): m = parser_re.match(unicode(query, 'utf8')) if not m: @@ -51,8 +37,8 @@ def request(query, params): if not from_lang or not to_lang: return params - params['url'] = url.format(from_lang=from_lang[1], - to_lang=to_lang[1], + params['url'] = url.format(from_lang=from_lang[2], + to_lang=to_lang[2], query=query) return params diff --git a/searx/engines/translated.py b/searx/engines/translated.py index 1b75e4f4e..3a077ae8e 100644 --- a/searx/engines/translated.py +++ b/searx/engines/translated.py @@ -13,7 +13,7 @@ from urlparse import urljoin from lxml import html from cgi import escape from searx.engines.xpath import extract_text -from searx.languages import language_codes +from searx.utils import is_valid_lang categories = ['general'] url = 'http://api.mymemory.translated.net/get?q={query}' \ @@ -25,20 +25,6 @@ parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.{2,})$', re.I) api_key = '' -def is_valid_lang(lang): - is_abbr = (len(lang) == 2) - if is_abbr: - for l in language_codes: - if l[0][:2] == lang.lower(): - return (True, l[0][:2], l[1].lower()) - return False - else: - for l in language_codes: - if l[1].lower() == lang.lower(): - return (True, l[0][:2], l[1].lower()) - return False - - def request(query, params): m = parser_re.match(unicode(query, 'utf8')) if not m: diff --git a/searx/utils.py b/searx/utils.py index 744142e36..b3806d3fd 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -9,6 +9,7 @@ from HTMLParser import HTMLParser from random import choice from searx.version import VERSION_STRING +from searx.languages import language_codes from searx import settings from searx import logger @@ -255,3 +256,17 @@ def get_torrent_size(filesize, filesize_multiplier): filesize = None return filesize + + +def is_valid_lang(lang): + is_abbr = (len(lang) == 2) + if is_abbr: + for l in language_codes: + if l[0][:2] == lang.lower(): + return (True, l[0][:2], l[1].lower()) + return False + else: + for l in language_codes: + if l[1].lower() == lang.lower(): + return (True, l[0][:2], l[1].lower()) + return False