#!/usr/bin/python3

import os
import json
import requests
import time
import logging

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}")


def main():
    # Download data from an API endpoint to a specified file in JSON format
    api_url = 'https://sample-api.com/data'
    outputdir = '/home/user/'
    outputfile = 'data.json'

    lprint = Logprint('output') 
    try:
        download_json_to_file(api_url, outputdir, outputfile)
        print("Download successful!")  
    except Exception as e: 
        # Handle any exceptions that occur during the download process and log them
        lprint.logprint("info", f"Downloading {outputdir}{outputfile} failed with error:{e}") 
        exit(1)

logger = logging.getLogger(__name__)  # Use __name__ as the name for the logger

formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')

fh = logging.FileHandler('logfilename.txt')  # Create a file handler object
fh.setFormatter(formatter)

sh = logging.StreamHandler()   # Create a stream handler object (for console output)
sh.setFormatter(formatter)


logger.setLevel(logging.INFO)   # Set the level of messages to log. Possible values are DEBUG, INFO, WARNING, ERROR, and CRITICAL. Default is INFO

if not logger.hasHandlers():
    logger.addHandler(fh)  # Add file handler object to the logger object
if not logger.handlers: 
    logger.addHandler(sh)   # Add stream handler object to the logger object


try:
    download_json_to_file()  # Your function that you want to log the output of
except Exception as e:   # Handle exceptions if any occur
    logger.error(f"Error while running {download_json_to_file}: {e}")
else:   
    # If no exceptions are encountered, write a INFO message to the log file
    logger.info(f"{download_json_to_file} completed successfully.")
    
if __name__ == "__main__":
    main()
