190def fetch_traits(engine_traits: EngineTraits):
191 """Fetch languages and regions from Bing-Web."""
192
193
195
196 resp = get("https://www.bing.com/account/general")
197 if not resp.ok:
198 print("ERROR: response from bing is not OK.")
199
200 dom = html.fromstring(resp.text)
201
202
203
204 engine_traits.languages['zh'] = 'zh-hans'
205
206 map_lang = {'prs': 'fa-AF', 'en': 'en-us'}
207 bing_ui_lang_map = {
208
209 'en': 'us',
210 'da': 'dk',
211 }
212
213 for href in eval_xpath(dom, '//div[@id="language-section"]//li/a/@href'):
214 eng_lang = parse_qs(urlparse(href).query)['setlang'][0]
215 babel_lang = map_lang.get(eng_lang, eng_lang)
216 try:
217 sxng_tag = language_tag(babel.Locale.parse(babel_lang.replace('-', '_')))
218 except babel.UnknownLocaleError:
219 print("ERROR: language (%s) is unknown by babel" % (babel_lang))
220 continue
221
222
223
224
225 bing_ui_lang = eng_lang.lower()
226 if '-' not in bing_ui_lang:
227 bing_ui_lang = bing_ui_lang + '-' + bing_ui_lang_map.get(bing_ui_lang, bing_ui_lang)
228
229 conflict = engine_traits.languages.get(sxng_tag)
230 if conflict:
231 if conflict != bing_ui_lang:
232 print(f"CONFLICT: babel {sxng_tag} --> {conflict}, {bing_ui_lang}")
233 continue
234 engine_traits.languages[sxng_tag] = bing_ui_lang
235
236
237
238 engine_traits.regions['zh-CN'] = 'zh-cn'
239
240 map_market_codes = {
241 'zh-hk': 'en-hk',
242 }
243 for href in eval_xpath(dom, '//div[@id="region-section"]//li/a/@href'):
244 cc_tag = parse_qs(urlparse(href).query)['cc'][0]
245 if cc_tag == 'clear':
246 engine_traits.all_locale = cc_tag
247 continue
248
249
250 for lang_tag in babel.languages.get_official_languages(cc_tag, de_facto=True):
251 if lang_tag not in engine_traits.languages.keys():
252
253 continue
254 lang_tag = lang_tag.split('_')[0]
255 market_code = f"{lang_tag}-{cc_tag}"
256
257 market_code = map_market_codes.get(market_code, market_code)
258 sxng_tag = region_tag(babel.Locale.parse('%s_%s' % (lang_tag, cc_tag.upper())))
259 conflict = engine_traits.regions.get(sxng_tag)
260 if conflict:
261 if conflict != market_code:
262 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, market_code))
263 continue
264 engine_traits.regions[sxng_tag] = market_code