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):
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)
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 {
def get_wikimedia_data(url):
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']
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))
d = list(
csv.DictReader(
StringIO(
- URLOpener().open(
+ urlopen(
'https://cyfrowe-api.mnw.art.pl/api/object/{}/csv'.format(nr)
).read().decode('utf-8')
)