diff --git a/searx/engines/dictzone.py b/searx/engines/dictzone.py index 2c2ec3ab..5de6c5b9 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 1b75e4f4..3a077ae8 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 744142e3..b3806d3f 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