#!/usr/bin/env python3 import os import json import requests def download_json_to_file(api_url, 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(output_file, 'w') as f: json.dump(data, f, indent=4) print(f"JSON data saved to {output_file}") except Exception as e: print(f"Failed to download or save JSON: {e}") def load(self, rawdata): jdata = {} jdata = json.loads(rawdata) newdata = self.dump(jdata) return (newdata) def dump(self, rawdata): jdata = json.dumps(rawdata, indent=4) return (jdata) def jconvert(self, inputdata): outputdata = json.loads(inputdata) return (outputdata) def populate_cache(self, jdata, jfile): try: with open(jfile, 'w') as datafile: datafile.write(jdata) except Exception: print('Unable to save cache file.') else: datafile.close() def fetch_json(weburl, seriesid, cachefile): try: webdata = get_data(weburl) jdata = json.load(webdata) except Exception as e: print('Unable to get JSON data for ' + str(seriesid) + ': ' + str(e)) else: if jdata == '[]': pass else: populate_cache(jdata, cachefile) def get_data(weburl): try: showresponse = requests.get(weburl) except requests.exceptions.ConnectionError: print('Unable to connect and get JSON data for url ' + str(weburl)) else: return (showresponse.text) def check_and_download(URL, series_id, cachefile): try: mazeurl = URL.replace('', str(series_id)) response = requests.get(mazeurl) data = json.loads(response.text) except FileNotFoundError: # print(f'{cachefile} does not exist. Attempting to download.') jget.fetch_json( URL.replace('SERIESID', str(series_id)), series_id, cachefile ) except Exception as e: print(f'Unable to download {cachefile}. {e}') else: with open(cachefile, 'w') as f: json.dump(data, f, indent=4) f.close() url_prefix = 'http://api.tvmaze.com/shows/' new_updates = [] listing_file= open('./temp/filelist.txt', 'r') os.chdir('./cache/series') # replace with your directory path directory_path = os.getcwd() for line in listing_file: seriesid = line.strip() cachefile = f'{seriesid}.json' download_json_to_file(url_prefix.replace('', seriesid), cachefile) listing_file.close() exit() # files = [ # f for f in os.listdir(directory_path) # if os.path.isfile(os.path.join(directory_path, f)) # ] # for filename in files: # with open(filename, 'r') as file: # content = file.read() # if 'Not Found' in content or 'Too Many Requests' in content: # seriesid = filename.split('.')[0] # new_updates.append(seriesid) # listing.write(seriesid + '\n') # listing.close() # exit() # import requests # import json