import os import json CACHE_DIR = os.getenv("CACHE_DIR") INDEX_FILE = os.getenv("INDEX_FILE") def get_all_tv_shows(indexed_cache): """Get all TV shows from the indexed cache structure JSON file.""" tv_shows = {} for directory in indexed_cache: if directory['type'] == 'directory' and directory['path'] == 'tv': for sub_directory in directory['contents']: if sub_directory['type'] == 'directory': show_title = sub_directory['path'].split('/')[-1] tv_shows[show_title] = [] for season_directory in sub_directory['contents']: if season_directory['type'] == 'directory': season = season_directory['path'].split('/')[-1] for episode in season_directory['contents']: if episode['type'] == 'file': tv_shows[show_title].append({ "season": season, "episode": episode['path'].split('/')[-1], "path": episode['path'] }) return tv_shows with open(INDEX_FILE, 'r') as f: file_structure = json.load(f) print(get_all_tv_shows(file_structure))