Update images downloader to use requests
This commit is contained in:
parent
5447db6ff4
commit
b3cd4aab0b
|
@ -294,6 +294,7 @@ STATICFILES_FINDERS = (
|
||||||
"sass_processor.finders.CssFinder",
|
"sass_processor.finders.CssFinder",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
AWS_AUTO_CREATE_BUCKET = True
|
||||||
AWS_DEFAULT_ACL = "public-read"
|
AWS_DEFAULT_ACL = "public-read"
|
||||||
AWS_S3_SECURE_URLS = False
|
AWS_S3_SECURE_URLS = False
|
||||||
AWS_ACCESS_KEY_ID = env('BUCKET_ACCESS_KEY')
|
AWS_ACCESS_KEY_ID = env('BUCKET_ACCESS_KEY')
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import os
|
import os
|
||||||
|
import tempfile
|
||||||
import requests
|
import requests
|
||||||
import logging
|
import logging
|
||||||
from io import BytesIO
|
|
||||||
from time import mktime
|
from time import mktime
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from django.conf import settings
|
||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
from stdimage.utils import render_variations
|
from stdimage.utils import render_variations
|
||||||
from mhackspace.feeds.reader import fetch_feeds
|
from mhackspace.feeds.reader import fetch_feeds
|
||||||
|
@ -42,23 +43,35 @@ def remove_old_articles():
|
||||||
|
|
||||||
def download_remote_images():
|
def download_remote_images():
|
||||||
for article in Article.objects.all():
|
for article in Article.objects.all():
|
||||||
|
print(article.original_image)
|
||||||
if not article.original_image:
|
if not article.original_image:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
result = requests.get(article.original_image)
|
result = requests.get(article.original_image)
|
||||||
article.image.save(
|
|
||||||
os.path.basename(article.original_image.__str__()),
|
|
||||||
File(open(BytesIO(result.content), "rb")),
|
|
||||||
)
|
|
||||||
render_variations(result[0], image_variations, replace=True)
|
|
||||||
article.save()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(result)
|
|
||||||
logger.exception(result.status_code)
|
logger.exception(result.status_code)
|
||||||
logger.exception(
|
logger.exception(
|
||||||
"Unable to download remote image for %s"
|
"Unable to download remote image for %s"
|
||||||
% article.original_image
|
% article.original_image
|
||||||
)
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
tmpfile = tempfile.TemporaryFile(mode='w+b')
|
||||||
|
tmpfile.write(result.content)
|
||||||
|
|
||||||
|
article.image.save(
|
||||||
|
os.path.basename(article.original_image),
|
||||||
|
File(tmpfile),
|
||||||
|
)
|
||||||
|
|
||||||
|
file_path = f'{settings.MEDIA_ROOT}/{article.image.file}'
|
||||||
|
render_variations(file_path, image_variations, replace=True)
|
||||||
|
article.save()
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(result)
|
||||||
|
finally:
|
||||||
|
tmpfile.close()
|
||||||
|
|
||||||
|
|
||||||
def get_active_feeds(feed=False):
|
def get_active_feeds(feed=False):
|
||||||
|
|
Loading…
Reference in New Issue