simplify generate.py
This commit is contained in:
parent
3734bc9ac8
commit
7291b1ddfc
1 changed files with 16 additions and 23 deletions
33
generate.py
33
generate.py
|
|
@ -5,23 +5,11 @@
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import requests
|
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")
|
API_ENDPOINT_URL = os.getenv("CONTEST_HOST") + os.getenv("CONTEST_ENDPOINT")
|
||||||
DOWNLOAD_DIR = os.getenv("CONTEST_OUTPUT")
|
|
||||||
|
|
||||||
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):
|
def download_image(url, payload, filepath):
|
||||||
try:
|
try:
|
||||||
|
|
@ -31,21 +19,26 @@ def download_image(url, payload, filepath):
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
# Write the content to the file in chunks
|
# 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):
|
for chunk in response.iter_content(chunk_size=8192):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
|
|
||||||
print(f" SUCCESS: Saved '{os.path.basename(filepath)}'")
|
print(f" SUCCESS: Saved '{os.path.basename(filepath)}'")
|
||||||
|
|
||||||
except requests.exceptions.HTTPError as e:
|
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:
|
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():
|
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:
|
if not manifest_data:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -58,8 +51,8 @@ def main():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(f"Requesting '{filename}'...")
|
print(f"Requesting '{filename}'...")
|
||||||
output_filepath = os.path.join(DOWNLOAD_DIR, filename)
|
download_image(API_ENDPOINT_URL, request_payload, filename)
|
||||||
download_image(API_ENDPOINT_URL, request_payload, output_filepath)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue