# /// script # requires-python = ">=3.12" # dependencies = ["requests<3"] # /// import json import os import requests API_ENDPOINT_URL = os.getenv("CONTEST_HOST") + os.getenv("CONTEST_ENDPOINT") def download_image(url, payload, filepath): try: # Use a session for potential connection pooling with requests.Session() as s: with s.post(url, json=payload, stream=True) as response: response.raise_for_status() # Write the content to the file in chunks 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}" ) except requests.exceptions.RequestException as e: print( f" ERROR: Could not download '{os.path.basename(filepath)}'. Reason: {e}" ) def main(): with open("answer.json", "r") as file: manifest_data = json.load(file) if not manifest_data: return for test_case in manifest_data.get("tests", []): filename = test_case.get("name") request_payload = test_case.get("request") if not filename or not request_payload: print(f"Skipping invalid test case: {test_case}") continue print(f"Requesting '{filename}'...") download_image(API_ENDPOINT_URL, request_payload, filename) if __name__ == "__main__": main()