From 107e71189cbf21bd55c9a116539868c7bc1b906b Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Thu, 21 Nov 2024 00:57:43 -0400 Subject: [PATCH] Add some more features --- README.md | 4 ++-- app/main.py | 24 ++++++++++++++++++++++-- poetry.lock | 29 ++++++++++++++++++++++++++++- providers/unsplash.py | 3 ++- pyproject.toml | 1 + settings/settings.py | 11 ----------- 6 files changed, 55 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 51cfd47..fd263b9 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Wikipedia API Docs - [ ] Add more providers - [ ] Deal with existing todos in code - [ ] Add more error handling -- [ ] Make logging to file work +- [x] Make logging to file work - [ ] Add tests - [ ] Add more documentation -- [ ] Add auto setting of wallpaper \ No newline at end of file +- [x] Add auto setting of wallpaper \ No newline at end of file diff --git a/app/main.py b/app/main.py index 4f242ce..9d022ae 100644 --- a/app/main.py +++ b/app/main.py @@ -6,11 +6,18 @@ import logging import slugify import time import croniter +import win32gui +import win32con from settings import settings def main(): config = settings.load_settings() - logging.basicConfig(level=config['general']['log_level'], format="%(asctime)s [%(levelname)s] %(message)s", force=True) + 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") + logging.debug(f"Config: {config}") chosen_providers = config['general']['provider'] @@ -19,6 +26,8 @@ def main(): if config['daemon']['daemon']: while True: for provider in chosen_providers: + if provider == config['general']['provider'][0]: + download_with_provider(provider, config, set_wallpaper=True) download_with_provider(provider, config) if config['daemon']['cron'] != "": now = datetime.datetime.now() @@ -31,9 +40,11 @@ def main(): # Download once else: for provider in chosen_providers: + if provider == config['general']['provider'][0]: + download_with_provider(provider, config, set_wallpaper=True) download_with_provider(provider, config) -def download_with_provider(provider_name, config): +def download_with_provider(provider_name, config, set_wallpaper=False): session = requests.Session() session.headers.update({ "User-Agent": config['general']['user_agent'] @@ -61,17 +72,26 @@ def download_with_provider(provider_name, config): file_path = f"{download_location}/{provider_name.title()}/{date}.jpg" # Create the download location if it doesn't exist if not os.path.exists(download_location): + logging.info(f"Creating download location: {download_location}") os.mkdir(download_location) if not os.path.exists(f"{download_location}/{provider_name.title()}"): + logging.info(f"Creating provider location: {download_location}/{provider_name.title()}") os.mkdir(f"{download_location}/{provider_name.title()}") # Check if the file exists and if we should overwrite it if os.path.exists(file_path) and not config['general']['overwrite']: + logging.info(f"File exists, skipping: {file_path}") return # Download the image + logging.info(f"Downloading image: {image_title}") image = session.get(image_url).content + logging.info(f"Saving file: {file_path}") with open(file_path, "wb") as file: file.write(image) + if config['general']['set_wallpaper'] and set_wallpaper: + logging.info("Setting wallpaper") + win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, file_path, win32con.SPIF_SENDCHANGE) + if __name__ == "__main__": main() \ No newline at end of file diff --git a/poetry.lock b/poetry.lock index 76a791d..561e0c1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -196,6 +196,33 @@ files = [ {file = "pytz-2024.2.tar.gz", hash = "sha256:2aa355083c50a0f93fa581709deac0c9ad65cca8a9e9beac660adcbd493c798a"}, ] +[[package]] +name = "pywin32" +version = "308" +description = "Python for Window Extensions" +optional = false +python-versions = "*" +files = [ + {file = "pywin32-308-cp310-cp310-win32.whl", hash = "sha256:796ff4426437896550d2981b9c2ac0ffd75238ad9ea2d3bfa67a1abd546d262e"}, + {file = "pywin32-308-cp310-cp310-win_amd64.whl", hash = "sha256:4fc888c59b3c0bef905ce7eb7e2106a07712015ea1c8234b703a088d46110e8e"}, + {file = "pywin32-308-cp310-cp310-win_arm64.whl", hash = "sha256:a5ab5381813b40f264fa3495b98af850098f814a25a63589a8e9eb12560f450c"}, + {file = "pywin32-308-cp311-cp311-win32.whl", hash = "sha256:5d8c8015b24a7d6855b1550d8e660d8daa09983c80e5daf89a273e5c6fb5095a"}, + {file = "pywin32-308-cp311-cp311-win_amd64.whl", hash = "sha256:575621b90f0dc2695fec346b2d6302faebd4f0f45c05ea29404cefe35d89442b"}, + {file = "pywin32-308-cp311-cp311-win_arm64.whl", hash = "sha256:100a5442b7332070983c4cd03f2e906a5648a5104b8a7f50175f7906efd16bb6"}, + {file = "pywin32-308-cp312-cp312-win32.whl", hash = "sha256:587f3e19696f4bf96fde9d8a57cec74a57021ad5f204c9e627e15c33ff568897"}, + {file = "pywin32-308-cp312-cp312-win_amd64.whl", hash = "sha256:00b3e11ef09ede56c6a43c71f2d31857cf7c54b0ab6e78ac659497abd2834f47"}, + {file = "pywin32-308-cp312-cp312-win_arm64.whl", hash = "sha256:9b4de86c8d909aed15b7011182c8cab38c8850de36e6afb1f0db22b8959e3091"}, + {file = "pywin32-308-cp313-cp313-win32.whl", hash = "sha256:1c44539a37a5b7b21d02ab34e6a4d314e0788f1690d65b48e9b0b89f31abbbed"}, + {file = "pywin32-308-cp313-cp313-win_amd64.whl", hash = "sha256:fd380990e792eaf6827fcb7e187b2b4b1cede0585e3d0c9e84201ec27b9905e4"}, + {file = "pywin32-308-cp313-cp313-win_arm64.whl", hash = "sha256:ef313c46d4c18dfb82a2431e3051ac8f112ccee1a34f29c263c583c568db63cd"}, + {file = "pywin32-308-cp37-cp37m-win32.whl", hash = "sha256:1f696ab352a2ddd63bd07430080dd598e6369152ea13a25ebcdd2f503a38f1ff"}, + {file = "pywin32-308-cp37-cp37m-win_amd64.whl", hash = "sha256:13dcb914ed4347019fbec6697a01a0aec61019c1046c2b905410d197856326a6"}, + {file = "pywin32-308-cp38-cp38-win32.whl", hash = "sha256:5794e764ebcabf4ff08c555b31bd348c9025929371763b2183172ff4708152f0"}, + {file = "pywin32-308-cp38-cp38-win_amd64.whl", hash = "sha256:3b92622e29d651c6b783e368ba7d6722b1634b8e70bd376fd7610fe1992e19de"}, + {file = "pywin32-308-cp39-cp39-win32.whl", hash = "sha256:7873ca4dc60ab3287919881a7d4f88baee4a6e639aa6962de25a98ba6b193341"}, + {file = "pywin32-308-cp39-cp39-win_amd64.whl", hash = "sha256:71b3322d949b4cc20776436a9c9ba0eeedcbc9c650daa536df63f0ff111bb920"}, +] + [[package]] name = "requests" version = "2.32.3" @@ -269,4 +296,4 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "65c5d981b2146aee4317e6d04fc32e949094e70be5dd7a26f44c76614a92ee33" +content-hash = "7bc68de8c28b8a00a3e83ae30a243333e6e6339b159f0e5593e8325659cb57ae" diff --git a/providers/unsplash.py b/providers/unsplash.py index ff8394a..d70cd4c 100644 --- a/providers/unsplash.py +++ b/providers/unsplash.py @@ -25,7 +25,8 @@ class Unsplash(Provider): image_slug = matches.group(1) logging.debug(f"Image slug: {image_slug}") - image_id = image_slug.split("-")[-1] + # Last 11 characters are the image ID + image_id = image_slug[-11:] logging.debug(f"Image ID: {image_id}") title = image_slug.replace("-", " ").replace(image_id, "").strip().title() diff --git a/pyproject.toml b/pyproject.toml index 2ff6d7a..30a1794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ urllib3 = "^1.26" python-slugify = "^8.0" tomlkit = "^0.13.2" croniter = "^5.0" +pywin32 = "^308" [tool.poetry.scripts] DailyWallpaper = "app.main:main" diff --git a/settings/settings.py b/settings/settings.py index d0626e6..3aa91b7 100644 --- a/settings/settings.py +++ b/settings/settings.py @@ -67,17 +67,6 @@ def default_settings(): return defaults def load_settings(): - # TODO: Find a better way to do this - if os.environ.get("RAPID_DEVELOPMENT") is not None: - if os.path.exists(user_path): - os.remove(user_path) - if os.path.exists(local_path): - os.remove(local_path) - settings = default_settings() - with open(local_path, mode='w') as file: - tomlkit.dump(settings, file) - return settings - if os.path.exists(local_path): settings = tomlkit.parse(open(local_path, mode='r').read()) elif os.path.exists(user_path):