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
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()
143 country_list = get(f'{base_url}/json/countries').json()
144
145 for lang in language_list:
146
147 babel_lang = lang.get('iso_639')
148 if not babel_lang:
149
150
151 continue
152 try:
153 sxng_tag = language_tag(babel.Locale.parse(babel_lang, sep="-"))
154 except babel.UnknownLocaleError:
155
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
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