Skip to content

Commit

Permalink
Improve favicon loading request now fetching first svgs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbarbier committed Oct 15, 2024
1 parent 296631e commit 3122163
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
34 changes: 18 additions & 16 deletions src/projects/signals.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
from django.utils import timezone
from django.db.models.signals import pre_save
from django.db.models.signals import post_save
from django.dispatch import receiver
from .tasks.fetch_favicon import fetch_favicon
from .models import Project

DELAY_REFRESH_FAVICON_SECONDS = 10 # Every 10 seconds
DELAY_REFRESH_FAVICON_SECONDS = 30 # Every 30 seconds

@receiver(pre_save, sender=Project)
def post_save_fetch_favicon(sender, instance=None, **kwargs):
@receiver(post_save, sender=Project)
def post_save_fetch_favicon(sender, instance, created, **kwargs):
favicon_need_refresh = False

# Fetch the existing instance from the database if it exists
if instance.pk:
try:
old_instance = Project.objects.get(pk=instance.pk)
# Check if the favicon value has changed
if old_instance.favicon != instance.favicon:
instance.favicon_task_status = 'SUCCESS'
instance.favicon_last_edited = timezone.now()
except Project.DoesNotExist:
# If the object does not exist in the database, it's a new object
favicon_need_refresh = True
else:
try:
old_instance = Project.objects.get(pk=instance.pk)
# Check if the favicon value has changed
if old_instance.favicon != instance.favicon:
instance.favicon_task_status = 'SUCCESS'
instance.favicon_last_edited = timezone.now()
except Project.DoesNotExist:
# If the object does not exist in the database, it's a new object
favicon_need_refresh = True

if created:
# If instance.pk is None, it's a new object
favicon_need_refresh = True

if instance.favicon_task_status == 'UNKNOWN':
favicon_need_refresh = True
if (timezone.now() - instance.favicon_last_edited).seconds > DELAY_REFRESH_FAVICON_SECONDS and instance.favicon_task_status != 'PENDING':

if (timezone.now() - instance.favicon_last_edited).seconds > DELAY_REFRESH_FAVICON_SECONDS:
favicon_need_refresh = True

if favicon_need_refresh:
instance.favicon_task_status = 'PENDING'
instance.favicon_last_edited = timezone.now()
fetch_favicon.delay(instance.pk, instance.url)
51 changes: 30 additions & 21 deletions src/projects/tasks/fetch_favicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,36 @@ def fetch_favicon(pk, url):
largest_favicon = None
largest_size = (0, 0)

for favicon_url in favicons:
try:
# Fetch the favicon image
favicon_response = requests.get(favicon_url, timeout=10)
favicon_response.raise_for_status()

# Open the image and get its size
image = Image.open(BytesIO(favicon_response.content))
width, height = image.size

# Update the largest favicon if this one is larger
if width * height > largest_size[0] * largest_size[1]:
largest_size = (width, height)
largest_favicon = {
'url': favicon_url,
'width': width,
'height': height,
}

except Exception as e:
print(f"Error fetching or processing {favicon_url}: {e}")
# If one url in favicon end with svg we keep this ad largest favicon
if any(favicon_url.endswith('.svg') for favicon_url in favicons):
largest_favicon = {
'url': next(favicon_url for favicon_url in favicons if favicon_url.endswith('.svg')),
'width': 0,
'height': 0,
}
print(f"Largest favicon found: {largest_favicon} (SVG)")
else:
for favicon_url in favicons:
try:
# Fetch the favicon image
favicon_response = requests.get(favicon_url, timeout=10)
favicon_response.raise_for_status()

# Open the image and get its size
image = Image.open(BytesIO(favicon_response.content))
width, height = image.size

# Update the largest favicon if this one is larger
if width * height > largest_size[0] * largest_size[1]:
largest_size = (width, height)
largest_favicon = {
'url': favicon_url,
'width': width,
'height': height,
}

except Exception as e:
print(f"Error fetching or processing {favicon_url}: {e}")

if largest_favicon:
print(f"Largest favicon found: {largest_favicon['url']} ({largest_favicon['width']}x{largest_favicon['height']})")
Expand Down

0 comments on commit 3122163

Please sign in to comment.