From 303219dfb140383e9b9c8f800bba898b52cb6a55 Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Wed, 20 Nov 2024 18:56:19 -0400 Subject: [PATCH] clean up a bit --- daily-wallpaper.py => app/daily-wallpaper.py | 2 -- settings.py => app/settings.py | 3 +- providers/wikimedia.py | 34 ++++++++++++-------- pyproject.toml | 3 ++ 4 files changed, 24 insertions(+), 18 deletions(-) rename daily-wallpaper.py => app/daily-wallpaper.py (99%) rename settings.py => app/settings.py (97%) diff --git a/daily-wallpaper.py b/app/daily-wallpaper.py similarity index 99% rename from daily-wallpaper.py rename to app/daily-wallpaper.py index d8d5ea8..4c947cc 100644 --- a/daily-wallpaper.py +++ b/app/daily-wallpaper.py @@ -1,6 +1,5 @@ import datetime import os -import sys import requests import importlib import logging @@ -9,7 +8,6 @@ import settings import time import croniter - def main(): config = settings.load_settings() logging.basicConfig(level=config['general']['log_level'], format="%(asctime)s [%(levelname)s] %(message)s", force=True) diff --git a/settings.py b/app/settings.py similarity index 97% rename from settings.py rename to app/settings.py index 7546db8..3563706 100644 --- a/settings.py +++ b/app/settings.py @@ -1,9 +1,8 @@ -import logging import os.path import tomlkit local_path = os.path.abspath("config.toml") -user_path = os.path.expanduser("~/.config/daily-wallpaper/config.toml") +user_path = os.path.expanduser("~/.config/app/config.toml") def default_settings(): general = tomlkit.table() diff --git a/providers/wikimedia.py b/providers/wikimedia.py index dd038c8..2af8c7d 100644 --- a/providers/wikimedia.py +++ b/providers/wikimedia.py @@ -1,6 +1,7 @@ import datetime import logging -import re +import time + from providers._provider import Provider # https://api.wikimedia.org/wiki/Feed_API/Reference/Featured_content @@ -13,19 +14,24 @@ class Wikimedia(Provider): super().__init__(settings, session) def get_image_info(self): - today = datetime.datetime.now() - date = today.strftime('%Y/%m/%d') - logging.debug(f"Date: {date}") - url = 'https://api.wikimedia.org/feed/v1/wikipedia/en/featured/' + date - logging.debug(f"URL: {url}") + # Since wikipedia API seems to fail to provide image url, we will retry on key error until we get the image url + try: + today = datetime.datetime.now() + date = today.strftime('%Y/%m/%d') + logging.debug(f"Date: {date}") + url = 'https://api.wikimedia.org/feed/v1/wikipedia/en/featured/' + date + logging.debug(f"URL: {url}") - response = self.session.get(url).json() - # logging.debug(f"Response: {response}") + response = self.session.get(url).json() + # logging.debug(f"Response: {response}") - image = response['image']['image']['source'] - logging.debug(f"Image: {image}") - image_url = image + image = response['image']['image']['source'] + logging.debug(f"Image: {image}") + image_url = image - title = response['image']['description']['text'] - - return image_url, title + title = response['image']['description']['text'] + return image_url, title + except KeyError: + logging.error("KeyError, retrying...") + time.sleep(10) # Wait 10 seconds + return self.get_image_info() diff --git a/pyproject.toml b/pyproject.toml index 6586cd2..791699d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,9 @@ python-slugify = "^8.0" tomlkit = "^0.13.2" croniter = "^5.0" +[tool.poetry.scripts] +daily-wallpaper = "app.daily_wallpaper:__main__" + [build-system] requires = ["poetry-core"]