Refactor code structure for improved readability and maintainability
This commit is contained in:
+44
-16
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user