Browse Source

Merge pull request #1079 from pyrrh0n1c/master

Fixed the currency_convert engine.
Adam Tauber 7 years ago
parent
commit
d00aaeedfa
No account linked to committer's email
2 changed files with 21 additions and 23 deletions
  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 View File

10
     unicode = str
10
     unicode = str
11
 
11
 
12
 categories = []
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
 weight = 100
14
 weight = 100
15
 
15
 
16
 parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
16
 parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
44
         # wrong query
44
         # wrong query
45
         return params
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
     from_currency = name_to_iso4217(from_currency.strip())
49
     from_currency = name_to_iso4217(from_currency.strip())
50
     to_currency = name_to_iso4217(to_currency.strip())
50
     to_currency = name_to_iso4217(to_currency.strip())
51
 
51
 
52
     q = (from_currency + to_currency).upper()
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
     params['from'] = from_currency
56
     params['from'] = from_currency
57
     params['to'] = to_currency
57
     params['to'] = to_currency
58
     params['from_name'] = iso4217_to_name(from_currency, 'en')
58
     params['from_name'] = iso4217_to_name(from_currency, 'en')
63
 
63
 
64
 def response(resp):
64
 def response(resp):
65
     results = []
65
     results = []
66
+    pat = '<span class=bld>(.+) {0}</span>'.format(
67
+        resp.search_params['to'].upper())
68
+
66
     try:
69
     try:
67
-        _, conversion_rate, _ = resp.text.split(',', 2)
70
+        conversion_rate = re.findall(pat, resp.text)[0]
68
         conversion_rate = float(conversion_rate)
71
         conversion_rate = float(conversion_rate)
69
     except:
72
     except:
70
         return results
73
         return results
71
 
74
 
72
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
75
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
73
-        resp.search_params['ammount'],
76
+        resp.search_params['amount'],
74
         resp.search_params['from'],
77
         resp.search_params['from'],
75
-        resp.search_params['ammount'] * conversion_rate,
78
+        resp.search_params['amount'] * conversion_rate,
76
         resp.search_params['to'],
79
         resp.search_params['to'],
77
         conversion_rate,
80
         conversion_rate,
78
         resp.search_params['from_name'],
81
         resp.search_params['from_name'],
79
         resp.search_params['to_name'],
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
     results.append({'answer': answer, 'url': url})
88
     results.append({'answer': answer, 'url': url})
92
 
89
 

+ 8
- 7
tests/unit/engines/test_currency_convert.py View File

17
         query = b'convert 10 Pound Sterlings to United States Dollars'
17
         query = b'convert 10 Pound Sterlings to United States Dollars'
18
         params = currency_convert.request(query, dicto)
18
         params = currency_convert.request(query, dicto)
19
         self.assertIn('url', params)
19
         self.assertIn('url', params)
20
-        self.assertIn('finance.yahoo.com', params['url'])
20
+        self.assertIn('finance.google.com', params['url'])
21
         self.assertIn('GBP', params['url'])
21
         self.assertIn('GBP', params['url'])
22
         self.assertIn('USD', params['url'])
22
         self.assertIn('USD', params['url'])
23
 
23
 
24
     def test_response(self):
24
     def test_response(self):
25
         dicto = defaultdict(dict)
25
         dicto = defaultdict(dict)
26
-        dicto['ammount'] = float(10)
26
+        dicto['amount'] = float(10)
27
         dicto['from'] = "GBP"
27
         dicto['from'] = "GBP"
28
         dicto['to'] = "USD"
28
         dicto['to'] = "USD"
29
         dicto['from_name'] = "pound sterling"
29
         dicto['from_name'] = "pound sterling"
31
         response = mock.Mock(text='a,b,c,d', search_params=dicto)
31
         response = mock.Mock(text='a,b,c,d', search_params=dicto)
32
         self.assertEqual(currency_convert.response(response), [])
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
         results = currency_convert.response(response)
36
         results = currency_convert.response(response)
37
         self.assertEqual(type(results), list)
37
         self.assertEqual(type(results), list)
38
         self.assertEqual(len(results), 1)
38
         self.assertEqual(len(results), 1)
39
         self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
39
         self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
40
                          ' = 0.5 USD (United States dollar)')
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)