on master help sign in
[enh] dictionary engine added
commitf4525880ed5f965ac4c241638933842a27a2acf7 authorpotato <mail@crazypotato.tk> dateTue 6 Sep 2016, 11:47 (10 years ago) parent465d472
2 files changed+74
searx/engines/dictionary.py+70 searx/settings.yml+4
searx/engines/dictionary.pynew file+70
@@ -0,0 +1,70 @@
1+import re
2+from lxml import html
3+from searx.engines.xpath import extract_text
4+from searx.languages import language_codes
5+
6+categories = []
7+url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
8+weight = 100
9+
10+parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.+)', re.I)
11+results_xpath = './/table[@id="r"]/tr'
12+
13+
14+def request(query, params):
15+ m = parser_re.match(unicode(query, 'utf8'))
16+ if not m:
17+ return params
18+
19+ from_lang, to_lang, query = m.groups()
20+
21+ if len(from_lang) == 2:
22+ lan = filter(lambda x: x[0][:2] == from_lang, language_codes)
23+ if lan:
24+ from_lang = lan[0][1].lower()
25+ else:
26+ return params
27+
28+ if len(to_lang) == 2:
29+ lan = filter(lambda x: x[0][:2] == to_lang, language_codes)
30+ if lan:
31+ to_lang = lan[0][1].lower()
32+ else:
33+ return params
34+
35+ params['url'] = url.format(from_lang=from_lang, to_lang=to_lang,query=query)
36+ params['from_lang'] = from_lang
37+ params['to_lang'] = to_lang
38+ params['query'] = query
39+
40+ return params
41+
42+def response(resp):
43+ results = []
44+ answers = []
45+
46+ dom = html.fromstring(resp.text)
47+
48+ for result in dom.xpath(results_xpath)[1:]:
49+ try:
50+ from_result, to_results_raw = result.xpath('./td')
51+ except:
52+ continue
53+
54+ to_results = []
55+ for to_result in to_results_raw.xpath('./p/a'):
56+ t = to_result.text_content()
57+ if t.strip():
58+ to_results.append(to_result.text_content())
59+
60+ results.append({
61+ 'answer': u'{0} - {1}'.format(
62+ from_result.text_content(),
63+ '; '.join(to_results)
64+ ),
65+ 'url': url
66+ })
67+
68+ return results
69+
70+
searx/settings.yml+4
@@ -495,6 +495,10 @@ engines:
495495 timeout: 6.0
496496 categories : science
497497
498+ - name : dictionary
499+ engine : dictionary
500+ shortcut : dc
501+
498502#The blekko technology and team have joined IBM Watson! -> https://blekko.com/
499503# - name : blekko images
500504# engine : blekko_images