logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git
commit: 983415bc38937a637e9b2aae191f2e087765800b
parent b7d578ae8041658fe6f088eb337f42238c25e2f5
Author: potato <mail@crazypotato.tk>
Date:   Tue,  6 Sep 2016 16:43:48 +0200

[enh] is_valid_lang moved to utils

Diffstat:

Msearx/engines/dictzone.py20+++-----------------
Msearx/engines/translated.py16+---------------
Msearx/utils.py15+++++++++++++++
3 files changed, 19 insertions(+), 32 deletions(-)

diff --git 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 @@ -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 @@ -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