Files
new_dbsync/json_downloader.py
T

23 lines
712 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(1) # Sleep for 1 second to avoid overwhelming the server
except Exception as e:
print(f"Failed to download or save JSON: {e}")