diff options
| author | Alexandre Flament <alex@al-f.net> | 2021-10-03 19:09:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-03 19:09:07 +0200 |
| commit | 47eb836c657f581fab12d68c978d9520e2e14417 (patch) | |
| tree | 2323c4bbc497657238723cad619ce5769b87ca34 /searxng_extra/update/update_wikidata_units.py | |
| parent | 9da9dbcbb45bd41948789529a3bd5e32da6cb62f (diff) | |
| parent | 715c445e9bcc4c842e0cd8fc49d93372da8c5fb9 (diff) | |
Merge pull request #375 from dalf/searxng_extra
SearXNG: searx_extra
Diffstat (limited to 'searxng_extra/update/update_wikidata_units.py')
| -rwxr-xr-x | searxng_extra/update/update_wikidata_units.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/searxng_extra/update/update_wikidata_units.py b/searxng_extra/update/update_wikidata_units.py new file mode 100755 index 000000000..43a872b1b --- /dev/null +++ b/searxng_extra/update/update_wikidata_units.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +# pylint: disable=missing-module-docstring + +import json +import collections + +# set path +from os.path import join + +from searx import searx_dir +from searx.engines import wikidata, set_loggers + +set_loggers(wikidata, 'wikidata') + +# the response contains duplicate ?item with the different ?symbol +# "ORDER BY ?item DESC(?rank) ?symbol" provides a deterministic result +# even if a ?item has different ?symbol of the same rank. +# A deterministic result +# see: +# * https://www.wikidata.org/wiki/Help:Ranking +# * https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format ("Statement representation" section) +# * https://w.wiki/32BT +# see the result for https://www.wikidata.org/wiki/Q11582 +# there are multiple symbols the same rank +SARQL_REQUEST = """ +SELECT DISTINCT ?item ?symbol +WHERE +{ + ?item wdt:P31/wdt:P279 wd:Q47574 . + ?item p:P5061 ?symbolP . + ?symbolP ps:P5061 ?symbol ; + wikibase:rank ?rank . + FILTER(LANG(?symbol) = "en"). +} +ORDER BY ?item DESC(?rank) ?symbol +""" + + +def get_data(): + results = collections.OrderedDict() + response = wikidata.send_wikidata_query(SARQL_REQUEST) + for unit in response['results']['bindings']: + name = unit['item']['value'].replace('http://www.wikidata.org/entity/', '') + unit = unit['symbol']['value'] + if name not in results: + # ignore duplicate: always use the first one + results[name] = unit + return results + + +def get_wikidata_units_filename(): + return join(join(searx_dir, "data"), "wikidata_units.json") + + +with open(get_wikidata_units_filename(), 'w', encoding="utf8") as f: + json.dump(get_data(), f, indent=4, ensure_ascii=False) |