From 9f3692c21727ddd59895477d972f896c495ae5bf Mon Sep 17 00:00:00 2001 From: Jonas_Jones <91549607+J-onasJones@users.noreply.github.com> Date: Sat, 2 Dec 2023 19:03:24 +0100 Subject: [PATCH] Refactor to obey to pep-8 --- ...loadCdnBanners.py => download_cdn_banners.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) rename downloadCdnBanners.py => download_cdn_banners.py (69%) diff --git a/downloadCdnBanners.py b/download_cdn_banners.py similarity index 69% rename from downloadCdnBanners.py rename to download_cdn_banners.py index 93edb9b..865506b 100644 --- a/downloadCdnBanners.py +++ b/download_cdn_banners.py @@ -1,22 +1,23 @@ +'''A script to download all the banners from the CDN and save them to a local directory.''' import json import os import requests # Define the URL pattern for downloading images -base_url = "https://cdn.jonasjones.dev/project-banners" +BASE_URL = "https://cdn.jonasjones.dev/project-banners" # Define the path to the JSON file -json_file_path = "/home/jonas_jones/GitHub/jonasjones.dev/src/routes/projects/projects.json" +JSON_FILE_PATH = "/home/jonas_jones/GitHub/jonasjones.dev/src/routes/projects/projects.json" # Define the directory where you want to save the downloaded images -download_directory = os.path.expanduser("~/Downloads/uwu/") +DOWNLOAD_DIR = os.path.expanduser("~/Downloads/uwu/") # Create the download directory if it doesn't exist -os.makedirs(download_directory, exist_ok=True) +os.makedirs(DOWNLOAD_DIR, exist_ok=True) try: # Open and parse the JSON file - with open(json_file_path, "r") as json_file: + with open(JSON_FILE_PATH, "r") as json_file: data = json.load(json_file) # Iterate through the list of objects in the JSON file @@ -25,7 +26,7 @@ try: if "backgroud" in item: background_value = item["backgroud"] # Construct the full URL for the image - image_url = f"{base_url}{background_value}" + image_url = f"{BASE_URL}{background_value}" # Download the image response = requests.get(image_url) @@ -33,7 +34,7 @@ try: # Get the filename from the URL filename = os.path.basename(image_url) # Save the image to the download directory - with open(os.path.join(download_directory, filename), "wb") as image_file: + with open(os.path.join(DOWNLOAD_DIR, filename), "wb") as image_file: image_file.write(response.content) print(f"Downloaded: {filename}") else: @@ -42,6 +43,6 @@ try: print(f"Doesn't contain background: {item}") except FileNotFoundError: - print(f"JSON file not found: {json_file_path}") + print(f"JSON file not found: {JSON_FILE_PATH}") except Exception as e: print(f"An error occurred: {str(e)}")