1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from io import StringIO
8 from urllib.request import FancyURLopener
10 class URLOpener(FancyURLopener):
16 class FlickrError(Exception):
20 def get_flickr_data(url):
21 m = re.match(r'(https?://)?(www\.|secure\.)?flickr\.com/photos/(?P<author>[^/]+)/(?P<img>\d+)/?', url)
23 raise FlickrError("It doesn't look like Flickr URL.")
24 author_slug, img_id = m.group('author'), m.group('img')
25 base_url = "https://www.flickr.com/photos/%s/%s/" % (author_slug, img_id)
27 html = URLOpener().open(url).read().decode('utf-8')
29 raise FlickrError('Error reading page')
30 match = re.search(r'<a href="([^"]*)"[^>]* rel="license ', html)
32 raise FlickrError('License not found.')
34 license_url = match.group(1)
35 re_license = re.compile(r'https?://creativecommons.org/licenses/([^/]*)/([^/]*)/.*')
36 m = re_license.match(license_url)
38 re_pd = re.compile(r'https?://creativecommons.org/publicdomain/([^/]*)/([^/]*)/.*')
39 m = re_pd.match(license_url)
41 raise FlickrError('License does not look like CC: %s' % license_url)
42 if m.group(1).lower() == 'zero':
43 license_name = 'Public domain (CC0 %s)' % m.group(2)
45 license_name = 'Public domain'
47 license_name = 'CC %s %s' % (m.group(1).upper(), m.group(2))
48 m = re.search(r'<a[^>]* class="owner-name [^>]*>([^<]*)<', html)
50 author = "%s@Flickr" % m.group(1)
52 raise FlickrError('Error reading author name.')
53 m = re.search(r'<h1[^>]*>(.*?)</h1>', html, re.S)
55 raise FlickrError('Error reading image title.')
56 title = m.group(1).strip()
57 m = re.search(r'modelExport: (\{.*\})', html)
60 download_url = 'https:' + json.loads(m.group(1))['main']['photo-models'][0]['sizes']['o']['url']
61 except (AssertionError, ValueError, IndexError, KeyError):
62 raise FlickrError('Error reading image URL.')
64 'source_url': base_url,
65 'license_url': license_url,
66 'license_name': license_name,
69 'download_url': download_url,
73 def get_wikimedia_data(url):
75 >>> get_wikimedia_data('https://commons.wikimedia.org/wiki/File:Valdai_IverskyMon_asv2018_img47.jpg')
76 {'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'}
79 file_name = url.rsplit('/', 1)[-1].rsplit(':', 1)[-1]
80 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'))
82 d = list(d['query']['pages'].values())[0]['imageinfo'][0]
83 ext = d['extmetadata']
86 'title': ext['ObjectName']['value'],
88 'source_url': d['descriptionurl'],
89 'download_url': d['url'],
90 'license_url': ext.get('LicenseUrl', {}).get('value', ''),
91 'license_name': ext['LicenseShortName']['value'],
94 if meta['license_name'] == 'Public domain':
95 meta['license_name'] = 'domena publiczna'
96 meta['license_url'] = 'https://pl.wikipedia.org/wiki/Domena_publiczna'
102 def get_mnw_data(url):
104 >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/511078')
105 {'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'}
107 nr = url.rsplit('/', 1)[-1]
112 'https://cyfrowe-api.mnw.art.pl/api/object/{}/csv'.format(nr)
113 ).read().decode('utf-8')
120 while f'authors.{i}.name' in d:
121 authors.append(d[f'authors.{i}.name'])
124 license_url = d['copyrights.0.link']
125 license_name = d['copyrights.0.name']
126 if license_name == 'DOMENA PUBLICZNA':
127 license_name = 'domena publiczna'
128 license_url = 'https://pl.wikipedia.org/wiki/Domena_publiczna'
132 'author': ', '.join(authors),
134 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/{}.{}'.format(
136 d['image.extension'],
138 'license_url': license_url,
139 'license_name': license_name,
143 def get_import_data(url):
144 if re.match(r'(https?://)?(www\.|secure\.)?flickr\.com/', url):
145 return get_flickr_data(url)
146 if re.match(r'(https?://)?(commons|upload)\.wikimedia\.org/', url):
147 return get_wikimedia_data(url)
148 if re.match(r'(https?://)?cyfrowe\.mnw\.art\.pl/', url):
149 return get_mnw_data(url)