Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.10.1 again #60

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/projects/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,7 @@

@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
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:
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)
105 changes: 60 additions & 45 deletions src/projects/tasks/fetch_favicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from urllib.parse import urljoin, urlparse
from PIL import Image
from io import BytesIO
from django.utils import timezone
from projects.models import Project

@shared_task()
def fetch_favicon(pk, url):
Expand Down Expand Up @@ -32,55 +34,68 @@ def fetch_favicon(pk, url):
largest_size = (0, 0)

# 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}")
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 no image found, we try to get the first svg found
if not 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,
}

if largest_favicon:
print(f"Largest favicon found: {largest_favicon['url']} ({largest_favicon['width']}x{largest_favicon['height']})")
# Fetch the content of the favicon
response = requests.get(largest_favicon['url'])
response.raise_for_status() # Ensure the request was successful

# Wrap the content in a BytesIO object
favicon_content = BytesIO(response.content)

# Get the Project instance
project = Project.objects.get(pk=pk)
# Save the favicon to the project's ImageField or FileField
project.favicon.save(largest_favicon['url'].split('/')[-1], favicon_content)
project.favicon_task_status = 'SUCCESS'
project.save()
else:
print("No favicon found.")
# Get the Project instance
project = Project.objects.get(pk=pk)
project.favicon_last_edited = timezone.now()
project.favicon_task_status = 'FAILURE'
project.save()

from projects.models import Project

# Fetch the content of the favicon
response = requests.get(largest_favicon['url'])
response.raise_for_status() # Ensure the request was successful

# Wrap the content in a BytesIO object
favicon_content = BytesIO(response.content)

# Get the Project instance
project = Project.objects.get(pk=pk)

# Save the favicon to the project's ImageField or FileField
project.favicon.save(largest_favicon['url'].split('/')[-1], favicon_content)

except requests.RequestException as e:
except e:
print(f"Error fetching the webpage: {e}")
try:
# Get the Project instance
project = Project.objects.get(pk=pk)
project.favicon_last_edited = timezone.now()
project.favicon_task_status = 'FAILURE'
project.save()
except e:
print(f"Error editing the project: {e}")

2 changes: 1 addition & 1 deletion src/projects/tasks/refresh_favicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def revoke_tasks(tasks):
projects = Project.objects.filter(
favicon_last_edited__lt=six_hours_ago,
).exclude(
favicon_task_status='QUEUED' # Exclude 'QUEUED' status
favicon_task_status='PENDING' # Exclude 'PENDING' status
)
print(f'Found {projects.count()} projects to refresh favicon.')
for project in projects:
Expand Down
Loading