Pārlūkot izejas kodu

[enh] dictionary engine added

potato 8 gadus atpakaļ
vecāks
revīzija
f4525880ed
2 mainītis faili ar 74 papildinājumiem un 0 dzēšanām
  1. 70
    0
      searx/engines/dictionary.py
  2. 4
    0
      searx/settings.yml

+ 70
- 0
searx/engines/dictionary.py Parādīt failu

@@ -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
+

+ 4
- 0
searx/settings.yml Parādīt failu

@@ -495,6 +495,10 @@ engines:
495 495
     timeout: 6.0
496 496
     categories : science
497 497
 
498
+  - name : dictionary
499
+    engine : dictionary
500
+    shortcut : dc
501
+
498 502
 #The blekko technology and team have joined IBM Watson! -> https://blekko.com/
499 503
 #  - name : blekko images
500 504
 #    engine : blekko_images