[enh] is_valid_lang moved to utils

This commit is contained in:
potato 2016-09-06 16:43:48 +02:00
parent b7d578ae80
commit 983415bc38
3 changed files with 19 additions and 32 deletions

View File

@ -14,7 +14,7 @@ from urlparse import urljoin
from lxml import html from lxml import html
from cgi import escape from cgi import escape
from searx.engines.xpath import extract_text from searx.engines.xpath import extract_text
from searx.languages import language_codes from searx.utils import is_valid_lang
categories = ['general'] categories = ['general']
url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}' 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' 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): def request(query, params):
m = parser_re.match(unicode(query, 'utf8')) m = parser_re.match(unicode(query, 'utf8'))
if not m: if not m:
@ -51,8 +37,8 @@ def request(query, params):
if not from_lang or not to_lang: if not from_lang or not to_lang:
return params return params
params['url'] = url.format(from_lang=from_lang[1], params['url'] = url.format(from_lang=from_lang[2],
to_lang=to_lang[1], to_lang=to_lang[2],
query=query) query=query)
return params return params

View File

@ -13,7 +13,7 @@ from urlparse import urljoin
from lxml import html from lxml import html
from cgi import escape from cgi import escape
from searx.engines.xpath import extract_text from searx.engines.xpath import extract_text
from searx.languages import language_codes from searx.utils import is_valid_lang
categories = ['general'] categories = ['general']
url = 'http://api.mymemory.translated.net/get?q={query}' \ 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 = '' 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): def request(query, params):
m = parser_re.match(unicode(query, 'utf8')) m = parser_re.match(unicode(query, 'utf8'))
if not m: if not m:

View File

@ -9,6 +9,7 @@ from HTMLParser import HTMLParser
from random import choice from random import choice
from searx.version import VERSION_STRING from searx.version import VERSION_STRING
from searx.languages import language_codes
from searx import settings from searx import settings
from searx import logger from searx import logger
@ -255,3 +256,17 @@ def get_torrent_size(filesize, filesize_multiplier):
filesize = None filesize = None
return filesize 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