Update scripts/generate_playlist.py
All checks were successful
📺 Generate M3U Playlist / build (push) Successful in 1m11s

This commit is contained in:
stoney420 2025-06-27 18:45:55 +02:00
parent e8517c0738
commit 70b4891402

View file

@ -348,6 +348,68 @@ def remove_duplicates(channels, settings):
return unique_channels 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(): def process_import():
"""Process bulk import file.""" """Process bulk import file."""
settings = load_settings() settings = load_settings()
@ -461,11 +523,14 @@ def generate_playlist():
settings = load_settings() settings = load_settings()
group_overrides = load_group_overrides() group_overrides = load_group_overrides()
# FIRST: Update existing channels with country detection
update_existing_channels_with_country_detection()
# Process import # Process import
imported_channels = process_import() imported_channels = process_import()
log_message(f"Import returned {len(imported_channels)} channels", "INFO") 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): if not os.path.exists(CHANNELS_FILE):
log_message(f"Error: {CHANNELS_FILE} not found", "ERROR") log_message(f"Error: {CHANNELS_FILE} not found", "ERROR")
return return
@ -481,7 +546,7 @@ def generate_playlist():
if block.strip(): if block.strip():
channel = parse_channel_block(block) channel = parse_channel_block(block)
if channel: if channel:
channel = apply_auto_country_detection(channel, group_overrides, settings) # Country detection already applied above, just load the channels
parsed_channels.append(channel) parsed_channels.append(channel)
log_message(f"Parsed {len(parsed_channels)} channels", "INFO") log_message(f"Parsed {len(parsed_channels)} channels", "INFO")