.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
radio_browser.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Search radio stations from RadioBrowser by `Advanced station search API`_.
3
4.. _Advanced station search API:
5 https://de1.api.radio-browser.info/#Advanced_station_search
6
7"""
8
9from urllib.parse import urlencode
10import babel
11from flask_babel import gettext
12
13from searx.network import get
14from searx.enginelib.traits import EngineTraits
15from searx.locales import language_tag
16
17traits: EngineTraits
18
19about = {
20 "website": 'https://www.radio-browser.info/',
21 "wikidata_id": 'Q111664849',
22 "official_api_documentation": 'https://de1.api.radio-browser.info/',
23 "use_official_api": True,
24 "require_api_key": False,
25 "results": 'JSON',
26}
27paging = True
28categories = ['music', 'radio']
29
30base_url = "https://de1.api.radio-browser.info" # see https://api.radio-browser.info/ for all nodes
31number_of_results = 10
32
33station_filters = [] # ['countrycode', 'language']
34"""A list of filters to be applied to the search of radio stations. By default
35none filters are applied. Valid filters are:
36
37``language``
38 Filter stations by selected language. For instance the ``de`` from ``:de-AU``
39 will be translated to `german` and used in the argument ``language=``.
40
41``countrycode``
42 Filter stations by selected country. The 2-digit countrycode of the station
43 comes from the region the user selected. For instance ``:de-AU`` will filter
44 out all stations not in ``AU``.
45
46.. note::
47
48 RadioBrowser has registered a lot of languages and countrycodes unknown to
49 :py:obj:`babel` and note that when searching for radio stations, users are
50 more likely to search by name than by region or language.
51
52"""
53
54
55def request(query, params):
56 args = {
57 'name': query,
58 'order': 'votes',
59 'offset': (params['pageno'] - 1) * number_of_results,
60 'limit': number_of_results,
61 'hidebroken': 'true',
62 'reverse': 'true',
63 }
64
65 if 'language' in station_filters:
66 lang = traits.get_language(params['searxng_locale']) # type: ignore
67 if lang:
68 args['language'] = lang
69
70 if 'countrycode' in station_filters:
71 if len(params['searxng_locale'].split('-')) > 1:
72 countrycode = params['searxng_locale'].split('-')[-1].upper()
73 if countrycode in traits.custom['countrycodes']: # type: ignore
74 args['countrycode'] = countrycode
75
76 params['url'] = f"{base_url}/json/stations/search?{urlencode(args)}"
77 return params
78
79
80def response(resp):
81 results = []
82
83 json_resp = resp.json()
84
85 for result in json_resp:
86 url = result['homepage']
87 if not url:
88 url = result['url_resolved']
89
90 content = []
91 tags = ', '.join(result.get('tags', '').split(','))
92 if tags:
93 content.append(tags)
94 for x in ['state', 'country']:
95 v = result.get(x)
96 if v:
97 v = str(v).strip()
98 content.append(v)
99
100 metadata = []
101 codec = result.get('codec')
102 if codec and codec.lower() != 'unknown':
103 metadata.append(f'{codec} ' + gettext('radio'))
104 for x, y in [
105 (gettext('bitrate'), 'bitrate'),
106 (gettext('votes'), 'votes'),
107 (gettext('clicks'), 'clickcount'),
108 ]:
109 v = result.get(y)
110 if v:
111 v = str(v).strip()
112 metadata.append(f"{x} {v}")
113 results.append(
114 {
115 'url': url,
116 'title': result['name'],
117 'thumbnail': result.get('favicon', '').replace("http://", "https://"),
118 'content': ' | '.join(content),
119 'metadata': ' | '.join(metadata),
120 'iframe_src': result['url_resolved'].replace("http://", "https://"),
121 }
122 )
123
124 return results
125
126
127def fetch_traits(engine_traits: EngineTraits):
128 """Fetch languages and countrycodes from RadioBrowser
129
130 - ``traits.languages``: `list of languages API`_
131 - ``traits.custom['countrycodes']``: `list of countries API`_
132
133 .. _list of countries API: https://de1.api.radio-browser.info/#List_of_countries
134 .. _list of languages API: https://de1.api.radio-browser.info/#List_of_languages
135 """
136 # pylint: disable=import-outside-toplevel
137
138 from babel.core import get_global
139
140 babel_reg_list = get_global("territory_languages").keys()
141
142 language_list = get(f'{base_url}/json/languages').json() # type: ignore
143 country_list = get(f'{base_url}/json/countries').json() # type: ignore
144
145 for lang in language_list:
146
147 babel_lang = lang.get('iso_639')
148 if not babel_lang:
149 # the language doesn't have any iso code, and hence can't be parsed
150 # print(f"ERROR: lang - no iso code in {lang}")
151 continue
152 try:
153 sxng_tag = language_tag(babel.Locale.parse(babel_lang, sep="-"))
154 except babel.UnknownLocaleError:
155 # print(f"ERROR: language tag {babel_lang} is unknown by babel")
156 continue
157
158 eng_tag = lang['name']
159 conflict = engine_traits.languages.get(sxng_tag)
160 if conflict:
161 if conflict != eng_tag:
162 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
163 continue
164 engine_traits.languages[sxng_tag] = eng_tag
165
166 countrycodes = set()
167 for region in country_list:
168 # country_list contains duplicates that differ only in upper/lower case
169 _reg = region['iso_3166_1'].upper()
170 if _reg not in babel_reg_list:
171 print(f"ERROR: region tag {region['iso_3166_1']} is unknown by babel")
172 continue
173 countrycodes.add(_reg)
174
175 countrycodes = list(countrycodes)
176 countrycodes.sort()
177 engine_traits.custom['countrycodes'] = countrycodes
fetch_traits(EngineTraits engine_traits)