fetch_wikidata_units.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. import json
  3. import collections
  4. # set path
  5. from sys import path
  6. from os.path import realpath, dirname, join
  7. path.append(realpath(dirname(realpath(__file__)) + '/../'))
  8. from searx import searx_dir
  9. from searx.engines.wikidata import send_wikidata_query
  10. SARQL_REQUEST = """
  11. SELECT DISTINCT ?item ?symbol ?P2370 ?P2370Unit ?P2442 ?P2442Unit
  12. WHERE
  13. {
  14. ?item wdt:P31/wdt:P279 wd:Q47574.
  15. ?item wdt:P5061 ?symbol.
  16. FILTER(LANG(?symbol) = "en").
  17. }
  18. ORDER BY ?item
  19. """
  20. def get_data():
  21. def get_key(unit):
  22. return unit['item']['value'].replace('http://www.wikidata.org/entity/', '')
  23. def get_value(unit):
  24. return unit['symbol']['value']
  25. result = send_wikidata_query(SARQL_REQUEST)
  26. if result is not None:
  27. # sort the unit by entity name
  28. # so different fetchs keep the file unchanged.
  29. list(result['results']['bindings']).sort(key=get_key)
  30. return collections.OrderedDict([(get_key(unit), get_value(unit)) for unit in result['results']['bindings']])
  31. def get_wikidata_units_filename():
  32. return join(join(searx_dir, "data"), "wikidata_units.json")
  33. with open(get_wikidata_units_filename(), 'w') as f:
  34. json.dump(get_data(), f, indent=4, ensure_ascii=False)