diff --git a/infomate/settings.py b/infomate/settings.py index f94b332..5d22dbf 100644 --- a/infomate/settings.py +++ b/infomate/settings.py @@ -6,7 +6,7 @@ from random import random import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration -DEBUG = True +DEBUG = os.getenv("DEBUG", True) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = "wow so secret" @@ -103,6 +103,9 @@ AUTH_FAILED_REDIRECT_URL = "https://vas3k.ru/auth/login/" SENTRY_DSN = None +MEDIA_UPLOAD_URL = "https://i.vas3k.ru/upload/" +MEDIA_UPLOAD_CODE = None # should be set in private_settings.py + try: # poor mans' private settings from infomate.private_settings import * diff --git a/scripts/initialize.py b/scripts/initialize.py index 34605d4..0bdab36 100644 --- a/scripts/initialize.py +++ b/scripts/initialize.py @@ -1,6 +1,9 @@ import os import sys import django + +from utils.images import upload_image_from_url + BASE_DIR = os.path.join(os.path.dirname(__file__), "..") sys.path.append(BASE_DIR) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "infomate.settings") @@ -133,6 +136,8 @@ def initialize(config, board_slug): if not icon: icon = find_favicon(feed_url, html) print(f"- found favicon: {icon}") + icon = upload_image_from_url(icon) + print(f"- uploaded favicon: {icon}") feed.icon = icon diff --git a/utils/images.py b/utils/images.py new file mode 100644 index 0000000..e1505e9 --- /dev/null +++ b/utils/images.py @@ -0,0 +1,49 @@ +import io +import logging +import os +from urllib.parse import urlparse + +import requests +from django.conf import settings + +log = logging.getLogger(__name__) + + +def upload_image_from_url(url): + if not url: + return None + + image_name = os.path.basename(urlparse(url).path) + if "." not in image_name: + image_name += ".jpg" + + try: + image_data = io.BytesIO(requests.get(url).content) + except requests.exceptions.RequestException: + return None + + try: + uploaded = requests.post( + url=settings.MEDIA_UPLOAD_URL, + params={ + "code": settings.MEDIA_UPLOAD_CODE + }, + files={ + "media": (image_name, image_data) + } + ) + except requests.exceptions.RequestException: + return None + finally: + image_data.close() + + if 200 <= uploaded.status_code <= 299: + try: + response_data = uploaded.json() + except Exception as ex: + log.exception(f"Error uploading avatar: {ex}") + return None + + return response_data["uploaded"][0] + + return None