Files
new_dbsync/json_downloader.py
T
2025-05-20 12:02:07 -04:00

96 lines
3.0 KiB
Python

#!/usr/bin/env python3
import os
from pathlib import Path
from time import sleep
from tqdm import tqdm
import json
import requests
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:
print('Unable to save cache file.')
else:
datafile.close()
def fetch_json(self, 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(self,weburl):
try:
showresponse = requests.get(weburl)
except requests.exceptions.ConnectionError as e:
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('<seriesid>', 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()
new_updates = []
listing = open ('D:/Temp/filelist.txt', 'w')
os.chdir('D:/Projects/new_dbsync/cache/series')
directory_path = 'D:/Projects/new_dbsync/cache/series' # replace with your directory path
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()
# new_updates_2 = []
# listing = open ('D:/Temp/filelist.txt', 'r')
# for seriesid in listing:
# seriesid = seriesid.strip()
# new_updates_2.append(seriesid)
# listing.close()
# for seriesid in tqdm(range(len(new_updates_2)), desc='Checking cache', unit='seriesid'):
# # cachefile = str(new_updates_2[seriesid]) + '.json'
# # URL ='http://api.tvmaze.com/shows/<seriesid>/cast'
# # check_and_download(URL, str(new_updates_2[seriesid]), f'{new_updates_2[seriesid]}.json')
# os.remove(f'D:/Projects/new_dbsync/cache/cast/{new_updates_2[seriesid]}.json')