From 55ec37324e9008cdb641ee55ed367a941f0fed07 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Tue, 1 Mar 2022 13:03:13 +0100 Subject: [PATCH] Use Wikidata for cover imports from Commons. --- src/catalogue/constants.py | 1 + src/cover/utils.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/catalogue/constants.py b/src/catalogue/constants.py index 5bb775ac..9134e2d3 100644 --- a/src/catalogue/constants.py +++ b/src/catalogue/constants.py @@ -4,6 +4,7 @@ class WIKIDATA: GENDER = "P21" AUTHOR = "P50" + CREATOR = "P170" LANGUAGE = "P407" DATE_OF_DEATH = "P570" LAST_NAME = "P734" diff --git a/src/cover/utils.py b/src/cover/utils.py index 55fac474..8e215dba 100644 --- a/src/cover/utils.py +++ b/src/cover/utils.py @@ -6,6 +6,9 @@ from io import StringIO import json import re from urllib.request import FancyURLopener +from wikidata.client import Client +from catalogue.constants import WIKIDATA + class URLOpener(FancyURLopener): @property @@ -75,6 +78,9 @@ def get_wikimedia_data(url): >>> get_wikimedia_data('https://commons.wikimedia.org/wiki/File:Valdai_IverskyMon_asv2018_img47.jpg') {'title': 'Valdai IverskyMon asv2018 img47', 'author': 'A.Savin', 'source_url': 'https://commons.wikimedia.org/wiki/File:Valdai_IverskyMon_asv2018_img47.jpg', 'download_url': 'https://upload.wikimedia.org/wikipedia/commons/4/43/Valdai_IverskyMon_asv2018_img47.jpg', 'license_url': 'http://artlibre.org/licence/lal/en', 'license_name': 'FAL'} + >>> get_wikimedia_data('https://commons.wikimedia.org/wiki/File:Pymonenko_A_boy_in_a_straw_hat.jpg') + {'title': 'Chłopiec w słomkowym kapeluszu', 'author': 'Mykola Pymonenko', 'source_url': 'https://commons.wikimedia.org/wiki/File:Pymonenko_A_boy_in_a_straw_hat.jpg', 'download_url': 'https://upload.wikimedia.org/wikipedia/commons/9/9b/Pymonenko_A_boy_in_a_straw_hat.jpg', 'license_url': 'https://pl.wikipedia.org/wiki/Domena_publiczna', 'license_name': 'domena publiczna'} + """ file_name = url.rsplit('/', 1)[-1].rsplit(':', 1)[-1] d = json.loads(URLOpener().open('https://commons.wikimedia.org/w/api.php?action=query&titles=File:{}&prop=imageinfo&iiprop=url|user|extmetadata&iimetadataversion=latest&format=json'.format(file_name)).read().decode('utf-8')) @@ -91,6 +97,16 @@ def get_wikimedia_data(url): 'license_name': ext['LicenseShortName']['value'], } + # There are Wikidata links in ObjectName sametimes. Let's use it. + wikidata_match = re.search(r'wikidata\.org/wiki/(Q\d+)', meta['title']) + if wikidata_match is not None: + qitem = wikidata_match.group(1) + client = Client() + entity = client.get(qitem) + meta['title'] = entity.label.get('pl', str(entity.label)) + author = entity.get(client.get(WIKIDATA.CREATOR)) + meta['author'] = author.label.get('pl', str(author.label)) + if meta['license_name'] == 'Public domain': meta['license_name'] = 'domena publiczna' meta['license_url'] = 'https://pl.wikipedia.org/wiki/Domena_publiczna' -- 2.20.1