[fix] code cleanup

This commit is contained in:
Adam Tauber 2014-10-04 22:53:54 +02:00
parent a48028b771
commit 727c7226d9

View File

@ -1,7 +1,6 @@
import json import json
from requests import get from requests import get
from urllib import urlencode from urllib import urlencode
from datetime import datetime
resultCount=2 resultCount=2
urlSearch = 'https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}' urlSearch = 'https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
@ -10,7 +9,6 @@ urlMap = 'https://www.openstreetmap.org/?lat={latitude}&lon={longitude}&zoom={zo
def request(query, params): def request(query, params):
params['url'] = urlSearch.format(query=urlencode({'srsearch': query, 'srlimit': resultCount})) params['url'] = urlSearch.format(query=urlencode({'srsearch': query, 'srlimit': resultCount}))
print params['url']
return params return params
@ -27,9 +25,7 @@ def response(resp):
language = 'en' language = 'en'
url = urlDetail.format(query=urlencode({'ids': '|'.join(wikidata_ids), 'languages': language + '|en'})) url = urlDetail.format(query=urlencode({'ids': '|'.join(wikidata_ids), 'languages': language + '|en'}))
before = datetime.now()
htmlresponse = get(url) htmlresponse = get(url)
print datetime.now() - before
jsonresponse = json.loads(htmlresponse.content) jsonresponse = json.loads(htmlresponse.content)
for wikidata_id in wikidata_ids: for wikidata_id in wikidata_ids:
results = results + getDetail(jsonresponse, wikidata_id, language) results = results + getDetail(jsonresponse, wikidata_id, language)
@ -38,7 +34,7 @@ def response(resp):
def getDetail(jsonresponse, wikidata_id, language): def getDetail(jsonresponse, wikidata_id, language):
result = jsonresponse.get('entities', {}).get(wikidata_id, {}) result = jsonresponse.get('entities', {}).get(wikidata_id, {})
title = result.get('labels', {}).get(language, {}).get('value', None) title = result.get('labels', {}).get(language, {}).get('value', None)
if title == None: if title == None:
title = result.get('labels', {}).get('en', {}).get('value', wikidata_id) title = result.get('labels', {}).get('en', {}).get('value', wikidata_id)
@ -86,7 +82,7 @@ def getDetail(jsonresponse, wikidata_id, language):
musicbrainz_release_group_id = get_string(claims, 'P436') musicbrainz_release_group_id = get_string(claims, 'P436')
if musicbrainz_release_group_id != None: if musicbrainz_release_group_id != None:
add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/release-group/' + musicbrainz_release_group_id) add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/release-group/' + musicbrainz_release_group_id)
musicbrainz_label_id = get_string(claims, 'P966') musicbrainz_label_id = get_string(claims, 'P966')
if musicbrainz_label_id != None: if musicbrainz_label_id != None:
add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/label/' + musicbrainz_label_id) add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/label/' + musicbrainz_label_id)
@ -111,7 +107,7 @@ def getDetail(jsonresponse, wikidata_id, language):
results.append({ results.append({
'infobox' : title, 'infobox' : title,
'id' : wikipedia_en_link, 'id' : wikipedia_en_link,
'content' : description, 'content' : description,
'attributes' : attributes, 'attributes' : attributes,
@ -144,7 +140,6 @@ def get_string(claims, propertyName, defaultValue=None):
for e in propValue: for e in propValue:
mainsnak = e.get('mainsnak', {}) mainsnak = e.get('mainsnak', {})
datatype = mainsnak.get('datatype', '')
datavalue = mainsnak.get('datavalue', {}) datavalue = mainsnak.get('datavalue', {})
if datavalue != None: if datavalue != None:
result.append(datavalue.get('value', '')) result.append(datavalue.get('value', ''))
@ -164,7 +159,6 @@ def get_time(claims, propertyName, defaultValue=None):
for e in propValue: for e in propValue:
mainsnak = e.get('mainsnak', {}) mainsnak = e.get('mainsnak', {})
datatype = mainsnak.get('datatype', '')
datavalue = mainsnak.get('datavalue', {}) datavalue = mainsnak.get('datavalue', {})
if datavalue != None: if datavalue != None:
value = datavalue.get('value', '') value = datavalue.get('value', '')
@ -192,7 +186,7 @@ def get_geolink(claims, propertyName, defaultValue=''):
precision = value.get('precision', 0.0002) precision = value.get('precision', 0.0002)
# there is no zoom information, deduce from precision (error prone) # there is no zoom information, deduce from precision (error prone)
# samples : # samples :
# 13 --> 5 # 13 --> 5
# 1 --> 6 # 1 --> 6