Refactor code structure for improved readability and maintainability

This commit is contained in:
2025-11-14 16:24:06 -05:00
parent 1d14e97637
commit 271bf606ca
5 changed files with 142895 additions and 16 deletions
View File
+89
View File
@@ -0,0 +1,89 @@
import os
import progressbar
from pathlib import Path
from time import sleep
from tqdm import tqdm
class Cacher(object):
def __init__(self):
pass
def process(self, options, jget, new_updates, ROOTDIR, URLS):
SERIESCACHE = str(ROOTDIR) + '/cache/series/'
EPISODECACHE = str(ROOTDIR) + '/cache/episodes/'
CASTCACHE = str(ROOTDIR) + '/cache/cast/'
CREWCACHE = str(ROOTDIR) + '/cache/crew/'
TEMPDIR = ROOTDIR + '/temp/'
available_updates = {}
updateList = []
if options['refreshcache'] == '1':
print('Refreshing cache directory.')
print('Checking cache directory for missing files.')
for update in new_updates:
available_updates[update[0]] = update[1]
updateList = [str(key) for key in available_updates.keys()]
for series in tqdm(range(len(new_updates)), desc='Checking cache', unit='series'):
seriesid = new_updates[series][0]
cachefile = str(seriesid) + '.json'
seriescachefile = f'{SERIESCACHE}{cachefile}'
episodecachefile = f'{EPISODECACHE}{cachefile}'
castcachefile = f'{CASTCACHE}{cachefile}'
crewcachefile = f'{CREWCACHE}{cachefile}'
self.check_and_download(jget, URLS['showurl'], str(seriesid), new_updates[0], seriescachefile, 1500)
sleep(1)
self.check_and_download(jget, URLS['episodesurl'], str(seriesid), new_updates[0], episodecachefile, 1500)
sleep(1)
self.check_and_download(jget, URLS['casturl'], str(seriesid), new_updates[0], castcachefile, 500)
sleep(1)
self.check_and_download(jget, URLS['crewurl'], str(seriesid), new_updates[0], crewcachefile, 500)
sleep(1)
print('Refresh completed.')
def check_and_download(self, jget, url, SERIESID, series_id, cachefile, size_limit):
try:
my_abs_path = Path(cachefile).resolve(strict=True)
file_stats = os.stat(cachefile)
if file_stats.st_size <= size_limit:
#print(f'File Size for {cachefile} in Bytes is {file_stats.st_size}. File too small. Attempting to download.')
jget.fetch_json(url.replace(SERIESID, str(series_id)), series_id, cachefile)
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}')
def update_cache(self, jget, updateList, ROOTDIR, URLS, options, lprint):
if options['skipcache'] == '0':
SERIESCACHE = str(ROOTDIR) + '/cache/series/'
EPISODECACHE = str(ROOTDIR) + '/cache/episodes/'
CASTCACHE = str(ROOTDIR) + '/cache/cast/'
CREWCACHE = str(ROOTDIR) + '/cache/crew/'
TEMPDIR = ROOTDIR + '/temp/'
print('Now downloading updates.')
updaterange = len(updateList)
for seriesid in tqdm(range(updaterange), desc='Downloading updates', unit='seriesid'):
cachefile = f'{updateList[seriesid]}.json'
seriescachefile = SERIESCACHE + cachefile
episodecachefile = EPISODECACHE + cachefile
castcachefile = CASTCACHE + cachefile
crewcachefile = CREWCACHE + cachefile
self.download_json(jget, URLS['showurl'], updateList[seriesid], seriescachefile, lprint)
sleep(1)
self.download_json(jget, URLS['episodesurl'], updateList[seriesid], episodecachefile, lprint)
sleep(1)
self.download_json(jget, URLS['casturl'], updateList[seriesid], castcachefile, lprint)
sleep(1)
self.download_json(jget, URLS['crewurl'], updateList[seriesid], crewcachefile, lprint)
sleep(1)
def download_json(self, jget, url, seriesid, cachefile, lprint):
try:
jget.fetch_json(url.replace('<seriesid>', str(seriesid)), seriesid, cachefile)
except Exception as e:
lprint.logprint('info', f'Unable to acquire {cachefile}. {e}')
def __del__(self):
pass
+44 -16
View File
@@ -5,6 +5,20 @@ 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)
@@ -72,24 +86,38 @@ def check_and_download(URL, series_id, cachefile):
json.dump(data, f, indent=4)
f.close()
url_prefix = 'http://api.tvmaze.com/shows/<tvmazeid>'
new_updates = []
listing = open('D:/Temp/filelist.txt', 'w')
listing_file= open('./temp/filelist.txt', 'r')
os.chdir('D:/Projects/new_dbsync/cache/series')
os.chdir('./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()
directory_path = os.getcwd()
for line in listing_file:
seriesid = line.strip()
cachefile = f'{seriesid}.json'
download_json_to_file(url_prefix.replace('<tvmazeid>', 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
+71381
View File
File diff suppressed because it is too large Load Diff
+71381
View File
File diff suppressed because it is too large Load Diff