Handle some deprecations
[redakcja.git] / src / cover / utils.py
index 8e215db..8440e06 100644 (file)
@@ -5,15 +5,16 @@ import csv
 from io import StringIO
 import json
 import re
-from urllib.request import FancyURLopener
+from urllib.request import build_opener, install_opener, urlopen
+from django.conf import settings
+import requests
 from wikidata.client import Client
 from catalogue.constants import WIKIDATA
 
 
-class URLOpener(FancyURLopener):
-    @property
-    def version(self):
-        return 'FNP Redakcja'
+opener = build_opener()
+opener.addheaders = [("User-Agent", "WolneLektury-Redakcja/1.0 (fundacja@wolnelektury.pl)")]
+install_opener(opener)
 
 
 class FlickrError(Exception):
@@ -27,7 +28,7 @@ def get_flickr_data(url):
     author_slug, img_id = m.group('author'), m.group('img')
     base_url = "https://www.flickr.com/photos/%s/%s/" % (author_slug, img_id)
     try:
-        html = URLOpener().open(url).read().decode('utf-8')
+        html = urlopen(url).read().decode('utf-8')
     except IOError:
         raise FlickrError('Error reading page')
     match = re.search(r'<a href="([^"]*)"[^>]* rel="license ', html)
@@ -60,7 +61,7 @@ def get_flickr_data(url):
     m = re.search(r'modelExport: (\{.*\})', html)
     try:
         assert m
-        download_url = 'https:' + json.loads(m.group(1))['main']['photo-models'][0]['sizes']['o']['url']
+        download_url = 'https:' + json.loads(m.group(1))['main']['photo-models'][0]['data']['sizes']['data']['o']['data']['url']
     except (AssertionError, ValueError, IndexError, KeyError):
         raise FlickrError('Error reading image URL.')
     return {
@@ -74,16 +75,8 @@ def get_flickr_data(url):
 
 
 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 = json.loads(urlopen('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']
@@ -102,10 +95,16 @@ def get_wikimedia_data(url):
     if wikidata_match is not None:
         qitem = wikidata_match.group(1)
         client = Client()
+        client.opener.addheaders = [(
+            'User-Agent', 'Wolne Lektury Redakcja / Python-wikidata'
+        )]
         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 author is not None:
+            meta['author'] = author.label.get('pl', str(author.label))
+        else:
+            meta['author'] = ''
 
     if meta['license_name'] == 'Public domain':
         meta['license_name'] = 'domena publiczna'
@@ -116,15 +115,11 @@ def get_wikimedia_data(url):
 
 
 def get_mnw_data(url):
-    """
-    >>> 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(
         csv.DictReader(
             StringIO(
-                URLOpener().open(
+                urlopen(
                     'https://cyfrowe-api.mnw.art.pl/api/object/{}/csv'.format(nr)
                 ).read().decode('utf-8')
             )
@@ -132,29 +127,61 @@ def get_mnw_data(url):
     )[0]
 
     authors = []
-    i = 0
-    while f'authors.{i}.name' in d:
-        authors.append(d[f'authors.{i}.name'])
+    i = 1
+    while f'twórca/wytwórnia {i}' in d:
+        authors.append(d[f'twórca/wytwórnia {i}'])
         i += 1
 
-    license_url = d['copyrights.0.link']
-    license_name = d['copyrights.0.name']
+    license_url = ''
+    license_name = d['klasyfikacja praw autorskich 1']
     if license_name == 'DOMENA PUBLICZNA':
         license_name = 'domena publiczna'
         license_url = 'https://pl.wikipedia.org/wiki/Domena_publiczna'
         
     return {
-        'title': d['title'],
+        'title': d['nazwa/tytuł'],
         'author': ', '.join(authors),
         'source_url': url,
         'download_url': 'https://cyfrowe-cdn.mnw.art.pl/upload/multimedia/{}.{}'.format(
-            d['image.filePath'],
-            d['image.extension'],
+            d['ścieżka wizerunku'],
+            d['rozszerzenie pliku wizerunku'],
         ),
         'license_url': license_url,
         'license_name': license_name,
     }
 
+def get_rawpixel_data(url):
+    photo_id = re.search(r'/(\d+)/', url).group(1)
+
+    s = requests.Session()
+    cookies = settings.RAWPIXEL_SESSION
+
+    token = s.post(
+            'https://www.rawpixel.com/api/v1/user/session',
+            cookies=cookies
+            ).json()['token']
+
+    h = {'X-CSRF-Token': token, 'Accept': 'application/json'}
+
+    data = s.get(
+        f'https://www.rawpixel.com/api/v1/image/data/{photo_id}',
+        headers=h, cookies=cookies).json()
+    download_url = s.post(
+        f'https://www.rawpixel.com/api/v1/image/download/{photo_id}/original',
+        headers=h, cookies=cookies
+    ).json()['downloadUrl']
+
+    title = data['metadata']['title'].rsplit('|', 1)[0].strip()
+
+    return {
+        'title': title,
+        'author': ', '.join(data['metadata']['artist_names']),
+        'source_url': data['url'],
+        'download_url': download_url,
+        'license_url': data['metadata']['licenseUrl'],
+        'license_name': data['metadata']['license'],
+    }
+
 
 def get_import_data(url):
     if re.match(r'(https?://)?(www\.|secure\.)?flickr\.com/', url):
@@ -163,3 +190,5 @@ def get_import_data(url):
         return get_wikimedia_data(url)
     if re.match(r'(https?://)?cyfrowe\.mnw\.art\.pl/', url):
         return get_mnw_data(url)
+    if re.match(r'(https?://)?www\.rawpixel\.com/', url):
+        return get_rawpixel_data(url)