2016-09-06 14:36:04 +00:00
|
|
|
"""
|
|
|
|
MyMemory Translated
|
|
|
|
|
|
|
|
@website https://mymemory.translated.net/
|
|
|
|
@provide-api yes (https://mymemory.translated.net/doc/spec.php)
|
|
|
|
@using-api yes
|
|
|
|
@results JSON
|
|
|
|
@stable yes
|
|
|
|
@parse url, title, content
|
|
|
|
"""
|
2016-09-06 13:40:07 +00:00
|
|
|
import re
|
2016-09-06 14:43:48 +00:00
|
|
|
from searx.utils import is_valid_lang
|
2016-09-06 13:40:07 +00:00
|
|
|
|
|
|
|
categories = ['general']
|
2020-07-12 22:43:24 +00:00
|
|
|
url = u'https://api.mymemory.translated.net/get?q={query}&langpair={from_lang}|{to_lang}{key}'
|
|
|
|
web_url = u'https://mymemory.translated.net/en/{from_lang}/{to_lang}/{query}'
|
2016-09-06 13:40:07 +00:00
|
|
|
weight = 100
|
|
|
|
|
2020-07-12 22:43:24 +00:00
|
|
|
parser_re = re.compile(b'.*?([a-z]+)-([a-z]+) (.{2,})$', re.I)
|
2016-09-06 14:12:34 +00:00
|
|
|
api_key = ''
|
2016-09-06 13:40:07 +00:00
|
|
|
|
2016-09-06 13:44:05 +00:00
|
|
|
|
2016-09-06 13:40:07 +00:00
|
|
|
def request(query, params):
|
2020-07-12 22:43:24 +00:00
|
|
|
m = parser_re.match(query)
|
2016-09-06 13:40:07 +00:00
|
|
|
if not m:
|
|
|
|
return params
|
|
|
|
|
|
|
|
from_lang, to_lang, query = m.groups()
|
|
|
|
|
|
|
|
from_lang = is_valid_lang(from_lang)
|
|
|
|
to_lang = is_valid_lang(to_lang)
|
|
|
|
|
|
|
|
if not from_lang or not to_lang:
|
|
|
|
return params
|
|
|
|
|
2016-09-06 14:12:34 +00:00
|
|
|
if api_key:
|
|
|
|
key_form = '&key=' + api_key
|
|
|
|
else:
|
|
|
|
key_form = ''
|
2016-09-06 13:40:07 +00:00
|
|
|
params['url'] = url.format(from_lang=from_lang[1],
|
|
|
|
to_lang=to_lang[1],
|
2020-07-12 22:43:24 +00:00
|
|
|
query=query.decode('utf-8'),
|
2016-09-06 14:12:34 +00:00
|
|
|
key=key_form)
|
2020-07-12 22:43:24 +00:00
|
|
|
params['query'] = query.decode('utf-8')
|
2016-09-06 13:40:07 +00:00
|
|
|
params['from_lang'] = from_lang
|
|
|
|
params['to_lang'] = to_lang
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
results.append({
|
2016-12-09 10:44:24 +00:00
|
|
|
'url': web_url.format(
|
2016-09-06 13:44:05 +00:00
|
|
|
from_lang=resp.search_params['from_lang'][2],
|
|
|
|
to_lang=resp.search_params['to_lang'][2],
|
2016-12-09 10:44:24 +00:00
|
|
|
query=resp.search_params['query']),
|
|
|
|
'title': '[{0}-{1}] {2}'.format(
|
2016-09-06 13:44:05 +00:00
|
|
|
resp.search_params['from_lang'][1],
|
|
|
|
resp.search_params['to_lang'][1],
|
2016-12-09 10:44:24 +00:00
|
|
|
resp.search_params['query']),
|
|
|
|
'content': resp.json()['responseData']['translatedText']
|
2016-09-06 13:40:07 +00:00
|
|
|
})
|
|
|
|
return results
|