diff --git a/generate.py b/generate.py index a542afe..1fe9474 100644 --- a/generate.py +++ b/generate.py @@ -5,23 +5,11 @@ import json import os + import requests -MANIFEST_URL = "https://null.formulatrix.dev/Contest/stitch-a-ton-answer/raw/branch/main/answer.json" -API_ENDPOINT_URL = os.getenv("CONTEST_HOST")+ os.getenv("CONTEST_ENDPOINT") -DOWNLOAD_DIR = os.getenv("CONTEST_OUTPUT") +API_ENDPOINT_URL = os.getenv("CONTEST_HOST") + os.getenv("CONTEST_ENDPOINT") -def fetch_manifest(url): - print(f"fetching manifest from {url}...") - try: - response = requests.get(url) - response.raise_for_status() - return response.json() - except requests.exceptions.RequestException as e: - print(f" ERROR: Could not fetch manifest. Reason: {e}") - except json.JSONDecodeError: - print(" ERROR: Failed to parse JSON from manifest. The content may not be valid JSON.") - return None def download_image(url, payload, filepath): try: @@ -31,21 +19,26 @@ def download_image(url, payload, filepath): response.raise_for_status() # Write the content to the file in chunks - with open(filepath, 'wb') as f: + with open(filepath, "wb") as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) - + print(f" SUCCESS: Saved '{os.path.basename(filepath)}'") except requests.exceptions.HTTPError as e: - print(f" ERROR: HTTP error for '{os.path.basename(filepath)}': {e.response.status_code} {e.response.reason}") + print( + f" ERROR: HTTP error for '{os.path.basename(filepath)}': {e.response.status_code} {e.response.reason}" + ) except requests.exceptions.RequestException as e: - print(f" ERROR: Could not download '{os.path.basename(filepath)}'. Reason: {e}") + print( + f" ERROR: Could not download '{os.path.basename(filepath)}'. Reason: {e}" + ) + def main(): - os.makedirs(DOWNLOAD_DIR, exist_ok=True) + with open("answer.json", "r") as file: + manifest_data = json.load(file) - manifest_data = fetch_manifest(MANIFEST_URL) if not manifest_data: return @@ -58,8 +51,8 @@ def main(): continue print(f"Requesting '{filename}'...") - output_filepath = os.path.join(DOWNLOAD_DIR, filename) - download_image(API_ENDPOINT_URL, request_payload, output_filepath) + download_image(API_ENDPOINT_URL, request_payload, filename) + if __name__ == "__main__": - main() \ No newline at end of file + main()