From 9c5d78ac2c710937eb8e80363f0e91b6851215e2 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 5 Jun 2023 20:28:56 +0200 Subject: [PATCH] favicon base --- gawa/gawa/settings.py | 3 ++- gawa/start/admin.py | 2 +- gawa/start/models.py | 26 +++++++++++++++++++++++++- requirements.txt | 3 --- web/requirements.txt | 1 + 5 files changed, 29 insertions(+), 6 deletions(-) delete mode 100644 requirements.txt diff --git a/gawa/gawa/settings.py b/gawa/gawa/settings.py index 6e28cf0..4bb019b 100644 --- a/gawa/gawa/settings.py +++ b/gawa/gawa/settings.py @@ -15,7 +15,7 @@ import os # default django from pathlib import Path -from django.conf.global_settings import MEDIA_ROOT +from django.conf.global_settings import FILE_UPLOAD_TEMP_DIR, MEDIA_ROOT from django.utils.log import ServerFormatter # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -296,3 +296,4 @@ LOGGING = { #MEDIA_ROOT = "/home/plex/Documents/code/python/gawa/media" MEDIA_ROOT = "media/" MEDIA_URL = "media/" +FILE_UPLOAD_TEMP_DIR = "/tmp/gawa/upload" diff --git a/gawa/start/admin.py b/gawa/start/admin.py index 275e097..446cfb6 100644 --- a/gawa/start/admin.py +++ b/gawa/start/admin.py @@ -41,6 +41,6 @@ class LinkAdmin(admin.ModelAdmin): """ Admin Interface for Links """ - list_display = ["title_en", "title_de", "url", "suburl"] + list_display = ["title_en", "title_de", "url", "suburl", "favicon"] ordering = ['title_de', 'title_en'] actions = [regenerate] diff --git a/gawa/start/models.py b/gawa/start/models.py index f5a02f2..7d2ad85 100644 --- a/gawa/start/models.py +++ b/gawa/start/models.py @@ -1,11 +1,17 @@ -from enum import unique from django.db import models from django.db.models.options import override from django.utils.translation import gettext as _ +from django.core.files import File + +from django.conf import settings import logging logger = logging.getLogger(__name__) +import requests +import favicon +import random + class Keyword(models.Model): """ this is the model that should contain searchable keywords @@ -103,6 +109,24 @@ class Link(Searchable): """ logger.info(f"regenerating {self.__class__.__name__} object: {self}") self.suburl = f"/links#{self.title_en}" + logger.info(f"getting favicon for link object {self}: '{self.url}'") + icons = favicon.get(self.url, timeout=2) + if icons[0] is None: + # just keep whatever was stored if we cant get a new favicon + return + + response = requests.get(icons[0].url, stream=True) + filename: str = f"favicon-{random.randint(0x1000, 0xffff)}.{icons[0].format}" + logger.debug(response) + logger.debug(filename) + with open(f"{settings.FILE_UPLOAD_TEMP_DIR}/{filename}", 'wb') as image: + for chunk in response.iter_content(1024): + image.write(chunk) + logger.debug(image) + with open(f"{settings.FILE_UPLOAD_TEMP_DIR}/{filename}", 'rb') as image: + self.favicon.save(filename, File(image)) + + #self.favicon.save(f"favicon-{self.title_en}", favicon.get(self.url, timeout=2)) self.save() class Meta: diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index b0e22f7..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -Django>=3.0,<4.0 -psycopg2>=2.8 -mysqlclient>=1.4.3 diff --git a/web/requirements.txt b/web/requirements.txt index b15909d..8cb5b66 100644 --- a/web/requirements.txt +++ b/web/requirements.txt @@ -5,3 +5,4 @@ django_compressor>=2.2 django-libsass>=0.7 pillow>=9.0.0 colorlog>=6.7.0 +favicon>=0.7.0