import re import os from datetime import datetime # Simple cleanup utility for corrupted channels.txt CHANNELS_FILE = 'channels.txt' def clean_channels_file(): """Clean up corrupted channels.txt file.""" if not os.path.exists(CHANNELS_FILE): print("āŒ channels.txt not found") return print("šŸ”§ Starting cleanup of channels.txt...") # Read the file with open(CHANNELS_FILE, 'r', encoding='utf-8') as f: content = f.read() print(f"šŸ“„ Read {len(content)} characters from channels.txt") # Split into channel blocks channel_blocks = re.split(r'\n\s*\n+', content.strip()) print(f"šŸ“‹ Found {len(channel_blocks)} channel blocks") cleaned_channels = [] fixed_count = 0 for i, block in enumerate(channel_blocks): if not block.strip(): continue print(f"šŸ” Processing block {i+1}...") # Parse the block channel = {} lines = block.strip().split('\n') for line in lines: if '=' in line: key, value = line.split('=', 1) channel[key.strip()] = value.strip() if not channel: continue stream_name = channel.get('Stream name', 'Unknown') print(f" Channel: {stream_name}") # Clean Stream URL stream_url = channel.get('Stream URL', '') original_url = stream_url if '#EXTINF' in stream_url: stream_url = stream_url.split('#EXTINF')[0].strip() fixed_count += 1 print(f" āœ… Fixed Stream URL (removed #EXTINF)") if 'group-title=' in stream_url: stream_url = stream_url.split('group-title=')[0].strip() fixed_count += 1 print(f" āœ… Fixed Stream URL (removed group-title)") # Remove any trailing junk stream_url = re.sub(r'\s+.*$', '', stream_url) if stream_url != original_url: print(f" šŸ”§ URL: {original_url[:50]}... → {stream_url[:50]}...") channel['Stream URL'] = stream_url # Clean Logo URL logo_url = channel.get('Logo', '') original_logo = logo_url if logo_url and 'group-title=' in logo_url: logo_url = logo_url.split('group-title=')[0].strip() fixed_count += 1 print(f" āœ… Fixed Logo URL") if logo_url and '#EXTINF' in logo_url: logo_url = logo_url.split('#EXTINF')[0].strip() fixed_count += 1 print(f" āœ… Fixed Logo URL") channel['Logo'] = logo_url cleaned_channels.append(channel) print(f"\nšŸ“Š Summary:") print(f" Total channels: {len(cleaned_channels)}") print(f" Fixes applied: {fixed_count}") if fixed_count > 0: # Create backup backup_name = f"{CHANNELS_FILE}.backup.{datetime.now().strftime('%Y%m%d_%H%M%S')}" print(f"šŸ’¾ Creating backup: {backup_name}") try: import shutil shutil.copy2(CHANNELS_FILE, backup_name) print(f"āœ… Backup created successfully") except Exception as e: print(f"āš ļø Could not create backup: {e}") # Write cleaned file print(f"šŸ“ Writing cleaned channels.txt...") with open(CHANNELS_FILE, 'w', encoding='utf-8') as f: for i, channel in enumerate(cleaned_channels): if i > 0: f.write("\n\n") # Write channel block f.write(f"Group = {channel.get('Group', 'Uncategorized')}\n") f.write(f"Stream name = {channel.get('Stream name', 'Unknown Channel')}\n") f.write(f"Logo = {channel.get('Logo', '')}\n") f.write(f"EPG id = {channel.get('EPG id', '')}\n") f.write(f"Stream URL = {channel.get('Stream URL', '')}") print(f"āœ… Successfully cleaned and rewrote channels.txt!") print(f"šŸŽ‰ Fixed {fixed_count} corrupted entries") else: print("āœ… No corrupted entries found - file is already clean!") if __name__ == "__main__": clean_channels_file()