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'))
81 d = list(d['query']['pages'].values())[0]['imageinfo'][0]
82 ext = d['extmetadata']
85 'title': ext['ObjectName']['value'],
87 'source_url': d['descriptionurl'],
88 'download_url': d['url'],
89 'license_url': ext['LicenseUrl']['value'],
90 'license_name': ext['LicenseShortName']['value'],
96 def get_mnw_data(url):
98 >>> get_mnw_data('https://cyfrowe.mnw.art.pl/pl/katalog/794032')
99 {'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'}
102 nr = url.rsplit('/', 1)[-1]
107 'https://cyfrowe-api.mnw.art.pl/api/object/{}/csv'.format(nr)
108 ).read().decode('utf-8')
115 while f'authors.{i}.name' in d:
116 authors.append(d[f'authors.{i}.name'])
119 license_url = d['copyrights.0.link']
120 license_name = d['copyrights.0.name']
121 if license_name == 'DOMENA PUBLICZNA':
122 license_name = 'domena publiczna'
127 'author': ', '.join(authors),
129 'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/{}.{}'.format(
131 d['image.extension'],
133 'license_url': license_url,
134 'license_name': license_name,
138 def get_import_data(url):
139 if re.match(r'(https?://)?(www\.|secure\.)?flickr\.com/', url):
140 return get_flickr_data(url)
141 if re.match(r'(https?://)?(commons|upload)\.wikimedia\.org/', url):
142 return get_wikimedia_data(url)
143 if re.match(r'(https?://)?cyfrowe\.mnw\.art\.pl/', url):
144 return get_mnw_data(url)