There can be more than 50 people.

This commit is contained in:
Edward Betts 2019-10-08 11:40:48 +01:00
parent 077cdcfeb2
commit bb68a46f59
2 changed files with 20 additions and 16 deletions

View file

@ -1,5 +1,5 @@
from .model import HumanItem from .model import HumanItem
from . import mediawiki from . import mediawiki, utils
import re import re
re_four_digits = re.compile(r'\b\d{4}\b') re_four_digits = re.compile(r'\b\d{4}\b')
@ -41,19 +41,20 @@ def from_name(name):
qids = list(lookup.keys()) qids = list(lookup.keys())
found = [] found = []
for entity in mediawiki.get_entities_with_cache(qids, props='labels|descriptions'): for cur in utils.chunk(qids, 50):
qid = entity['id'] for entity in mediawiki.get_entities_with_cache(cur, props='labels|descriptions'):
item = lookup[qid] qid = entity['id']
i = { item = lookup[qid]
'qid': entity['id'], i = {
'year_of_birth': item.year_of_birth, 'qid': entity['id'],
'year_of_death': item.year_of_death, 'year_of_birth': item.year_of_birth,
} 'year_of_death': item.year_of_death,
label = mediawiki.get_entity_label(entity) }
if label: label = mediawiki.get_entity_label(entity)
i['label'] = label if label:
if 'en' in entity['descriptions']: i['label'] = label
i['description'] = entity['descriptions']['en']['value'] if 'en' in entity['descriptions']:
found.append(i) i['description'] = entity['descriptions']['en']['value']
found.append(i)
found.sort(key=lambda i: i['label']) found.sort(key=lambda i: i['label'])
return found return found

View file

@ -1,6 +1,7 @@
import requests import requests
import os import os
import json import json
import hashlib
from .category import Category from .category import Category
wikidata_url = 'https://www.wikidata.org/w/api.php' wikidata_url = 'https://www.wikidata.org/w/api.php'
@ -64,7 +65,9 @@ def get_entity_with_cache(qid, refresh=False):
return entity return entity
def get_entities_with_cache(ids, **params): def get_entities_with_cache(ids, **params):
filename = f'cache/entities_{"_".join(ids)}.json' md5 = hashlib.md5(' '.join(ids).encode('utf-8')).hexdigest()
filename = f'cache/entities_{md5}.json'
if os.path.exists(filename): if os.path.exists(filename):
entity = json.load(open(filename)) entity = json.load(open(filename))
else: else: