diff --git a/main.py b/main.py index 2d62b38..ecca82b 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ from os import makedirs -from os.path import join +from os.path import isfile, join +from pathlib import Path from random import choice from sys import argv from urllib.request import urlretrieve @@ -22,6 +23,9 @@ zodiac_signs = [ ] +image_dir = join(Path(argv[0]).parent.resolve(), "images") + + def generate_horoscope() -> dict[str, dict[str, str]]: """Generate horoscope predictions""" horoscope = {} @@ -33,8 +37,8 @@ def generate_horoscope() -> dict[str, dict[str, str]]: def get_path(image: str): - """Return the path of the image""" - return join("images", f"{image.lower()}.png") + """Return the path of an image""" + return join(image_dir, f"{image.lower()}.png") def create_horoscope_image(horoscope: dict[str, dict[str, str]]): @@ -62,7 +66,7 @@ def create_horoscope_image(horoscope: dict[str, dict[str, str]]): image.paste(text_draw, (x - 10, y - 10), text_draw) # Love prediction - love = Image.open(join("images", "love.png")).convert("RGBA") + love = Image.open(join(image_dir, "love.png")).convert("RGBA") love.thumbnail(size_emoji) image.paste(love, (x + 125, y + 20), love) draw.text( @@ -73,7 +77,7 @@ def create_horoscope_image(horoscope: dict[str, dict[str, str]]): ) # Work prediction - work = Image.open(join("images", "work.png")).convert("RGBA") + work = Image.open(join(image_dir, "work.png")).convert("RGBA") work.thumbnail(size_emoji) image.paste(work, (x + 125, y + 60), work) draw.text( @@ -95,9 +99,13 @@ def download_images(): """Download images from source""" url = "https://git.mylloon.fr/Anri/Horoscope/raw/branch/main/images/" - makedirs("images", exist_ok=True) + makedirs(image_dir, exist_ok=True) for el in zodiac_signs + ["love", "work"]: - urlretrieve(url + el.lower() + ".png", get_path(el)) + path = get_path(el) + if not isfile(path): + image = el.lower() + ".png" + print(f"Download {image}...") + urlretrieve(url + image, path) if __name__ == "__main__": @@ -105,6 +113,7 @@ if __name__ == "__main__": exit(1) if len(argv) == 2: + print("Fetch missing images...") download_images() # Generate new horoscope @@ -112,6 +121,8 @@ if __name__ == "__main__": # Create and save the image horoscope_image = create_horoscope_image(new_horoscope) - horoscope_image.save("nouvel_horoscope.png") - print("Nouvel horoscope généré et sauvegardé sous 'nouvel_horoscope.png'") + png = "nouvel_horoscope.png" + horoscope_image.save(png) + + print(f"Nouvel horoscope généré et sauvegardé sous '{png}'")