149def fetch_traits(engine_traits: EngineTraits):
150 """Fetch languages and other search arguments from Anna's search form."""
151
152
153 import babel
156
157 engine_traits.all_locale = ''
158 engine_traits.custom['content'] = []
159 engine_traits.custom['ext'] = []
160 engine_traits.custom['sort'] = []
161
162 resp = get(base_url + '/search')
163 if not resp.ok:
164 raise RuntimeError("Response from Anna's search page is not OK.")
165 dom = html.fromstring(resp.text)
166
167
168
169 lang_map = {}
170 for x in eval_xpath_list(dom, "//form//input[@name='lang']"):
171 eng_lang = x.get("value")
172 if eng_lang in ('', '_empty', 'nl-BE', 'und'):
173 continue
174 try:
175 locale = babel.Locale.parse(lang_map.get(eng_lang, eng_lang), sep='-')
176 except babel.UnknownLocaleError:
177
178
179 continue
180 sxng_lang = language_tag(locale)
181 conflict = engine_traits.languages.get(sxng_lang)
182 if conflict:
183 if conflict != eng_lang:
184 print("CONFLICT: babel %s --> %s, %s" % (sxng_lang, conflict, eng_lang))
185 continue
186 engine_traits.languages[sxng_lang] = eng_lang
187
188 for x in eval_xpath_list(dom, "//form//input[@name='content']"):
189 engine_traits.custom['content'].append(x.get("value"))
190
191 for x in eval_xpath_list(dom, "//form//input[@name='ext']"):
192 engine_traits.custom['ext'].append(x.get("value"))
193
194 for x in eval_xpath_list(dom, "//form//select[@name='sort']//option"):
195 engine_traits.custom['sort'].append(x.get("value"))
196
197
198 engine_traits.custom['content'].sort()
199 engine_traits.custom['ext'].sort()
200 engine_traits.custom['sort'].sort()