23 lines
715 B
Python
23 lines
715 B
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import json
|
|
import requests
|
|
import time
|
|
|
|
def download_json_to_file(api_url, outputdir, output_file):
|
|
"""
|
|
Downloads JSON data from the given API URL and saves it to the specified file.
|
|
"""
|
|
try:
|
|
response = requests.get(api_url)
|
|
response.raise_for_status() # Raise an error for bad status codes
|
|
data = response.json()
|
|
with open(f"{outputdir}{output_file}", 'w') as f:
|
|
json.dump(data, f, indent=4)
|
|
#print(f"JSON data saved to {outputdir}{output_file}")
|
|
time.sleep(.5) # Sleep for 0.5 second to avoid overwhelming the server
|
|
except Exception as e:
|
|
print(f"Failed to download or save JSON: {e}")
|
|
|