#!/usr/bin/env python3

import os
import json
import requests
import time

def download_json_to_file(api_url, outputdir, output_file, lprint):
    """
    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(0.5)  # Sleep for 0.5 second to avoid overwhelming the server
    except Exception as e:
        lprint.logprint("info", f"Downloadinng {outputdir}{output_file} for  series ID: {output_file.replace('.json', '')} failed: {e}")

