Преглед на файлове

Merge pull request #1079 from pyrrh0n1c/master

Fixed the currency_convert engine.
Adam Tauber преди 7 години
родител
ревизия
d00aaeedfa
No account linked to committer's email
променени са 2 файла, в които са добавени 21 реда и са изтрити 23 реда
  1. 13
    16
      searx/engines/currency_convert.py
  2. 8
    7
      tests/unit/engines/test_currency_convert.py

+ 13
- 16
searx/engines/currency_convert.py Целия файл

@@ -10,7 +10,7 @@ if sys.version_info[0] == 3:
10 10
     unicode = str
11 11
 
12 12
 categories = []
13
-url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
13
+url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
14 14
 weight = 100
15 15
 
16 16
 parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
@@ -44,15 +44,15 @@ def request(query, params):
44 44
         # wrong query
45 45
         return params
46 46
 
47
-    ammount, from_currency, to_currency = m.groups()
48
-    ammount = float(ammount)
47
+    amount, from_currency, to_currency = m.groups()
48
+    amount = float(amount)
49 49
     from_currency = name_to_iso4217(from_currency.strip())
50 50
     to_currency = name_to_iso4217(to_currency.strip())
51 51
 
52 52
     q = (from_currency + to_currency).upper()
53 53
 
54
-    params['url'] = url.format(query=q)
55
-    params['ammount'] = ammount
54
+    params['url'] = url.format(from_currency, to_currency)
55
+    params['amount'] = amount
56 56
     params['from'] = from_currency
57 57
     params['to'] = to_currency
58 58
     params['from_name'] = iso4217_to_name(from_currency, 'en')
@@ -63,30 +63,27 @@ def request(query, params):
63 63
 
64 64
 def response(resp):
65 65
     results = []
66
+    pat = '<span class=bld>(.+) {0}</span>'.format(
67
+        resp.search_params['to'].upper())
68
+
66 69
     try:
67
-        _, conversion_rate, _ = resp.text.split(',', 2)
70
+        conversion_rate = re.findall(pat, resp.text)[0]
68 71
         conversion_rate = float(conversion_rate)
69 72
     except:
70 73
         return results
71 74
 
72 75
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
73
-        resp.search_params['ammount'],
76
+        resp.search_params['amount'],
74 77
         resp.search_params['from'],
75
-        resp.search_params['ammount'] * conversion_rate,
78
+        resp.search_params['amount'] * conversion_rate,
76 79
         resp.search_params['to'],
77 80
         conversion_rate,
78 81
         resp.search_params['from_name'],
79 82
         resp.search_params['to_name'],
80 83
     )
81 84
 
82
-    now_date = datetime.now().strftime('%Y%m%d')
83
-    url = 'https://finance.yahoo.com/currency/converter-results/{0}/{1}-{2}-to-{3}.html'  # noqa
84
-    url = url.format(
85
-        now_date,
86
-        resp.search_params['ammount'],
87
-        resp.search_params['from'].lower(),
88
-        resp.search_params['to'].lower()
89
-    )
85
+    url = 'https://finance.google.com/finance?q={0}{1}'.format(
86
+        resp.search_params['from'].upper(), resp.search_params['to'])
90 87
 
91 88
     results.append({'answer': answer, 'url': url})
92 89
 

+ 8
- 7
tests/unit/engines/test_currency_convert.py Целия файл

@@ -17,13 +17,13 @@ class TestCurrencyConvertEngine(SearxTestCase):
17 17
         query = b'convert 10 Pound Sterlings to United States Dollars'
18 18
         params = currency_convert.request(query, dicto)
19 19
         self.assertIn('url', params)
20
-        self.assertIn('finance.yahoo.com', params['url'])
20
+        self.assertIn('finance.google.com', params['url'])
21 21
         self.assertIn('GBP', params['url'])
22 22
         self.assertIn('USD', params['url'])
23 23
 
24 24
     def test_response(self):
25 25
         dicto = defaultdict(dict)
26
-        dicto['ammount'] = float(10)
26
+        dicto['amount'] = float(10)
27 27
         dicto['from'] = "GBP"
28 28
         dicto['to'] = "USD"
29 29
         dicto['from_name'] = "pound sterling"
@@ -31,13 +31,14 @@ class TestCurrencyConvertEngine(SearxTestCase):
31 31
         response = mock.Mock(text='a,b,c,d', search_params=dicto)
32 32
         self.assertEqual(currency_convert.response(response), [])
33 33
 
34
-        csv = "2,0.5,1"
35
-        response = mock.Mock(text=csv, search_params=dicto)
34
+        body = "<span class=bld>0.5 {}</span>".format(dicto['to'])
35
+        response = mock.Mock(text=body, search_params=dicto)
36 36
         results = currency_convert.response(response)
37 37
         self.assertEqual(type(results), list)
38 38
         self.assertEqual(len(results), 1)
39 39
         self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
40 40
                          ' = 0.5 USD (United States dollar)')
41
-        now_date = datetime.now().strftime('%Y%m%d')
42
-        self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
43
-                                            now_date + '/10.0-gbp-to-usd.html')
41
+
42
+        target_url = 'https://finance.google.com/finance?q={}{}'.format(
43
+            dicto['from'], dicto['to'])
44
+        self.assertEqual(results[0]['url'], target_url)