X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/52d79280796679d28fb526063c77c43851ae52c4..df3e7b60880c31eda0abdedd0eb6bd8b54a8e364:/src/cover/utils.py?ds=sidebyside diff --git a/src/cover/utils.py b/src/cover/utils.py index 3bf3e9f9..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,9 +78,13 @@ 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')) + d = list(d['query']['pages'].values())[0]['imageinfo'][0] ext = d['extmetadata'] @@ -86,18 +93,32 @@ def get_wikimedia_data(url): 'author': d['user'], 'source_url': d['descriptionurl'], 'download_url': d['url'], - 'license_url': ext['LicenseUrl']['value'], + 'license_url': ext.get('LicenseUrl', {}).get('value', ''), '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' + + return meta def get_mnw_data(url): """ - >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/794032') - {'title': 'Pejzaż z podwójnym świerkiem', 'author': 'nieznany, Altdorfer, Albrecht (ca 1480-1538)', 'source_url': 'https://cyfrowe.mnw.art.pl/pl/katalog/794032', 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/49/58/49583b3e9b23e2d25f372fe6021ae220.jpg', 'license_url': 'https://pl.wikipedia.org/wiki/Domena_publiczna', 'license_name': 'DOMENA PUBLICZNA'} - + >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/511078') + {'title': 'Chłopka (Baba ukraińska)', 'author': 'Krzyżanowski, Konrad (1872-1922)', 'source_url': 'https://cyfrowe.mnw.art.pl/pl/katalog/511078', 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/32/60/3260ae1704cc530cc62befa9b7d58cbd.jpg', 'license_url': 'https://pl.wikipedia.org/wiki/Domena_publiczna', 'license_name': 'domena publiczna'} """ nr = url.rsplit('/', 1)[-1] d = list( @@ -120,7 +141,7 @@ def get_mnw_data(url): license_name = d['copyrights.0.name'] if license_name == 'DOMENA PUBLICZNA': license_name = 'domena publiczna' - license_url = '' + license_url = 'https://pl.wikipedia.org/wiki/Domena_publiczna' return { 'title': d['title'],