.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.radio_browser Namespace Reference

Functions

 request (query, params)
 
 response (resp)
 
 fetch_traits (EngineTraits engine_traits)
 

Variables

dict about
 
bool paging = True
 
list categories = ['music', 'radio']
 
str base_url = "https://de1.api.radio-browser.info"
 
int number_of_results = 10
 
list station_filters = []
 

Detailed Description

Search radio stations from RadioBrowser by `Advanced station search API`_.

.. _Advanced station search API:
   https://de1.api.radio-browser.info/#Advanced_station_search

Function Documentation

◆ fetch_traits()

searx.engines.radio_browser.fetch_traits ( EngineTraits engine_traits)
Fetch languages and countrycodes from RadioBrowser

- ``traits.languages``: `list of languages API`_
- ``traits.custom['countrycodes']``: `list of countries API`_

.. _list of countries API: https://de1.api.radio-browser.info/#List_of_countries
.. _list of languages API: https://de1.api.radio-browser.info/#List_of_languages

Definition at line 127 of file radio_browser.py.

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

◆ request()

searx.engines.radio_browser.request ( query,
params )

Definition at line 55 of file radio_browser.py.

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

◆ response()

searx.engines.radio_browser.response ( resp)

Definition at line 80 of file radio_browser.py.

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

Variable Documentation

◆ about

dict searx.engines.radio_browser.about
Initial value:
1= {
2 "website": 'https://www.radio-browser.info/',
3 "wikidata_id": 'Q111664849',
4 "official_api_documentation": 'https://de1.api.radio-browser.info/',
5 "use_official_api": True,
6 "require_api_key": False,
7 "results": 'JSON',
8}

Definition at line 19 of file radio_browser.py.

◆ base_url

str searx.engines.radio_browser.base_url = "https://de1.api.radio-browser.info"

Definition at line 30 of file radio_browser.py.

◆ categories

list searx.engines.radio_browser.categories = ['music', 'radio']

Definition at line 28 of file radio_browser.py.

◆ number_of_results

int searx.engines.radio_browser.number_of_results = 10

Definition at line 31 of file radio_browser.py.

◆ paging

bool searx.engines.radio_browser.paging = True

Definition at line 27 of file radio_browser.py.

◆ station_filters

list searx.engines.radio_browser.station_filters = []

Definition at line 33 of file radio_browser.py.