daily-wallpaper/daily-wallpaper.py

50 lines
1.5 KiB
Python
Raw Normal View History

2024-11-20 16:34:20 -04:00
import datetime
2024-11-20 04:53:04 -04:00
import os
import sys
import requests
import importlib
import logging
2024-11-20 16:34:20 -04:00
import slugify
import settings
2024-11-20 04:53:04 -04:00
def main():
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
2024-11-20 16:34:20 -04:00
config = settings.load_settings()
logging.debug(f"Config: {config}")
2024-11-20 04:53:04 -04:00
session = requests.Session()
session.headers.update({
2024-11-20 16:34:20 -04:00
"User-Agent": config['general']['user_agent']
2024-11-20 04:53:04 -04:00
})
2024-11-20 16:34:20 -04:00
# Convenience variables, could be inlined
provider_name = config['general']['provider']
provider_settings = config[config['general']['provider']]
download_location = os.path.abspath(os.path.expanduser(config['general']['location']))
2024-11-20 04:53:04 -04:00
# Load the provider module
2024-11-20 16:34:20 -04:00
provider = importlib.import_module(f"providers.{config['general']['provider']}")
2024-11-20 04:53:04 -04:00
# Create an instance of the provider
2024-11-20 16:34:20 -04:00
provider_obj = getattr(provider, provider_name.title())(provider_settings, session)
# Get the image URL and title
image_url, image_title = provider_obj.get_image_info()
2024-11-20 04:53:04 -04:00
logging.debug(f"Image URL: {image_url}")
# Download the image
image = session.get(image_url).content
if not os.path.exists(download_location):
os.mkdir(download_location)
2024-11-20 16:34:20 -04:00
if not os.path.exists(f"{download_location}/{provider_name.title()}"):
os.mkdir(f"{download_location}/{provider_name.title()}")
date = datetime.datetime.now().strftime("%Y-%m-%d")
image_title = slugify.slugify(image_title)
with open(f"{download_location}/{provider_name.title()}/{date} [{image_title}].jpg", "wb") as file:
2024-11-20 04:53:04 -04:00
file.write(image)
if __name__ == "__main__":
main()