From e83930d6e94ceee82f3331f3642a82b9eb281124 Mon Sep 17 00:00:00 2001 From: stoney420 Date: Fri, 27 Jun 2025 23:57:37 +0200 Subject: [PATCH] Update scripts/generate_playlist.py --- scripts/generate_playlist.py | 103 ++++++++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 7 deletions(-) diff --git a/scripts/generate_playlist.py b/scripts/generate_playlist.py index 395fc26..dc70266 100644 --- a/scripts/generate_playlist.py +++ b/scripts/generate_playlist.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ -IPTV Playlist Generator - Main Script (Scripts Folder Version) -Modular design for better maintainability and easier development. +IPTV Playlist Generator - Debug Version +Enhanced debugging to find why channels.txt is empty """ import logging @@ -23,7 +23,7 @@ from report_generator import ReportGenerator def setup_logging(): """Setup comprehensive logging.""" logging.basicConfig( - level=logging.INFO, + level=logging.DEBUG, # Changed to DEBUG for more info format='[%(asctime)s] %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S', handlers=[ @@ -32,14 +32,67 @@ def setup_logging(): ] ) +def debug_file_system(): + """Debug the file system to see what files exist.""" + logging.info("=== FILE SYSTEM DEBUG ===") + + # Check current directory + current_dir = os.getcwd() + logging.info(f"Current working directory: {current_dir}") + + # List all files in current directory + try: + files = os.listdir('.') + logging.info(f"Files in current directory: {files}") + except Exception as e: + logging.error(f"Could not list current directory: {e}") + + # Check for specific files + files_to_check = [ + 'bulk_import.m3u', + 'channels.txt', + 'playlist.m3u', + '../bulk_import.m3u', + '../channels.txt' + ] + + for file_path in files_to_check: + if os.path.exists(file_path): + try: + size = os.path.getsize(file_path) + logging.info(f"✅ Found {file_path} (size: {size} bytes)") + + # If it's the import file, show first few lines + if 'bulk_import.m3u' in file_path and size > 0: + with open(file_path, 'r', encoding='utf-8') as f: + first_lines = [f.readline().strip() for _ in range(3)] + logging.info(f" First 3 lines: {first_lines}") + + except Exception as e: + logging.error(f" Error reading {file_path}: {e}") + else: + logging.info(f"❌ Missing: {file_path}") + + logging.info("=== END FILE SYSTEM DEBUG ===") + def generate_playlist(): - """Main playlist generation function - coordinates all modules.""" + """Main playlist generation function with enhanced debugging.""" try: setup_logging() - logging.info("Starting modular playlist generation...") + logging.info("Starting DEBUG playlist generation...") + + # Debug file system first + debug_file_system() # Initialize all modules + logging.info("Initializing modules...") config = ConfigManager() + + # Debug config + logging.info(f"Config channels_file: {config.channels_file}") + logging.info(f"Config import_file: {config.import_file}") + logging.info(f"Config settings: {config.settings}") + file_manager = FileManager(config) processor = ChannelProcessor(config) builder = PlaylistBuilder(config) @@ -61,26 +114,57 @@ def generate_playlist(): } # Step 1: Create backup + logging.info("=== STEP 1: Creating backup ===") file_manager.create_backup('channels.txt') # Step 2: Clean existing corrupted entries + logging.info("=== STEP 2: Cleaning corrupted channels ===") processor.clean_corrupted_channels() # Step 3: Force update existing channels with new country detection + logging.info("=== STEP 3: Updating existing channels ===") processor.update_existing_channels_with_country_detection() - # Step 4: Process imports + # Step 4: Process imports (THE CRITICAL STEP) + logging.info("=== STEP 4: Processing imports ===") + + # Extra debugging for import process + import_file_paths = ['bulk_import.m3u', '../bulk_import.m3u'] + for path in import_file_paths: + if os.path.exists(path): + logging.info(f"Found import file at: {path}") + # Temporarily update config to use this path + config.import_file = path + break + imported_channels = processor.process_import() stats['imported_channels'] = len(imported_channels) logging.info(f"Import returned {len(imported_channels)} channels") + if len(imported_channels) == 0: + logging.warning("NO CHANNELS IMPORTED! This is the problem.") + logging.info("Checking import file details...") + + # Manual check of import file + for path in ['bulk_import.m3u', '../bulk_import.m3u']: + if os.path.exists(path): + with open(path, 'r', encoding='utf-8') as f: + content = f.read() + logging.info(f"Import file {path} has {len(content)} characters") + logging.info(f"Import file first 200 chars: {content[:200]}") + break + # Step 5: Load all channels + logging.info("=== STEP 5: Loading all channels ===") all_channels = file_manager.load_all_channels() stats['total_channels'] = len(all_channels) + logging.info(f"Loaded {len(all_channels)} total channels") # Step 6: Remove duplicates + logging.info("=== STEP 6: Removing duplicates ===") unique_channels = processor.remove_duplicates_optimized(all_channels) stats['duplicates_removed'] = len(all_channels) - len(unique_channels) + logging.info(f"After deduplication: {len(unique_channels)} channels") # Step 7: Sort channels if config.settings.get('sort_channels', True): @@ -92,16 +176,21 @@ def generate_playlist(): health_results = health_checker.batch_health_check(unique_channels) # Step 9: Generate M3U playlist + logging.info("=== STEP 9: Generating M3U ===") valid_channels, country_stats = builder.generate_m3u(unique_channels) stats['valid_channels'] = valid_channels stats['country_distribution'] = country_stats stats['countries_detected'] = len(country_stats) # Step 10: Generate report + logging.info("=== STEP 10: Generating report ===") report_gen.save_report(stats, health_results) logging.info(f"Playlist generation complete: {valid_channels} channels across {len(country_stats)} countries") - logging.info(f"Top countries: {dict(list(sorted(country_stats.items(), key=lambda x: x[1], reverse=True))[:5])}") + + # Final debug + logging.info("=== FINAL DEBUG ===") + debug_file_system() return True