Add some more features
This commit is contained in:
24
app/main.py
24
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()
|
Reference in New Issue
Block a user