logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

fetch_currencies.py (4370B)


  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import json
  4. import re
  5. import unicodedata
  6. import string
  7. from urllib import urlencode
  8. from requests import get
  9. languages = {'de', 'en', 'es', 'fr', 'hu', 'it', 'nl', 'jp'}
  10. url_template = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&{query}&props=labels%7Cdatatype%7Cclaims%7Caliases&languages=' + '|'.join(languages)
  11. url_wmflabs_template = 'http://wdq.wmflabs.org/api?q='
  12. url_wikidata_search_template = 'http://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
  13. wmflabs_queries = [
  14. 'CLAIM[31:8142]', # all devise
  15. ]
  16. db = {
  17. 'iso4217': {
  18. },
  19. 'names': {
  20. }
  21. }
  22. def remove_accents(data):
  23. return unicodedata.normalize('NFKD', data).lower()
  24. def normalize_name(name):
  25. return re.sub(' +', ' ', remove_accents(name.lower()).replace('-', ' '))
  26. def add_currency_name(name, iso4217):
  27. global db
  28. db_names = db['names']
  29. if not isinstance(iso4217, basestring):
  30. print("problem", name, iso4217)
  31. return
  32. name = normalize_name(name)
  33. if name == '':
  34. print("name empty", iso4217)
  35. return
  36. iso4217_set = db_names.get(name, None)
  37. if iso4217_set is not None and iso4217 not in iso4217_set:
  38. db_names[name].append(iso4217)
  39. else:
  40. db_names[name] = [iso4217]
  41. def add_currency_label(label, iso4217, language):
  42. global db
  43. db['iso4217'][iso4217] = db['iso4217'].get(iso4217, {})
  44. db['iso4217'][iso4217][language] = label
  45. def get_property_value(data, name):
  46. prop = data.get('claims', {}).get(name, {})
  47. if len(prop) == 0:
  48. return None
  49. value = prop[0].get('mainsnak', {}).get('datavalue', {}).get('value', '')
  50. if value == '':
  51. return None
  52. return value
  53. def parse_currency(data):
  54. iso4217 = get_property_value(data, 'P498')
  55. if iso4217 is not None:
  56. unit = get_property_value(data, 'P558')
  57. if unit is not None:
  58. add_currency_name(unit, iso4217)
  59. labels = data.get('labels', {})
  60. for language in languages:
  61. name = labels.get(language, {}).get('value', None)
  62. if name is not None:
  63. add_currency_name(name, iso4217)
  64. add_currency_label(name, iso4217, language)
  65. aliases = data.get('aliases', {})
  66. for language in aliases:
  67. for i in range(0, len(aliases[language])):
  68. alias = aliases[language][i].get('value', None)
  69. add_currency_name(alias, iso4217)
  70. def fetch_data(wikidata_ids):
  71. url = url_template.format(query=urlencode({'ids': '|'.join(wikidata_ids)}))
  72. htmlresponse = get(url)
  73. jsonresponse = json.loads(htmlresponse.content)
  74. entities = jsonresponse.get('entities', {})
  75. for pname in entities:
  76. pvalue = entities.get(pname)
  77. parse_currency(pvalue)
  78. def add_q(i):
  79. return "Q" + str(i)
  80. def fetch_data_batch(wikidata_ids):
  81. while len(wikidata_ids) > 0:
  82. if len(wikidata_ids) > 50:
  83. fetch_data(wikidata_ids[0:49])
  84. wikidata_ids = wikidata_ids[50:]
  85. else:
  86. fetch_data(wikidata_ids)
  87. wikidata_ids = []
  88. def wdq_query(query):
  89. url = url_wmflabs_template + query
  90. htmlresponse = get(url)
  91. jsonresponse = json.loads(htmlresponse.content)
  92. qlist = map(add_q, jsonresponse.get('items', {}))
  93. error = jsonresponse.get('status', {}).get('error', None)
  94. if error is not None and error != 'OK':
  95. print("error for query '" + query + "' :" + error)
  96. fetch_data_batch(qlist)
  97. def wd_query(query, offset=0):
  98. qlist = []
  99. url = url_wikidata_search_template.format(query=urlencode({'srsearch': query, 'srlimit': 50, 'sroffset': offset}))
  100. htmlresponse = get(url)
  101. jsonresponse = json.loads(htmlresponse.content)
  102. for r in jsonresponse.get('query', {}).get('search', {}):
  103. qlist.append(r.get('title', ''))
  104. fetch_data_batch(qlist)
  105. # fetch #
  106. for q in wmflabs_queries:
  107. wdq_query(q)
  108. # static
  109. add_currency_name(u"euro", 'EUR')
  110. add_currency_name(u"euros", 'EUR')
  111. add_currency_name(u"dollar", 'USD')
  112. add_currency_name(u"dollars", 'USD')
  113. add_currency_name(u"peso", 'MXN')
  114. add_currency_name(u"pesos", 'MXN')
  115. # write
  116. f = open("currencies.json", "wb")
  117. json.dump(db, f, indent=4, encoding="utf-8")
  118. f.close()