.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
currency_convert.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Currency convert (DuckDuckGo)
3"""
4
5import json
6
7# about
8about = {
9 "website": 'https://duckduckgo.com/',
10 "wikidata_id": 'Q12805',
11 "official_api_documentation": 'https://duckduckgo.com/api',
12 "use_official_api": False,
13 "require_api_key": False,
14 "results": 'JSONP',
15 "description": "Service from DuckDuckGo.",
16}
17
18engine_type = 'online_currency'
19categories = []
20base_url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'
21weight = 100
22
23https_support = True
24
25
26def request(_query, params):
27 params['url'] = base_url.format(params['from'], params['to'])
28 return params
29
30
31def response(resp):
32 # remove first and last lines to get only json
33 json_resp = resp.text[resp.text.find('\n') + 1 : resp.text.rfind('\n') - 2]
34 try:
35 conversion_rate = float(json.loads(json_resp)["to"][0]["mid"])
36 except IndexError:
37 return []
38 answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
39 resp.search_params['amount'],
40 resp.search_params['from'],
41 resp.search_params['amount'] * conversion_rate,
42 resp.search_params['to'],
43 conversion_rate,
44 resp.search_params['from_name'],
45 resp.search_params['to_name'],
46 )
47
48 url = f"https://duckduckgo.com/?q={resp.search_params['from']}+to+{resp.search_params['to']}"
49
50 return [{"answer": answer, "url": url}]