daily-wallpaper/providers/unsplash.py

37 lines
1.1 KiB
Python
Raw Normal View History

2024-11-20 04:53:04 -04:00
# Using official Unsplash API to get images
# curl https://api.unsplash.com/collections/1459961/photos
# But we will scrape >:)
import logging
import re
2024-11-20 16:34:20 -04:00
from providers._provider import Provider
2024-11-20 04:53:04 -04:00
2024-11-20 16:34:20 -04:00
class Unsplash(Provider):
2024-11-20 04:53:04 -04:00
name = "Unsplash"
url = "https://unsplash.com/collections/1459961/photo-of-the-day-(archive)"
def __init__(self, settings, session):
super().__init__(settings, session)
2024-11-20 16:34:20 -04:00
def get_image_info(self):
2024-11-20 18:22:46 -04:00
query = f"https://unsplash.com/collections/{self.settings['collection']}"
2024-11-20 04:53:04 -04:00
logging.debug(f"Query: {query}")
response = self.session.get(query).text
# logging.debug(f"Response: {response}")
regex = r"href=\"\/photos\/(.*?)\""
matches = re.search(regex, response)
logging.debug(f"Matches: {matches}")
image_slug = matches.group(1)
logging.debug(f"Image slug: {image_slug}")
2024-11-21 00:57:43 -04:00
# Last 11 characters are the image ID
image_id = image_slug[-11:]
2024-11-20 04:53:04 -04:00
logging.debug(f"Image ID: {image_id}")
2024-11-20 16:34:20 -04:00
title = image_slug.replace("-", " ").replace(image_id, "").strip().title()
2024-11-20 04:53:04 -04:00
image_url = f"https://unsplash.com/photos/{image_id}/download"
logging.debug(f"Image URL: {image_url}")
2024-11-20 16:34:20 -04:00
return image_url, title