| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import re | 
					
						
							|  |  |  | import unicodedata | 
					
						
							|  |  |  | import string | 
					
						
							|  |  |  | from urllib import urlencode | 
					
						
							|  |  |  | from requests import get | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | languages = {'de', 'en', 'es', 'fr', 'hu', 'it', 'nl', 'jp'} | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | url_template = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&{query}&props=labels%7Cdatatype%7Cclaims%7Caliases&languages=' + '|'.join(languages) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | url_wmflabs_template = 'http://wdq.wmflabs.org/api?q=' | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  | url_wikidata_search_template = 'http://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}' | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | wmflabs_queries = [ | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  |     'CLAIM[31:8142]',  # all devise | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | ] | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | db = { | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  |     'iso4217': { | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     'names': { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def remove_accents(data): | 
					
						
							|  |  |  |     return unicodedata.normalize('NFKD', data).lower() | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def normalize_name(name): | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  |     return re.sub(' +', ' ', remove_accents(name.lower()).replace('-', ' ')) | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def add_currency_name(name, iso4217): | 
					
						
							|  |  |  |     global db | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     db_names = db['names'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not isinstance(iso4217, basestring): | 
					
						
							|  |  |  |         print "problem", name, iso4217 | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     name = normalize_name(name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if name == '': | 
					
						
							|  |  |  |         print "name empty", iso4217 | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     iso4217_set = db_names.get(name, None) | 
					
						
							|  |  |  |     if iso4217_set is not None and iso4217 not in iso4217_set: | 
					
						
							|  |  |  |         db_names[name].append(iso4217) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  |         db_names[name] = [iso4217] | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def add_currency_label(label, iso4217, language): | 
					
						
							|  |  |  |     global db | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     db['iso4217'][iso4217] = db['iso4217'].get(iso4217, {}) | 
					
						
							|  |  |  |     db['iso4217'][iso4217][language] = label | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_property_value(data, name): | 
					
						
							|  |  |  |     prop = data.get('claims', {}).get(name, {}) | 
					
						
							|  |  |  |     if len(prop) == 0: | 
					
						
							|  |  |  |         return None | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     value = prop[0].get('mainsnak', {}).get('datavalue', {}).get('value', '') | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |     if value == '': | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return value | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def parse_currency(data): | 
					
						
							|  |  |  |     iso4217 = get_property_value(data, 'P498') | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |     if iso4217 is not None: | 
					
						
							|  |  |  |         unit = get_property_value(data, 'P558') | 
					
						
							|  |  |  |         if unit is not None: | 
					
						
							|  |  |  |             add_currency_name(unit, iso4217) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |         labels = data.get('labels', {}) | 
					
						
							|  |  |  |         for language in languages: | 
					
						
							|  |  |  |             name = labels.get(language, {}).get('value', None) | 
					
						
							| 
									
										
										
										
											2016-07-11 14:17:05 +02:00
										 |  |  |             if name is not None: | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |                 add_currency_name(name, iso4217) | 
					
						
							|  |  |  |                 add_currency_label(name, iso4217, language) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         aliases = data.get('aliases', {}) | 
					
						
							|  |  |  |         for language in aliases: | 
					
						
							|  |  |  |             for i in range(0, len(aliases[language])): | 
					
						
							|  |  |  |                 alias = aliases[language][i].get('value', None) | 
					
						
							|  |  |  |                 add_currency_name(alias, iso4217) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | def fetch_data(wikidata_ids): | 
					
						
							| 
									
										
										
										
											2016-07-11 13:52:37 +02:00
										 |  |  |     url = url_template.format(query=urlencode({'ids': '|'.join(wikidata_ids)})) | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |     htmlresponse = get(url) | 
					
						
							|  |  |  |     jsonresponse = json.loads(htmlresponse.content) | 
					
						
							|  |  |  |     entities = jsonresponse.get('entities', {}) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |     for pname in entities: | 
					
						
							|  |  |  |         pvalue = entities.get(pname) | 
					
						
							|  |  |  |         parse_currency(pvalue) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | def add_q(i): | 
					
						
							|  |  |  |     return "Q" + str(i) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | def fetch_data_batch(wikidata_ids): | 
					
						
							|  |  |  |     while len(wikidata_ids) > 0: | 
					
						
							|  |  |  |         if len(wikidata_ids) > 50: | 
					
						
							|  |  |  |             fetch_data(wikidata_ids[0:49]) | 
					
						
							|  |  |  |             wikidata_ids = wikidata_ids[50:] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             fetch_data(wikidata_ids) | 
					
						
							|  |  |  |             wikidata_ids = [] | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | def wdq_query(query): | 
					
						
							|  |  |  |     url = url_wmflabs_template + query | 
					
						
							|  |  |  |     htmlresponse = get(url) | 
					
						
							|  |  |  |     jsonresponse = json.loads(htmlresponse.content) | 
					
						
							|  |  |  |     qlist = map(add_q, jsonresponse.get('items', {})) | 
					
						
							|  |  |  |     error = jsonresponse.get('status', {}).get('error', None) | 
					
						
							| 
									
										
										
										
											2016-07-11 14:17:05 +02:00
										 |  |  |     if error is not None and error != 'OK': | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |         print "error for query '" + query + "' :" + error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fetch_data_batch(qlist) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | def wd_query(query, offset=0): | 
					
						
							|  |  |  |     qlist = [] | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  |     url = url_wikidata_search_template.format(query=urlencode({'srsearch': query, 'srlimit': 50, 'sroffset': offset})) | 
					
						
							|  |  |  |     htmlresponse = get(url) | 
					
						
							|  |  |  |     jsonresponse = json.loads(htmlresponse.content) | 
					
						
							|  |  |  |     for r in jsonresponse.get('query', {}).get('search', {}): | 
					
						
							|  |  |  |         qlist.append(r.get('title', '')) | 
					
						
							|  |  |  |     fetch_data_batch(qlist) | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-15 19:49:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 14:40:21 +02:00
										 |  |  | # fetch # | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | for q in wmflabs_queries: | 
					
						
							|  |  |  |     wdq_query(q) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 18:43:28 +02:00
										 |  |  | # static | 
					
						
							| 
									
										
										
										
											2015-05-12 20:52:08 +02:00
										 |  |  | add_currency_name(u"euro", 'EUR') | 
					
						
							|  |  |  | add_currency_name(u"euros", 'EUR') | 
					
						
							|  |  |  | add_currency_name(u"dollar", 'USD') | 
					
						
							|  |  |  | add_currency_name(u"dollars", 'USD') | 
					
						
							|  |  |  | add_currency_name(u"peso", 'MXN') | 
					
						
							|  |  |  | add_currency_name(u"pesos", 'MXN') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # write | 
					
						
							|  |  |  | f = open("currencies.json", "wb") | 
					
						
							|  |  |  | json.dump(db, f, indent=4, encoding="utf-8") | 
					
						
							|  |  |  | f.close() |