6from urllib import urlencode
7from requests import get
9languages = {'de', 'en', 'es', 'fr', 'hu', 'it', 'nl', 'jp'}
11url_template = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&{query}&props=labels%7Cdatatype%7Cclaims%7Caliases&languages=' + '|'.join(languages)
12url_wmflabs_template = 'http://wdq.wmflabs.org/api?q='
13url_wikidata_search_template='http://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
16 'CLAIM[31:8142]', # all devise
27def remove_accents(data):
28 return unicodedata.normalize('NFKD', data).lower()
31def normalize_name(name):
32 return re.sub(' +',' ', remove_accents(name.lower()).replace('-', ' '))
35def add_currency_name(name, iso4217):
38 db_names = db['names']
41 if not isinstance(iso4217, basestring):
42 print "problem", name, iso4217
45 name = normalize_name(name)
48 print "name empty", iso4217
51 iso4217_set = db_names.get(name, None)
52 if iso4217_set is not None and iso4217 not in iso4217_set:
53 db_names[name].append(iso4217)
55 db_names[name] = [ iso4217 ]
58def add_currency_label(label, iso4217, language):
61 db['iso4217'][iso4217] = db['iso4217'].get(iso4217, {})
62 db['iso4217'][iso4217][language] = label
65def get_property_value(data, name):
66 prop = data.get('claims', {}).get(name, {})
70 value = prop[0].get('mainsnak', {}).get('datavalue', {}).get('value', '')
77def parse_currency(data):
78 iso4217 = get_property_value(data, 'P498')
80 if iso4217 is not None:
81 unit = get_property_value(data, 'P558')
83 add_currency_name(unit, iso4217)
85 labels = data.get('labels', {})
86 for language in languages:
87 name = labels.get(language, {}).get('value', None)
89 add_currency_name(name, iso4217)
90 add_currency_label(name, iso4217, language)
92 aliases = data.get('aliases', {})
93 for language in aliases:
94 for i in range(0, len(aliases[language])):
95 alias = aliases[language][i].get('value', None)
96 add_currency_name(alias, iso4217)
99def fetch_data(wikidata_ids):
100 url = url_template.format(query=urlencode({'ids' : '|'.join(wikidata_ids)}))
101 htmlresponse = get(url)
102 jsonresponse = json.loads(htmlresponse.content)
103 entities = jsonresponse.get('entities', {})
105 for pname in entities:
106 pvalue = entities.get(pname)
107 parse_currency(pvalue)
114def fetch_data_batch(wikidata_ids):
115 while len(wikidata_ids) > 0:
116 if len(wikidata_ids) > 50:
117 fetch_data(wikidata_ids[0:49])
118 wikidata_ids = wikidata_ids[50:]
120 fetch_data(wikidata_ids)
125 url = url_wmflabs_template + query
126 htmlresponse = get(url)
127 jsonresponse = json.loads(htmlresponse.content)
128 qlist = map(add_q, jsonresponse.get('items', {}))
129 error = jsonresponse.get('status', {}).get('error', None)
130 if error != None and error != 'OK':
131 print "error for query '" + query + "' :" + error
133 fetch_data_batch(qlist)
136def wd_query(query, offset=0):
139 url = url_wikidata_search_template.format(query=urlencode({'srsearch': query, 'srlimit': 50, 'sroffset': offset}))
140 htmlresponse = get(url)
141 jsonresponse = json.loads(htmlresponse.content)
142 for r in jsonresponse.get('query', {}).get('search', {}):
143 qlist.append(r.get('title', ''))
144 fetch_data_batch(qlist)
147for q in wmflabs_queries:
151add_currency_name(u"euro", 'EUR')
152add_currency_name(u"euros", 'EUR')
153add_currency_name(u"dollar", 'USD')
154add_currency_name(u"dollars", 'USD')
155add_currency_name(u"peso", 'MXN')
156add_currency_name(u"pesos", 'MXN')
159f = open("currencies.json", "wb")
160json.dump(db, f, indent=4, encoding="utf-8")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list