From 98ef0d49f7c7aa816ce49edb3a977f5b430ff6b9 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Fri, 2 May 2025 11:23:36 -0500 Subject: [PATCH] Add file existence checks and error handling in parse_channels_only --- apps/epg/tasks.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/apps/epg/tasks.py b/apps/epg/tasks.py index cb985a51..ac05d464 100644 --- a/apps/epg/tasks.py +++ b/apps/epg/tasks.py @@ -103,17 +103,40 @@ def parse_channels_only(source): if not file_path: file_path = source.get_cache_file() + # Add check for file existence + if not os.path.exists(file_path): + logger.error(f"EPG file does not exist at path: {file_path}") + # Get a new cache file path and update the source + new_path = source.get_cache_file() + logger.info(f"Updating file_path from '{file_path}' to '{new_path}'") + source.file_path = new_path + source.save(update_fields=['file_path']) + + # Still need to check if we need to fetch it + if not os.path.exists(new_path): + logger.info(f"New cache file does not exist, need to fetch EPG data first") + return + + file_path = new_path + logger.info(f"Parsing channels from EPG file: {file_path}") existing_epgs = {e.tvg_id: e for e in EPGData.objects.filter(epg_source=source)} # Read entire file (decompress if .gz) - if file_path.endswith('.gz'): - with open(file_path, 'rb') as gz_file: - decompressed = gzip.decompress(gz_file.read()) - xml_data = decompressed.decode('utf-8') - else: - with open(file_path, 'r', encoding='utf-8') as xml_file: - xml_data = xml_file.read() + try: + if file_path.endswith('.gz'): + with open(file_path, 'rb') as gz_file: + decompressed = gzip.decompress(gz_file.read()) + xml_data = decompressed.decode('utf-8') + else: + with open(file_path, 'r', encoding='utf-8') as xml_file: + xml_data = xml_file.read() + except FileNotFoundError: + logger.error(f"EPG file not found at: {file_path}") + return + except Exception as e: + logger.error(f"Error reading EPG file {file_path}: {e}", exc_info=True) + return root = ET.fromstring(xml_data) channels = root.findall('channel')