Compare commits

...

10 commits

6 changed files with 674 additions and 510 deletions

1134
app.py

File diff suppressed because it is too large Load diff

View file

@ -25,7 +25,7 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None):
call_params = params.copy() call_params = params.copy()
call_params['titles'] = '|'.join(f'File:{f}' for f in cur) call_params['titles'] = '|'.join(f'File:{f}' for f in cur)
r = mediawiki.api_call(call_params, api_url=commons_url) r = mediawiki.api_post(call_params, api_url=commons_url)
for image in r.json()['query']['pages']: for image in r.json()['query']['pages']:
filename = utils.drop_start(image['title'], 'File:') filename = utils.drop_start(image['title'], 'File:')

View file

@ -1,15 +1,23 @@
import logging import logging
from logging.handlers import SMTPHandler from logging.handlers import SMTPHandler
from logging import Formatter from logging import Formatter
from flask import request from flask import request, g
PROJECT = 'depicts' PROJECT = 'depicts'
class MatcherSMTPHandler(SMTPHandler): class MatcherSMTPHandler(SMTPHandler):
def getSubject(self, record): # noqa: N802 def getSubject(self, record): # noqa: N802
return (f'{PROJECT} error: {record.exc_info[0].__name__}' subject = (f'{PROJECT} error: {record.exc_info[0].__name__}'
if (record.exc_info and record.exc_info[0]) if (record.exc_info and record.exc_info[0])
else f'{PROJECT} error: {record.pathname}:{record.lineno:d}') else f'{PROJECT} error: {record.pathname}:{record.lineno:d}')
if qid := getattr(g, 'qid', None):
subject += f' {qid}'
if label := getattr(g, 'label', None):
subject += f': {label}'
return subject
class RequestFormatter(Formatter): class RequestFormatter(Formatter):
def format(self, record): def format(self, record):

View file

@ -39,7 +39,7 @@ def from_name(name):
found = [] found = []
for entity in mediawiki.get_entities_with_cache(qids, props='labels|descriptions'): for entity in mediawiki.get_entities_with_cache(qids, props='labels|descriptions'):
if 'redirects' in entity: if 'redirects' in entity or 'missing' in entity:
continue continue
qid = entity['id'] qid = entity['id']
item = lookup[qid] item = lookup[qid]
@ -51,7 +51,7 @@ def from_name(name):
label = wikibase.get_entity_label(entity) label = wikibase.get_entity_label(entity)
if label: if label:
i['label'] = label i['label'] = label
if 'en' in entity['descriptions']: if 'en' in entity.get('descriptions', {}):
i['description'] = entity['descriptions']['en']['value'] i['description'] = entity['descriptions']['en']['value']
found.append(i) found.append(i)
found.sort(key=lambda i: i.get('label', '')) found.sort(key=lambda i: i.get('label', ''))

View file

@ -24,6 +24,16 @@ def api_call(params, api_url=wikidata_url):
r = requests.get(api_url, params=call_params, timeout=5) r = requests.get(api_url, params=call_params, timeout=5)
return r return r
def api_post(params, api_url=wikidata_url):
call_params = {
'format': 'json',
'formatversion': 2,
**params,
}
r = requests.post(api_url, data=call_params, timeout=5)
return r
def get_list(list_name, **params): def get_list(list_name, **params):
r = api_call({'action': 'query', 'list': list_name, **params}) r = api_call({'action': 'query', 'list': list_name, **params})
return r.json()['query'][list_name] return r.json()['query'][list_name]

View file

@ -153,12 +153,15 @@ def check_catalog(entity, catalog):
catalog.update(cat) catalog.update(cat)
return return
html = get_catalog_url(catalog_url) try:
if html: html = get_catalog_url(catalog_url)
description = get_description_from_page(html) if html:
if description: description = get_description_from_page(html)
catalog['description'] = description if description:
return catalog['description'] = description
return
except UnicodeDecodeError:
return
for property_id in sorted(catalog_ids): for property_id in sorted(catalog_ids):
if property_id == 'P350': if property_id == 'P350':
@ -203,7 +206,8 @@ def get_catalog_from_artwork(entity):
check_catalog(entity, catalog) check_catalog(entity, catalog)
except (requests.exceptions.ReadTimeout, except (requests.exceptions.ReadTimeout,
requests.exceptions.ConnectTimeout, requests.exceptions.ConnectTimeout,
requests.exceptions.ConnectionError): requests.exceptions.ConnectionError,
requests.exceptions.TooManyRedirects):
pass pass
return catalog return catalog