Compare commits

..

6 Commits

Author SHA1 Message Date
8ab838839a Try to figure out build system? 2024-11-21 01:41:46 -04:00
ef3aa58fe9 Add somewhat working build system 2024-11-21 01:10:58 -04:00
107e71189c Add some more features 2024-11-21 00:57:43 -04:00
1ab4e8a813 Add todo for later 2024-11-20 19:42:02 -04:00
d4011ee9ec Weird packaging thing 2024-11-20 19:34:35 -04:00
1762843ba4 Fix refactor mistake 2024-11-20 18:59:46 -04:00
7 changed files with 80 additions and 20 deletions

View File

@ -9,4 +9,13 @@ Unsplash Applications
- https://unsplash.com/oauth/applications - https://unsplash.com/oauth/applications
Wikipedia API Docs Wikipedia API Docs
- https://api.wikimedia.org/wiki/Getting_featured_content_from_Wikipedia_with_Python#Today's_featured_article - https://api.wikimedia.org/wiki/Getting_featured_content_from_Wikipedia_with_Python#Today's_featured_article
# TODO
- [ ] Add more providers
- [ ] Deal with existing todos in code
- [ ] Add more error handling
- [x] Make logging to file work
- [ ] Add tests
- [ ] Add more documentation
- [x] Add auto setting of wallpaper

View File

@ -4,13 +4,20 @@ import requests
import importlib import importlib
import logging import logging
import slugify import slugify
import settings
import time import time
import croniter import croniter
import win32gui
import win32con
from settings import settings
def main(): def main():
config = settings.load_settings() 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}") logging.debug(f"Config: {config}")
chosen_providers = config['general']['provider'] chosen_providers = config['general']['provider']
@ -19,6 +26,8 @@ def main():
if config['daemon']['daemon']: if config['daemon']['daemon']:
while True: while True:
for provider in chosen_providers: for provider in chosen_providers:
if provider == config['general']['provider'][0]:
download_with_provider(provider, config, set_wallpaper=True)
download_with_provider(provider, config) download_with_provider(provider, config)
if config['daemon']['cron'] != "": if config['daemon']['cron'] != "":
now = datetime.datetime.now() now = datetime.datetime.now()
@ -31,9 +40,11 @@ def main():
# Download once # Download once
else: else:
for provider in chosen_providers: for provider in chosen_providers:
if provider == config['general']['provider'][0]:
download_with_provider(provider, config, set_wallpaper=True)
download_with_provider(provider, config) 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 = requests.Session()
session.headers.update({ session.headers.update({
"User-Agent": config['general']['user_agent'] "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" file_path = f"{download_location}/{provider_name.title()}/{date}.jpg"
# Create the download location if it doesn't exist # Create the download location if it doesn't exist
if not os.path.exists(download_location): if not os.path.exists(download_location):
logging.info(f"Creating download location: {download_location}")
os.mkdir(download_location) os.mkdir(download_location)
if not os.path.exists(f"{download_location}/{provider_name.title()}"): 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()}") os.mkdir(f"{download_location}/{provider_name.title()}")
# Check if the file exists and if we should overwrite it # Check if the file exists and if we should overwrite it
if os.path.exists(file_path) and not config['general']['overwrite']: if os.path.exists(file_path) and not config['general']['overwrite']:
logging.info(f"File exists, skipping: {file_path}")
return return
# Download the image # Download the image
logging.info(f"Downloading image: {image_title}")
image = session.get(image_url).content image = session.get(image_url).content
logging.info(f"Saving file: {file_path}")
with open(file_path, "wb") as file: with open(file_path, "wb") as file:
file.write(image) 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__": if __name__ == "__main__":
main() main()

29
poetry.lock generated
View File

@ -196,6 +196,33 @@ files = [
{file = "pytz-2024.2.tar.gz", hash = "sha256:2aa355083c50a0f93fa581709deac0c9ad65cca8a9e9beac660adcbd493c798a"}, {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]] [[package]]
name = "requests" name = "requests"
version = "2.32.3" version = "2.32.3"
@ -269,4 +296,4 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "65c5d981b2146aee4317e6d04fc32e949094e70be5dd7a26f44c76614a92ee33" content-hash = "7bc68de8c28b8a00a3e83ae30a243333e6e6339b159f0e5593e8325659cb57ae"

View File

@ -25,7 +25,8 @@ class Unsplash(Provider):
image_slug = matches.group(1) image_slug = matches.group(1)
logging.debug(f"Image slug: {image_slug}") 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}") logging.debug(f"Image ID: {image_id}")
title = image_slug.replace("-", " ").replace(image_id, "").strip().title() title = image_slug.replace("-", " ").replace(image_id, "").strip().title()

View File

@ -1,9 +1,14 @@
[tool.poetry] [tool.poetry]
name = "daily-wallpaper" name = "DailyWallpaper"
version = "0.1.0" version = "0.1.0"
description = "" description = ""
authors = ["Isaac Shoebottom <ir.shoebottom@gmail.com>"] authors = ["Isaac Shoebottom <ir.shoebottom@gmail.com>"]
readme = "README.md" readme = "README.md"
packages = [
{ include = "providers"},
{ include = "settings"},
{ include = "app"}
]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.11" python = "^3.11"
@ -12,10 +17,19 @@ urllib3 = "^1.26"
python-slugify = "^8.0" python-slugify = "^8.0"
tomlkit = "^0.13.2" tomlkit = "^0.13.2"
croniter = "^5.0" croniter = "^5.0"
pywin32 = "^308"
[tool.poetry.scripts] [tool.poetry.scripts]
daily-wallpaper = "app.daily_wallpaper:__main__" DailyWallpaper = "app.main:main"
# https://pypi.org/project/poetry-pyinstaller-plugin/
# https://stackoverflow.com/a/78050613
[tool.poetry-pyinstaller-plugin.package]
providers = "."
settings = "."
[tool.poetry-pyinstaller-plugin.scripts]
DailyWallpaper = { source = "app/main.py", type = "onefile", bundle = false }
[build-system] [build-system]
requires = ["poetry-core"] requires = ["poetry-core"]

0
settings/__init__.py Normal file
View File

View File

@ -2,7 +2,7 @@ import os.path
import tomlkit import tomlkit
local_path = os.path.abspath("config.toml") local_path = os.path.abspath("config.toml")
user_path = os.path.expanduser("~/.config/app/config.toml") user_path = os.path.expanduser("~/.config/daily-wallpaper/config.toml")
def default_settings(): def default_settings():
general = tomlkit.table() general = tomlkit.table()
@ -67,17 +67,6 @@ def default_settings():
return defaults return defaults
def load_settings(): 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): if os.path.exists(local_path):
settings = tomlkit.parse(open(local_path, mode='r').read()) settings = tomlkit.parse(open(local_path, mode='r').read())
elif os.path.exists(user_path): elif os.path.exists(user_path):