Explorar el Código

[enh] currency converter engine added

asciimoo hace 11 años
padre
commit
cfff04f7d0
Se han modificado 1 ficheros con 45 adiciones y 0 borrados
  1. 45
    0
      searx/engines/currency_convert.py

+ 45
- 0
searx/engines/currency_convert.py Ver fichero

@@ -0,0 +1,45 @@
1
+
2
+categories = []
3
+url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
4
+
5
+def request(query, params):
6
+    try:
7
+        # eg.: "X EUR in USD"
8
+        ammount, from_currency, _, to_currency = query.split()
9
+        ammount = float(ammount)
10
+    except:
11
+        # wrong params
12
+        return params
13
+
14
+    q = (from_currency+to_currency).upper()
15
+    if not q.isalpha():
16
+        return params
17
+
18
+    params['url'] = url.format(query=q)
19
+    params['ammount'] = ammount
20
+    params['from'] = from_currency
21
+    params['to'] = to_currency
22
+
23
+    return params
24
+
25
+
26
+def response(resp):
27
+    global base_url
28
+    results = []
29
+    try:
30
+        _,conversion_rate,_ = resp.text.split(',', 2)
31
+        conversion_rate = float(conversion_rate)
32
+    except:
33
+        return results
34
+
35
+    title = '{0} {1} in {2} is {3}'.format(resp.search_params['ammount']
36
+                                          ,resp.search_params['from']
37
+                                          ,resp.search_params['to']
38
+                                          ,resp.search_params['ammount']*conversion_rate
39
+                                          )
40
+
41
+    content = '1 {0} is {1} {2}'.format(resp.search_params['from'], conversion_rate, resp.search_params['to'])
42
+    url = 'http://finance.yahoo.com/currency/converter-results/20131104/{0}-{1}-to-{2}.html'.format(resp.search_params['ammount'], resp.search_params['from'].lower(), resp.search_params['to'].lower())
43
+    results.append({'title': title, 'content': content, 'url': url})
44
+
45
+    return results