1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 import urllib2 as urllib
8 from django.core.files.base import ContentFile
9 from django.core.management.base import NoArgsCommand
11 from cover.models import Image
12 from cover.utils import get_flickr_data, URLOpener, FlickrError
15 class Command(NoArgsCommand):
16 def handle_noargs(self, **options):
17 for image in Image.objects.exclude(book=None).order_by('id'):
19 if 'flickr.com' in image.source_url:
21 flickr_data = get_flickr_data(image.source_url)
22 except FlickrError as e:
23 print 'Flickr analysis failed: %s' % e
26 t = URLOpener().open(image.download_url).read()
27 except urllib.URLError:
28 print 'Broken download url'
30 print 'Connection failed'
32 image.download_url = flickr_data['download_url']
33 image.file.save(image.file.name, ContentFile(t))