31def response(resp):
32
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}]