From 2295af1947e8fe5777120462ea174ec1b044ebdb Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Tue, 23 May 2017 12:27:14 +0200 Subject: [PATCH] fix/refactor refresh covers --- .../management/commands/refresh_covers.py | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/cover/management/commands/refresh_covers.py b/apps/cover/management/commands/refresh_covers.py index 1097a754..cc0ef31c 100644 --- a/apps/cover/management/commands/refresh_covers.py +++ b/apps/cover/management/commands/refresh_covers.py @@ -20,28 +20,29 @@ class Command(BaseCommand): def handle(self, *args, **options): from_id = options.get('from_id', 1) - for image in Image.objects.filter(id__gte=from_id).exclude(book=None).order_by('id'): - if image.source_url and 'flickr.com' in image.source_url and not image.download_url.endswith('_o.jpg'): - print image.id + images = Image.objects.filter(id__gte=from_id).exclude(book=None).order_by('id') + images = images.filter(source_url__contains='flickr.com').exclude(download_url__endswith='_o.jpg') + for image in images: + print image.id + try: + flickr_data = get_flickr_data(image.source_url) + print flickr_data + except FlickrError as e: + print 'Flickr analysis failed: %s' % e + else: + flickr_url = flickr_data['download_url'] + if flickr_url != image.download_url: + same_url = Image.objects.filter(download_url=flickr_url) + if same_url: + print 'Download url already present in image %s' % same_url.get().id + continue try: - flickr_data = get_flickr_data(image.source_url) - print flickr_data - except FlickrError as e: - print 'Flickr analysis failed: %s' % e + t = URLOpener().open(flickr_url).read() + except urllib.URLError: + print 'Broken download url' + except IOError: + print 'Connection failed' else: - flickr_url = flickr_data['download_url'] - if flickr_url != image.download_url: - same_url = Image.objects.filter(download_url=flickr_url) - if same_url: - print 'Download url already present in image %s' % same_url.get().id - continue - try: - t = URLOpener().open(flickr_url).read() - except urllib.URLError: - print 'Broken download url' - except IOError: - print 'Connection failed' - else: - image.download_url = flickr_url - image.file.save(image.file.name, ContentFile(t)) - image.save() + image.download_url = flickr_url + image.file.save(image.file.name, ContentFile(t)) + image.save() -- 2.20.1