daily-wallpaper/app/main.py

97 lines
3.6 KiB
Python
Raw Permalink Normal View History

2024-11-20 16:34:20 -04:00
import datetime
2024-11-20 04:53:04 -04:00
import os
import requests
import importlib
import logging
2024-11-20 16:34:20 -04:00
import slugify
2024-11-20 18:22:46 -04:00
import time
import croniter
2024-11-21 00:57:43 -04:00
import win32gui
import win32con
2024-11-20 19:34:35 -04:00
from settings import settings
2024-11-20 16:34:20 -04:00
2024-11-20 04:53:04 -04:00
def main():
2024-11-20 16:34:20 -04:00
config = settings.load_settings()
2024-11-21 00:57:43 -04:00
log_path = os.path.abspath(os.path.expanduser(config['general']['location'] + "/log.txt"))
if config['general']['log']:
logging.basicConfig(filename=log_path, level=config['general']['log_level'], format="%(asctime)s [%(levelname)s] %(message)s")
else:
logging.basicConfig(level=config['general']['log_level'], format="%(asctime)s [%(levelname)s] %(message)s")
2024-11-20 16:34:20 -04:00
logging.debug(f"Config: {config}")
2024-11-20 18:22:46 -04:00
chosen_providers = config['general']['provider']
# Daemon mode
if config['daemon']['daemon']:
while True:
for provider in chosen_providers:
2024-11-21 00:57:43 -04:00
if provider == config['general']['provider'][0]:
download_with_provider(provider, config, set_wallpaper=True)
2024-11-20 18:22:46 -04:00
download_with_provider(provider, config)
if config['daemon']['cron'] != "":
now = datetime.datetime.now()
cron = croniter.croniter(config['daemon']['cron'], now)
next_run = cron.get_next(datetime.datetime)
logging.info(f"Next run: {next_run}")
time.sleep((next_run - now).total_seconds())
else:
time.sleep(config['daemon']['interval'])
# Download once
else:
for provider in chosen_providers:
2024-11-21 00:57:43 -04:00
if provider == config['general']['provider'][0]:
download_with_provider(provider, config, set_wallpaper=True)
2024-11-20 18:22:46 -04:00
download_with_provider(provider, config)
2024-11-21 00:57:43 -04:00
def download_with_provider(provider_name, config, set_wallpaper=False):
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
2024-11-20 18:22:46 -04:00
provider_settings = config[provider_name] if provider_name in config else None
2024-11-20 16:34:20 -04:00
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 18:22:46 -04:00
provider = importlib.import_module(f"providers.{provider_name}")
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}")
2024-11-20 18:22:46 -04:00
# Variables for the file path
date = datetime.datetime.now().strftime("%Y-%m-%d")
image_title = slugify.slugify(image_title)
file_path = f"{download_location}/{provider_name.title()}/{date} [{image_title}].jpg"
# Check if we should include the title in the filename
if not config['general']['include_title']:
file_path = f"{download_location}/{provider_name.title()}/{date}.jpg"
# Create the download location if it doesn't exist
2024-11-20 04:53:04 -04:00
if not os.path.exists(download_location):
2024-11-21 00:57:43 -04:00
logging.info(f"Creating download location: {download_location}")
2024-11-20 04:53:04 -04:00
os.mkdir(download_location)
2024-11-20 16:34:20 -04:00
if not os.path.exists(f"{download_location}/{provider_name.title()}"):
2024-11-21 00:57:43 -04:00
logging.info(f"Creating provider location: {download_location}/{provider_name.title()}")
2024-11-20 16:34:20 -04:00
os.mkdir(f"{download_location}/{provider_name.title()}")
2024-11-20 18:22:46 -04:00
# Check if the file exists and if we should overwrite it
if os.path.exists(file_path) and not config['general']['overwrite']:
2024-11-21 00:57:43 -04:00
logging.info(f"File exists, skipping: {file_path}")
2024-11-20 18:22:46 -04:00
return
2024-11-20 16:34:20 -04:00
2024-11-20 18:22:46 -04:00
# Download the image
2024-11-21 00:57:43 -04:00
logging.info(f"Downloading image: {image_title}")
2024-11-20 18:22:46 -04:00
image = session.get(image_url).content
2024-11-21 00:57:43 -04:00
logging.info(f"Saving file: {file_path}")
2024-11-20 18:22:46 -04:00
with open(file_path, "wb") as file:
2024-11-20 04:53:04 -04:00
file.write(image)
2024-11-21 00:57:43 -04:00
if config['general']['set_wallpaper'] and set_wallpaper:
logging.info("Setting wallpaper")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, file_path, win32con.SPIF_SENDCHANGE)
2024-11-20 04:53:04 -04:00
if __name__ == "__main__":
main()