download in temp directory
All checks were successful
Upload release / build (push) Successful in 2s

This commit is contained in:
Mylloon 2024-09-29 01:01:56 +02:00
parent 97b9ea356d
commit 4f0232fe42
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

29
main.py
View file

@ -1,5 +1,6 @@
from os import makedirs from os import makedirs
from os.path import join from os.path import isfile, join
from pathlib import Path
from random import choice from random import choice
from sys import argv from sys import argv
from urllib.request import urlretrieve 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]]: def generate_horoscope() -> dict[str, dict[str, str]]:
"""Generate horoscope predictions""" """Generate horoscope predictions"""
horoscope = {} horoscope = {}
@ -33,8 +37,8 @@ def generate_horoscope() -> dict[str, dict[str, str]]:
def get_path(image: str): def get_path(image: str):
"""Return the path of the image""" """Return the path of an image"""
return join("images", f"{image.lower()}.png") return join(image_dir, f"{image.lower()}.png")
def create_horoscope_image(horoscope: dict[str, dict[str, str]]): 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) image.paste(text_draw, (x - 10, y - 10), text_draw)
# Love prediction # 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) love.thumbnail(size_emoji)
image.paste(love, (x + 125, y + 20), love) image.paste(love, (x + 125, y + 20), love)
draw.text( draw.text(
@ -73,7 +77,7 @@ def create_horoscope_image(horoscope: dict[str, dict[str, str]]):
) )
# Work prediction # 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) work.thumbnail(size_emoji)
image.paste(work, (x + 125, y + 60), work) image.paste(work, (x + 125, y + 60), work)
draw.text( draw.text(
@ -95,9 +99,13 @@ def download_images():
"""Download images from source""" """Download images from source"""
url = "https://git.mylloon.fr/Anri/Horoscope/raw/branch/main/images/" 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"]: 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__": if __name__ == "__main__":
@ -105,6 +113,7 @@ if __name__ == "__main__":
exit(1) exit(1)
if len(argv) == 2: if len(argv) == 2:
print("Fetch missing images...")
download_images() download_images()
# Generate new horoscope # Generate new horoscope
@ -112,6 +121,8 @@ if __name__ == "__main__":
# Create and save the image # Create and save the image
horoscope_image = create_horoscope_image(new_horoscope) 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}'")