.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
4import typing as t
5import json
6from searx.result_types import EngineResults
7
8if t.TYPE_CHECKING:
9 from searx.search.processors import OnlineCurrenciesParams
10 from searx.extended_types import SXNG_Response
11
12# about
13about = {
14 "website": "https://duckduckgo.com/",
15 "wikidata_id": "Q12805",
16 "official_api_documentation": "https://duckduckgo.com/api",
17 "use_official_api": False,
18 "require_api_key": False,
19 "results": "JSONP",
20 "description": "Service from DuckDuckGo.",
21}
22
23engine_type = "online_currency"
24categories = ["currency", "general"]
25
26base_url = "https://duckduckgo.com/js/spice/currency/1/%(from_iso4217)s/%(to_iso4217)s"
27ddg_link_url = "https://duckduckgo.com/?q=%(from_iso4217)s+to+%(to_iso4217)s"
28
29weight = 100
30
31
32def request(query: str, params: "OnlineCurrenciesParams") -> None: # pylint: disable=unused-argument
33 params["url"] = base_url % params
34
35
36def response(resp: "SXNG_Response") -> EngineResults:
37 res = EngineResults()
38
39 # remove first and last lines to get only json
40 json_resp = resp.text[resp.text.find("\n") + 1 : resp.text.rfind("\n") - 2]
41 try:
42 conversion_rate = float(json.loads(json_resp)["to"][0]["mid"])
43 except IndexError:
44 return res
45
46 params: OnlineCurrenciesParams = resp.search_params # pyright: ignore[reportAssignmentType]
47 answer = "{0} {1} = {2} {3} (1 {5} : {4} {6})".format(
48 params["amount"],
49 params["from_iso4217"],
50 params["amount"] * conversion_rate,
51 params["to_iso4217"],
52 conversion_rate,
53 params["from_name"],
54 params["to_name"],
55 )
56 url = ddg_link_url % params
57 res.add(res.types.Answer(answer=answer, url=url))
58 return res
None request(str query, "OnlineCurrenciesParams" params)
EngineResults response("SXNG_Response" resp)