update_wikidata_units.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. # lint: pylint
  4. # pylint: disable=missing-module-docstring
  5. """Fetch units from :origin:`searx/engines/wikidata.py` engine.
  6. Output file: :origin:`searx/data/wikidata_units.json` (:origin:`CI Update data
  7. ... <.github/workflows/data-update.yml>`).
  8. """
  9. import json
  10. import collections
  11. # set path
  12. from os.path import join
  13. from searx import searx_dir
  14. from searx.engines import wikidata, set_loggers
  15. from searx.data import data_dir
  16. DATA_FILE = data_dir / 'wikidata_units.json'
  17. set_loggers(wikidata, 'wikidata')
  18. # the response contains duplicate ?item with the different ?symbol
  19. # "ORDER BY ?item DESC(?rank) ?symbol" provides a deterministic result
  20. # even if a ?item has different ?symbol of the same rank.
  21. # A deterministic result
  22. # see:
  23. # * https://www.wikidata.org/wiki/Help:Ranking
  24. # * https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format ("Statement representation" section)
  25. # * https://w.wiki/32BT
  26. # see the result for https://www.wikidata.org/wiki/Q11582
  27. # there are multiple symbols the same rank
  28. SARQL_REQUEST = """
  29. SELECT DISTINCT ?item ?symbol
  30. WHERE
  31. {
  32. ?item wdt:P31/wdt:P279 wd:Q47574 .
  33. ?item p:P5061 ?symbolP .
  34. ?symbolP ps:P5061 ?symbol ;
  35. wikibase:rank ?rank .
  36. FILTER(LANG(?symbol) = "en").
  37. }
  38. ORDER BY ?item DESC(?rank) ?symbol
  39. """
  40. def get_data():
  41. results = collections.OrderedDict()
  42. response = wikidata.send_wikidata_query(SARQL_REQUEST)
  43. for unit in response['results']['bindings']:
  44. name = unit['item']['value'].replace('http://www.wikidata.org/entity/', '')
  45. unit = unit['symbol']['value']
  46. if name not in results:
  47. # ignore duplicate: always use the first one
  48. results[name] = unit
  49. return results
  50. def get_wikidata_units_filename():
  51. return join(join(searx_dir, "data"), "")
  52. if __name__ == '__main__':
  53. with DATA_FILE.open('w', encoding="utf8") as f:
  54. json.dump(get_data(), f, indent=4, sort_keys=True, ensure_ascii=False)