mirror of
https://github.com/Findus23/acronomy.git
synced 2024-09-19 15:33:45 +02:00
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
from io import BytesIO
|
|
from tempfile import TemporaryFile
|
|
|
|
import requests
|
|
from PIL import Image
|
|
from django.core.files.base import ContentFile
|
|
from django.db import models
|
|
|
|
from acros.utils.apis import requests_session
|
|
|
|
|
|
class Host(models.Model):
|
|
host = models.CharField(max_length=100)
|
|
icon = models.ImageField(upload_to="host_icons/", blank=True,
|
|
height_field="icon_height", width_field="icon_width")
|
|
icon_width = models.IntegerField(blank=True, null=True, editable=False)
|
|
icon_height = models.IntegerField(blank=True, null=True, editable=False)
|
|
fetched = models.BooleanField(default=False)
|
|
|
|
def __str__(self):
|
|
return self.host
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.fetched:
|
|
with TemporaryFile("rb+") as fd:
|
|
r = requests_session.get(f"https://external-content.duckduckgo.com/ip3/{self.host}.ico")
|
|
if r.ok:
|
|
filename = self.host + ".png"
|
|
for chunk in r.iter_content(chunk_size=128):
|
|
fd.write(chunk)
|
|
image = Image.open(fd)
|
|
image_io = BytesIO()
|
|
image.save(image_io, format='PNG', quality=9)
|
|
self.icon.save(filename, ContentFile(image_io.getvalue()), save=False)
|
|
else:
|
|
self.icon = None
|
|
self.fetched = True
|
|
|
|
super(Host, self).save(*args, **kwargs)
|