devine
devine copied to clipboard
Feature: Add Attachment class with URL support and download functionality
feat(Attachment): Add URL support and download functionality to Attachment class
Description
This PR introduces several enhancements to the Attachment class within the devine/core/tracks/attachment.py file:
URL Support:
- Added the ability to create an
Attachmentfrom a URL. This includes downloading the file to a temporary directory if a URL is provided. - Added
urlandsessionparameters to the constructor. - Added a new class method
from_urlto streamline the creation of attachments from URLs.
Mime Type Handling:
- Expanded MIME type support to include
.jpg,.jpeg, and.pngfile extensions.
Example Code in Service.
if title.data["info"]["poster"]:
poster_url = title.data["info"]["poster"]
try:
thumbnail_name = title.data["meta"]["title"]["en"] + " thumbnail"
except (KeyError, TypeError):
thumbnail_name = "thumbnail"
try:
thumbnail_attachment = Attachment.from_url(
url=poster_url,
name=thumbnail_name,
mime_type="image/jpeg",
description="Thumbnail",
session=self.session,
)
tracks.attachments.append(thumbnail_attachment)
except Exception as e:
self.log.warning(f"Failed to download thumbnail: {e}")