|
@@ -0,0 +1,69 @@
|
|
1
|
+"""
|
|
2
|
+ MyMemory Translated
|
|
3
|
+
|
|
4
|
+ @website https://mymemory.translated.net/
|
|
5
|
+ @provide-api yes (https://mymemory.translated.net/doc/spec.php)
|
|
6
|
+ @using-api yes
|
|
7
|
+ @results JSON
|
|
8
|
+ @stable yes
|
|
9
|
+ @parse url, title, content
|
|
10
|
+"""
|
|
11
|
+import re
|
|
12
|
+from urlparse import urljoin
|
|
13
|
+from lxml import html
|
|
14
|
+from cgi import escape
|
|
15
|
+from searx.engines.xpath import extract_text
|
|
16
|
+from searx.utils import is_valid_lang
|
|
17
|
+
|
|
18
|
+categories = ['general']
|
|
19
|
+url = 'http://api.mymemory.translated.net/get?q={query}' \
|
|
20
|
+ '&langpair={from_lang}|{to_lang}{key}'
|
|
21
|
+web_url = 'http://mymemory.translated.net/en/{from_lang}/{to_lang}/{query}'
|
|
22
|
+weight = 100
|
|
23
|
+
|
|
24
|
+parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.{2,})$', re.I)
|
|
25
|
+api_key = ''
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+def request(query, params):
|
|
29
|
+ m = parser_re.match(unicode(query, 'utf8'))
|
|
30
|
+ if not m:
|
|
31
|
+ return params
|
|
32
|
+
|
|
33
|
+ from_lang, to_lang, query = m.groups()
|
|
34
|
+
|
|
35
|
+ from_lang = is_valid_lang(from_lang)
|
|
36
|
+ to_lang = is_valid_lang(to_lang)
|
|
37
|
+
|
|
38
|
+ if not from_lang or not to_lang:
|
|
39
|
+ return params
|
|
40
|
+
|
|
41
|
+ if api_key:
|
|
42
|
+ key_form = '&key=' + api_key
|
|
43
|
+ else:
|
|
44
|
+ key_form = ''
|
|
45
|
+ params['url'] = url.format(from_lang=from_lang[1],
|
|
46
|
+ to_lang=to_lang[1],
|
|
47
|
+ query=query,
|
|
48
|
+ key=key_form)
|
|
49
|
+ params['query'] = query
|
|
50
|
+ params['from_lang'] = from_lang
|
|
51
|
+ params['to_lang'] = to_lang
|
|
52
|
+
|
|
53
|
+ return params
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+def response(resp):
|
|
57
|
+ results = []
|
|
58
|
+ results.append({
|
|
59
|
+ 'url': escape(web_url.format(
|
|
60
|
+ from_lang=resp.search_params['from_lang'][2],
|
|
61
|
+ to_lang=resp.search_params['to_lang'][2],
|
|
62
|
+ query=resp.search_params['query'])),
|
|
63
|
+ 'title': escape('[{0}-{1}] {2}'.format(
|
|
64
|
+ resp.search_params['from_lang'][1],
|
|
65
|
+ resp.search_params['to_lang'][1],
|
|
66
|
+ resp.search_params['query'])),
|
|
67
|
+ 'content': escape(resp.json()['responseData']['translatedText'])
|
|
68
|
+ })
|
|
69
|
+ return results
|