Files
new_dbsync/json_downloader.py

96 lines
2.4 KiB
Python

#!/usr/bin/env python3
import os
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 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('<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')
# replace with your directory path
directory_path = 'D:/Projects/new_dbsync/cache/series'
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()