From 1bb82a6b54e53d683c3041a1576be64ae234abee Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Sat, 2 Oct 2021 17:30:39 +0200 Subject: SearXNG: searxng_extra --- searxng_extra/update/update_wikidata_units.py | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 searxng_extra/update/update_wikidata_units.py (limited to 'searxng_extra/update/update_wikidata_units.py') diff --git a/searxng_extra/update/update_wikidata_units.py b/searxng_extra/update/update_wikidata_units.py new file mode 100755 index 000000000..ddde4c135 --- /dev/null +++ b/searxng_extra/update/update_wikidata_units.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +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') as f: + json.dump(get_data(), f, indent=4, ensure_ascii=False) -- cgit v1.2.3 From 955eab8240b4dd903e431b85f6daf35c8672d39c Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sun, 3 Oct 2021 15:12:09 +0200 Subject: [mod] searxng_extras - minor improvements - fix docs/searxng_extra/standalone_searx.py.rst - add SPDX tag - pylint standalone_searx.py and update_wikidata_units.py Signed-off-by: Markus Heiser --- searxng_extra/update/update_wikidata_units.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'searxng_extra/update/update_wikidata_units.py') diff --git a/searxng_extra/update/update_wikidata_units.py b/searxng_extra/update/update_wikidata_units.py index ddde4c135..43a872b1b 100755 --- a/searxng_extra/update/update_wikidata_units.py +++ b/searxng_extra/update/update_wikidata_units.py @@ -1,4 +1,7 @@ #!/usr/bin/env python +# SPDX-License-Identifier: AGPL-3.0-or-later +# lint: pylint +# pylint: disable=missing-module-docstring import json import collections @@ -51,5 +54,5 @@ def get_wikidata_units_filename(): return join(join(searx_dir, "data"), "wikidata_units.json") -with open(get_wikidata_units_filename(), 'w') as f: +with open(get_wikidata_units_filename(), 'w', encoding="utf8") as f: json.dump(get_data(), f, indent=4, ensure_ascii=False) -- cgit v1.2.3