my-private-iptv-m3u/scripts/generate_playlist.py
stoney420 2943aa881f
All checks were successful
Generate M3U Playlist with Auto-Organization / build-and-organize (push) Successful in 26s
Update scripts/generate_playlist.py
2025-06-29 02:47:11 +02:00

223 lines
No EOL
7.8 KiB
Python

#!/usr/bin/env python3
"""
IPTV Traditional Broadcasters Only - Simplified Version
Clean, minimal code with no redundancy
"""
import os
import shutil
from datetime import datetime
from pathlib import Path
# Ensure correct directory
script_dir = Path(__file__).parent
root_dir = script_dir.parent
os.chdir(root_dir)
def is_streaming_platform(all_text):
"""Check if channel is from streaming platform (simplified)."""
platforms = ["plex.tv", "pluto.tv", "jmp2.uk/plu-", "sam-", "samsung", "tubi", "xumo", "roku", "youtube"]
return any(platform in all_text for platform in platforms)
def detect_traditional_broadcaster_country(channel_name, epg_id="", logo_url="", stream_url=""):
"""Detect country for traditional broadcasters only."""
all_text = f"{channel_name.lower()} {epg_id.lower()} {logo_url.lower()} {stream_url.lower()}"
channel_lower = channel_name.lower()
# Skip streaming platforms
if is_streaming_platform(all_text):
return "Uncategorized"
# EPG ID detection (most reliable)
epg_countries = {
".ca": "🇨🇦 Canada", ".us": "🇺🇸 United States", ".uk": "🇬🇧 United Kingdom",
".ph": "🇵🇭 Philippines", ".au": "🇦🇺 Australia", ".jp": "🇯🇵 Japan",
".my": "🇲🇾 Malaysia", ".de": "🇩🇪 Germany", ".fr": "🇫🇷 France",
".es": "🇪🇸 Spain", ".it": "🇮🇹 Italy", ".br": "🇧🇷 Brazil"
}
for domain, country in epg_countries.items():
if domain in epg_id.lower():
return country
# Essential broadcaster patterns (simplified)
broadcasters = {
"🇨🇦 Canada": ["tsn", "cbc", "ctv", "global", "sportsnet", "w network"],
"🇺🇸 United States": ["cbs", "nbc", "abc", "fox", "cnn", "espn", "discovery", "hgtv", "mtv", "c-span"],
"🇬🇧 United Kingdom": ["bbc", "itv", "sky", "channel 4", "channel 5"],
"🇵🇭 Philippines": ["abs-cbn", "gma", "anc", "tv5"],
"🇦🇺 Australia": ["abc australia", "nine network", "seven network"],
"🇯🇵 Japan": ["nhk", "fuji tv", "animax"],
"🇲🇾 Malaysia": ["tv1", "tv2", "astro"],
"🇩🇪 Germany": ["ard", "zdf", "rtl", "sat.1"],
"🇫🇷 France": ["tf1", "france 2", "canal+"],
"🇪🇸 Spain": ["antena 3", "telecinco", "tve"],
"🇮🇹 Italy": ["rai", "mediaset", "canale 5"],
"🇧🇷 Brazil": ["globo", "sbt", "record"],
"🇲🇽 Mexico": ["televisa", "tv azteca"],
"🇷🇺 Russia": ["первый", "россия", "нтв"]
}
for country, keywords in broadcasters.items():
if any(keyword in all_text for keyword in keywords):
return country
return "Uncategorized"
def load_channels():
"""Load channels from channels.txt."""
if not os.path.exists('channels.txt'):
print("❌ No channels.txt found")
return []
try:
with open('channels.txt', 'r', encoding='utf-8') as f:
content = f.read()
channels = []
for block in content.split('\n\n'):
if not block.strip():
continue
channel_data = {}
for line in block.strip().split('\n'):
if '=' in line:
key, value = line.split('=', 1)
channel_data[key.strip()] = value.strip()
if channel_data.get('Stream name'):
channels.append(channel_data)
print(f"✅ Loaded {len(channels)} channels")
return channels
except Exception as e:
print(f"❌ Error loading channels: {e}")
return []
def reorganize_channels(channels):
"""Reorganize channels by country (traditional only)."""
print("📺 Organizing traditional broadcasters by country...")
changes = 0
stats = {}
for channel in channels:
old_group = channel.get('Group', 'Uncategorized')
stream_name = channel.get('Stream name', '')
epg_id = channel.get('EPG id', '')
logo = channel.get('Logo', '')
stream_url = channel.get('Stream URL', '')
new_group = detect_traditional_broadcaster_country(stream_name, epg_id, logo, stream_url)
if old_group != new_group:
if new_group == "Uncategorized":
print(f"📱 Platform: '{stream_name}' → Uncategorized")
else:
print(f"📺 Fixed: '{stream_name}' {old_group}{new_group}")
channel['Group'] = new_group
changes += 1
stats[new_group] = stats.get(new_group, 0) + 1
print(f"\n✅ Changes made: {changes}")
print(f"📺 Traditional broadcasters: {sum(v for k, v in stats.items() if k != 'Uncategorized')}")
print(f"📱 Streaming/Unknown: {stats.get('Uncategorized', 0)}")
return channels
def save_channels(channels):
"""Save channels to file."""
# Backup
if os.path.exists('channels.txt'):
backup = f"channels_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
shutil.copy2('channels.txt', backup)
print(f"📋 Backup: {backup}")
try:
with open('channels.txt', 'w', encoding='utf-8') as f:
for i, channel in enumerate(channels):
if i > 0:
f.write("\n\n")
f.write(f"Group = {channel.get('Group', 'Uncategorized')}\n")
f.write(f"Stream name = {channel.get('Stream name', 'Unknown')}\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', '')}\n")
print(f"✅ Saved {len(channels)} channels")
return True
except Exception as e:
print(f"❌ Save error: {e}")
return False
def generate_m3u(channels):
"""Generate M3U playlist."""
try:
with open('playlist.m3u', 'w', encoding='utf-8') as f:
f.write('#EXTM3U\n')
for channel in channels:
name = channel.get('Stream name', '')
group = channel.get('Group', 'Uncategorized')
logo = channel.get('Logo', '')
epg_id = channel.get('EPG id', '')
url = channel.get('Stream URL', '')
if name and url:
f.write(f'#EXTINF:-1 group-title="{group}"')
if logo:
f.write(f' tvg-logo="{logo}"')
if epg_id:
f.write(f' tvg-id="{epg_id}"')
f.write(f',{name}\n{url}\n')
print("✅ Generated playlist.m3u")
return True
except Exception as e:
print(f"❌ M3U error: {e}")
return False
def main():
"""Main function."""
print("📺 Traditional Broadcasters Only - Country Organization")
print("=" * 60)
channels = load_channels()
if not channels:
return False
# Reorganize
channels = reorganize_channels(channels)
# Sort: Countries first, then Uncategorized last
channels.sort(key=lambda x: (
"zzz" if x.get('Group') == "Uncategorized" else x.get('Group', ''),
x.get('Stream name', '')
))
# Save and generate
if not save_channels(channels):
return False
if not generate_m3u(channels):
return False
# Clear import
try:
with open('bulk_import.m3u', 'w', encoding='utf-8') as f:
f.write('#EXTM3U\n')
print("🧹 Cleared import file")
except:
pass
print("\n🎉 COMPLETED!")
print("✅ Traditional broadcasters organized by country")
print("✅ All streaming platforms → Uncategorized")
return True
if __name__ == "__main__":
success = main()
exit(0 if success else 1)