From 70b4891402943ec9b1be86d06b25c8a8bac3b137 Mon Sep 17 00:00:00 2001 From: stoney420 Date: Fri, 27 Jun 2025 18:45:55 +0200 Subject: [PATCH] Update scripts/generate_playlist.py --- scripts/generate_playlist.py | 69 ++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/scripts/generate_playlist.py b/scripts/generate_playlist.py index 6ba43d5..486d359 100644 --- a/scripts/generate_playlist.py +++ b/scripts/generate_playlist.py @@ -348,6 +348,68 @@ def remove_duplicates(channels, settings): return unique_channels +def update_existing_channels_with_country_detection(): + """Re-process existing channels.txt to apply country detection to old channels.""" + if not os.path.exists(CHANNELS_FILE): + return + + settings = load_settings() + group_overrides = load_group_overrides() + + if not settings.get('auto_detect_country', True): + return + + log_message("Re-detecting countries for existing channels...", "INFO") + + # Read existing channels + with open(CHANNELS_FILE, 'r', encoding='utf-8') as f: + content = f.read() + + channel_blocks = re.split(r'\n\s*\n+', content.strip()) + updated_channels = [] + changes_made = 0 + + for block in channel_blocks: + if block.strip(): + channel = parse_channel_block(block) + if channel: + old_group = channel.get('Group', 'Uncategorized') + + # Apply auto-detection (this will update the group) + channel = apply_auto_country_detection(channel, group_overrides, settings) + + new_group = channel.get('Group', 'Uncategorized') + + # Log if group changed + if old_group != new_group: + changes_made += 1 + log_message(f"Updated: '{channel.get('Stream name')}' from '{old_group}' to '{new_group}'", "INFO") + + updated_channels.append(channel) + + # If changes were made, rewrite the file + if changes_made > 0: + log_message(f"Updating {changes_made} channels with new country detection...", "INFO") + + # Backup original + backup_name = f"{CHANNELS_FILE}.backup.{datetime.now().strftime('%Y%m%d_%H%M%S')}" + import shutil + shutil.copy2(CHANNELS_FILE, backup_name) + log_message(f"Created backup: {backup_name}", "INFO") + + # Write updated channels + with open(CHANNELS_FILE, 'w', encoding='utf-8') as f: + for i, channel in enumerate(updated_channels): + if i > 0: + f.write("\n\n") + + block_content = convert_to_channels_txt_block(channel) + f.write(block_content) + + log_message(f"Successfully updated {changes_made} channels with country detection", "INFO") + else: + log_message("No channels needed country detection updates", "INFO") + def process_import(): """Process bulk import file.""" settings = load_settings() @@ -461,11 +523,14 @@ def generate_playlist(): settings = load_settings() group_overrides = load_group_overrides() + # FIRST: Update existing channels with country detection + update_existing_channels_with_country_detection() + # Process import imported_channels = process_import() log_message(f"Import returned {len(imported_channels)} channels", "INFO") - # Read channels.txt + # Read channels.txt (now with updated countries) if not os.path.exists(CHANNELS_FILE): log_message(f"Error: {CHANNELS_FILE} not found", "ERROR") return @@ -481,7 +546,7 @@ def generate_playlist(): if block.strip(): channel = parse_channel_block(block) if channel: - channel = apply_auto_country_detection(channel, group_overrides, settings) + # Country detection already applied above, just load the channels parsed_channels.append(channel) log_message(f"Parsed {len(parsed_channels)} channels", "INFO")