|
@@ -5,9 +5,12 @@
|
|
|
|
|
|
from urllib.parse import quote
|
|
|
from json import loads
|
|
|
-from lxml.html import fromstring
|
|
|
+from lxml import html
|
|
|
from searx.utils import match_language, searx_useragent
|
|
|
-from searx.network import raise_for_httperror
|
|
|
+from searx import network
|
|
|
+from searx.enginelib.traits import EngineTraits
|
|
|
+
|
|
|
+engine_traits: EngineTraits
|
|
|
|
|
|
|
|
|
about = {
|
|
@@ -68,7 +71,7 @@ def response(resp):
|
|
|
):
|
|
|
return []
|
|
|
|
|
|
- raise_for_httperror(resp)
|
|
|
+ network.raise_for_httperror(resp)
|
|
|
|
|
|
results = []
|
|
|
api_result = loads(resp.text)
|
|
@@ -98,7 +101,7 @@ def response(resp):
|
|
|
|
|
|
def _fetch_supported_languages(resp):
|
|
|
supported_languages = {}
|
|
|
- dom = fromstring(resp.text)
|
|
|
+ dom = html.fromstring(resp.text)
|
|
|
tables = dom.xpath('//table[contains(@class,"sortable")]')
|
|
|
for table in tables:
|
|
|
|
|
@@ -114,3 +117,166 @@ def _fetch_supported_languages(resp):
|
|
|
supported_languages[code] = {"name": name, "english_name": english_name}
|
|
|
|
|
|
return supported_languages
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+lang_map = {
|
|
|
+ 'be-tarask': 'bel',
|
|
|
+ 'ak': 'aka',
|
|
|
+ 'als': 'gsw',
|
|
|
+ 'bat-smg': 'sgs',
|
|
|
+ 'cbk-zam': 'cbk',
|
|
|
+ 'fiu-vro': 'vro',
|
|
|
+ 'map-bms': 'map',
|
|
|
+ 'nrm': 'nrf',
|
|
|
+ 'roa-rup': 'rup',
|
|
|
+ 'nds-nl': 'nds',
|
|
|
+
|
|
|
+
|
|
|
+ 'zh-classical': 'zh_Hant',
|
|
|
+ 'zh-min-nan': 'nan',
|
|
|
+ 'zh-yue': 'yue',
|
|
|
+ 'an': 'arg',
|
|
|
+}
|
|
|
+
|
|
|
+unknown_langs =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|
+ 'vep',
|
|
|
+ 'vls',
|
|
|
+ 'vo',
|
|
|
+ 'wa',
|
|
|
+ 'xal',
|
|
|
+]
|
|
|
+
|
|
|
+
|
|
|
+def fetch_traits(engine_traits: EngineTraits):
|
|
|
+ """Fetch languages from Wikipedia"""
|
|
|
+
|
|
|
+
|
|
|
+ engine_traits.data_type = 'supported_languages'
|
|
|
+
|
|
|
+ import babel
|
|
|
+ from searx.locales import language_tag
|
|
|
+
|
|
|
+ resp = network.get('https://meta.wikimedia.org/wiki/List_of_Wikipedias')
|
|
|
+ if not resp.ok:
|
|
|
+ print("ERROR: response from Wikipedia is not OK.")
|
|
|
+
|
|
|
+ dom = html.fromstring(resp.text)
|
|
|
+ for row in dom.xpath('//table[contains(@class,"sortable")]//tbody/tr'):
|
|
|
+
|
|
|
+ cols = row.xpath('./td')
|
|
|
+ if not cols:
|
|
|
+ continue
|
|
|
+
|
|
|
+ cols = [c.text_content().strip() for c in cols]
|
|
|
+ articles = int(cols[4].replace(',', '').replace('-', '0'))
|
|
|
+ users = int(cols[8].replace(',', '').replace('-', '0'))
|
|
|
+ depth = cols[11].strip('-')
|
|
|
+
|
|
|
+ if articles < 1000:
|
|
|
+
|
|
|
+ continue
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if depth == '':
|
|
|
+ if users < 1000:
|
|
|
+
|
|
|
+ continue
|
|
|
+ elif int(depth) < 20:
|
|
|
+ continue
|
|
|
+
|
|
|
+ eng_tag = cols[3]
|
|
|
+
|
|
|
+ if eng_tag in unknown_langs:
|
|
|
+ continue
|
|
|
+
|
|
|
+ try:
|
|
|
+ sxng_tag = language_tag(babel.Locale.parse(lang_map.get(eng_tag, eng_tag)))
|
|
|
+ except babel.UnknownLocaleError:
|
|
|
+ print("ERROR: %s -> %s is unknown by babel" % (cols[1], eng_tag))
|
|
|
+ continue
|
|
|
+
|
|
|
+ conflict = engine_traits.languages.get(sxng_tag)
|
|
|
+ if conflict:
|
|
|
+ if conflict != eng_tag:
|
|
|
+ print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag))
|
|
|
+ continue
|
|
|
+ engine_traits.languages[sxng_tag] = eng_tag
|
|
|
+
|
|
|
+ engine_traits.languages['zh_Hans'] = 'zh'
|