2025-06-27 23:26:06 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2025-06-29 02:35:11 +02:00
|
|
|
IPTV Country + Platform Organizer
|
|
|
|
Groups channels by country first, then platform within country
|
|
|
|
Example: 🇺🇸 USA, 🇺🇸 USA - Plex, 🇺🇸 USA - Pluto, 🇨🇦 Canada, 🇨🇦 Canada - Plex
|
2025-06-27 23:26:06 +02:00
|
|
|
"""
|
|
|
|
|
2025-06-27 16:34:52 +02:00
|
|
|
import os
|
2025-06-29 02:06:07 +02:00
|
|
|
import sys
|
2025-06-29 02:02:34 +02:00
|
|
|
import shutil
|
2025-06-27 17:36:03 +02:00
|
|
|
from datetime import datetime
|
2025-06-29 02:06:07 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
# FIXED: Ensure we're in the right directory
|
|
|
|
script_dir = Path(__file__).parent
|
|
|
|
root_dir = script_dir.parent
|
|
|
|
os.chdir(root_dir)
|
2025-06-27 16:34:52 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
def detect_country_and_platform(channel_name, epg_id="", logo_url="", stream_url=""):
|
|
|
|
"""Enhanced country + platform detection."""
|
|
|
|
all_text = f"{channel_name.lower().strip()} {epg_id.lower().strip()} {logo_url.lower().strip()} {stream_url.lower().strip()}"
|
|
|
|
channel_lower = channel_name.lower()
|
|
|
|
|
|
|
|
# STEP 1: Detect the country first
|
|
|
|
country = detect_base_country(channel_name, epg_id, logo_url, stream_url)
|
|
|
|
|
|
|
|
# STEP 2: Detect platform
|
|
|
|
platform = detect_platform(all_text)
|
|
|
|
|
|
|
|
# STEP 3: Combine country + platform
|
|
|
|
if platform:
|
|
|
|
return f"{country} - {platform}"
|
|
|
|
else:
|
|
|
|
return country
|
2025-06-28 00:11:19 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
def detect_base_country(channel_name, epg_id="", logo_url="", stream_url=""):
|
|
|
|
"""Detect the base country of the channel."""
|
|
|
|
all_text = f"{channel_name.lower().strip()} {epg_id.lower().strip()} {logo_url.lower().strip()} {stream_url.lower().strip()}"
|
2025-06-29 02:02:34 +02:00
|
|
|
channel_lower = channel_name.lower()
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
# PRIORITY 1: EPG ID suffix detection (most reliable)
|
|
|
|
if ".ca" in epg_id.lower():
|
|
|
|
return "🇨🇦 Canada"
|
|
|
|
elif ".us" in epg_id.lower():
|
|
|
|
return "🇺🇸 United States"
|
|
|
|
elif ".uk" in epg_id.lower():
|
|
|
|
return "🇬🇧 United Kingdom"
|
|
|
|
elif ".ph" in epg_id.lower():
|
|
|
|
return "🇵🇭 Philippines"
|
|
|
|
elif ".au" in epg_id.lower():
|
|
|
|
return "🇦🇺 Australia"
|
|
|
|
elif ".jp" in epg_id.lower():
|
|
|
|
return "🇯🇵 Japan"
|
2025-06-29 02:35:11 +02:00
|
|
|
elif ".my" in epg_id.lower():
|
|
|
|
return "🇲🇾 Malaysia"
|
|
|
|
elif ".de" in epg_id.lower():
|
|
|
|
return "🇩🇪 Germany"
|
|
|
|
elif ".fr" in epg_id.lower():
|
|
|
|
return "🇫🇷 France"
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
# PRIORITY 2: Specific channel fixes for misclassified channels
|
2025-06-29 02:35:11 +02:00
|
|
|
|
|
|
|
# Canadian channels
|
2025-06-29 02:02:34 +02:00
|
|
|
if any(x in channel_lower for x in ["tsn 1", "tsn 2", "tsn 3", "tsn 4", "tsn 5", "tsn1", "tsn2", "tsn3", "tsn4", "tsn5"]):
|
|
|
|
return "🇨🇦 Canada"
|
2025-06-29 02:35:11 +02:00
|
|
|
if any(x in channel_lower for x in ["cbc news", "cbc news toronto", "cbc news british columbia"]):
|
|
|
|
return "🇨🇦 Canada"
|
|
|
|
if "w network" in channel_lower and "canada" in all_text:
|
2025-06-29 02:02:34 +02:00
|
|
|
return "🇨🇦 Canada"
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
# US channels that were misclassified
|
|
|
|
if any(x in channel_lower for x in ["tv land", "tvland", "we tv", "wetv", "all weddings we tv", "cheaters", "cheers", "christmas 365"]):
|
|
|
|
return "🇺🇸 United States"
|
2025-06-29 02:35:11 +02:00
|
|
|
if any(x in channel_lower for x in ["cbs", "nbc", "abc", "fox news", "cnn", "espn", "discovery channel", "cartoon network"]):
|
|
|
|
return "🇺🇸 United States"
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# UK channels
|
2025-06-29 02:02:34 +02:00
|
|
|
if "come dine with me" in channel_lower:
|
|
|
|
return "🇬🇧 United Kingdom"
|
2025-06-29 02:35:11 +02:00
|
|
|
if any(x in channel_lower for x in ["bbc", "itv", "sky news", "channel 4", "channel 5"]):
|
|
|
|
return "🇬🇧 United Kingdom"
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Philippines channels
|
2025-06-29 02:02:34 +02:00
|
|
|
if any(x in channel_lower for x in ["anc global", "anc ph"]):
|
|
|
|
return "🇵🇭 Philippines"
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Malaysia channels
|
|
|
|
if "malaysia" in channel_lower or "bein sports 1 malaysia" in channel_lower:
|
|
|
|
return "🇲🇾 Malaysia"
|
|
|
|
|
|
|
|
# Japan channels
|
2025-06-29 02:02:34 +02:00
|
|
|
if "animax" in channel_lower:
|
|
|
|
return "🇯🇵 Japan"
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# PRIORITY 3: Special platform handling with country detection
|
|
|
|
|
|
|
|
# Pluto TV regional detection
|
2025-06-29 02:02:34 +02:00
|
|
|
if "pluto.tv" in all_text or "images.pluto.tv" in all_text or "jmp2.uk/plu-" in all_text:
|
2025-06-29 02:35:11 +02:00
|
|
|
pluto_countries = {
|
2025-06-29 02:02:34 +02:00
|
|
|
"cbc news toronto": "🇨🇦 Canada",
|
2025-06-29 02:35:11 +02:00
|
|
|
"cbc news british columbia": "🇨🇦 Canada",
|
2025-06-29 02:02:34 +02:00
|
|
|
"come dine with me": "🇬🇧 United Kingdom"
|
|
|
|
}
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
for channel_pattern, country in pluto_countries.items():
|
2025-06-29 02:02:34 +02:00
|
|
|
if channel_pattern in channel_lower:
|
|
|
|
return country
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Default Pluto TV to US
|
|
|
|
return "🇺🇸 United States"
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Samsung TV Plus regional detection
|
|
|
|
if "samsung" in all_text or "sam-" in stream_url:
|
|
|
|
if "cabc" in epg_id.lower(): # Canadian Samsung channels
|
|
|
|
return "🇨🇦 Canada"
|
|
|
|
return "🇺🇸 United States" # Default Samsung to US
|
|
|
|
|
|
|
|
# Plex TV (mostly US unless specifically regional)
|
2025-06-29 02:02:34 +02:00
|
|
|
if "plex.tv" in all_text or "provider-static.plex.tv" in all_text:
|
|
|
|
return "🇺🇸 United States"
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# PRIORITY 4: Pattern matching by keywords
|
|
|
|
country_patterns = {
|
|
|
|
"🇺🇸 United States": ["usa", "america", "united states", "c-span", "newsmax", "newsnation"],
|
|
|
|
"🇨🇦 Canada": ["canada", "canadian", "ctv", "global", "sportsnet"],
|
|
|
|
"🇬🇧 United Kingdom": ["uk", "british", "britain", "england"],
|
|
|
|
"🇵🇭 Philippines": ["philippines", "filipino"],
|
|
|
|
"🇦🇺 Australia": ["australia", "australian"],
|
|
|
|
"🇯🇵 Japan": ["japan", "japanese", "nhk"],
|
|
|
|
"🇲🇾 Malaysia": ["malaysia", "malaysian"],
|
|
|
|
"🇩🇪 Germany": ["germany", "german", "deutschland"],
|
|
|
|
"🇫🇷 France": ["france", "french"],
|
|
|
|
"🇪🇸 Spain": ["spain", "spanish"],
|
|
|
|
"🇮🇹 Italy": ["italy", "italian"],
|
|
|
|
"🇧🇷 Brazil": ["brazil", "brazilian"],
|
|
|
|
"🇲🇽 Mexico": ["mexico", "mexican"],
|
|
|
|
"🇷🇺 Russia": ["russia", "russian"]
|
2025-06-29 02:02:34 +02:00
|
|
|
}
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
for country, keywords in country_patterns.items():
|
2025-06-29 02:02:34 +02:00
|
|
|
if any(keyword in all_text for keyword in keywords):
|
|
|
|
return country
|
|
|
|
|
|
|
|
return "🌍 International"
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
def detect_platform(all_text):
|
|
|
|
"""Detect streaming platform."""
|
|
|
|
|
|
|
|
# Platform detection patterns
|
|
|
|
if "pluto.tv" in all_text or "images.pluto.tv" in all_text or "jmp2.uk/plu-" in all_text:
|
|
|
|
return "Pluto TV"
|
|
|
|
elif "plex.tv" in all_text or "provider-static.plex.tv" in all_text:
|
|
|
|
return "Plex TV"
|
|
|
|
elif "samsung" in all_text or "sam-" in all_text:
|
|
|
|
return "Samsung TV+"
|
|
|
|
elif "tubi" in all_text:
|
|
|
|
return "Tubi"
|
|
|
|
elif "xumo" in all_text:
|
|
|
|
return "Xumo"
|
|
|
|
elif "crackle" in all_text:
|
|
|
|
return "Crackle"
|
|
|
|
elif "roku" in all_text:
|
|
|
|
return "Roku Channel"
|
|
|
|
elif "youtube" in all_text:
|
|
|
|
return "YouTube"
|
|
|
|
elif "peacock" in all_text:
|
|
|
|
return "Peacock"
|
|
|
|
elif "paramount+" in all_text or "paramount plus" in all_text:
|
|
|
|
return "Paramount+"
|
|
|
|
|
|
|
|
return None # No platform detected = traditional broadcaster
|
2025-06-29 02:06:07 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
def load_channels():
|
|
|
|
"""Load existing channels from channels.txt."""
|
2025-06-28 23:41:12 +02:00
|
|
|
channels = []
|
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
if not os.path.exists('channels.txt'):
|
2025-06-29 02:06:07 +02:00
|
|
|
print("❌ No existing channels.txt found")
|
2025-06-28 23:41:12 +02:00
|
|
|
return channels
|
|
|
|
|
|
|
|
try:
|
2025-06-29 02:02:34 +02:00
|
|
|
with open('channels.txt', 'r', encoding='utf-8') as f:
|
2025-06-28 23:41:12 +02:00
|
|
|
content = f.read()
|
|
|
|
|
2025-06-29 02:06:07 +02:00
|
|
|
print(f"📄 channels.txt size: {len(content)} characters")
|
|
|
|
|
2025-06-28 23:41:12 +02:00
|
|
|
blocks = content.split('\n\n')
|
|
|
|
|
|
|
|
for block in blocks:
|
|
|
|
if not block.strip():
|
|
|
|
continue
|
|
|
|
|
|
|
|
lines = block.strip().split('\n')
|
2025-06-29 02:02:34 +02:00
|
|
|
channel_data = {}
|
2025-06-28 23:41:12 +02:00
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if '=' in line:
|
|
|
|
key, value = line.split('=', 1)
|
|
|
|
channel_data[key.strip()] = value.strip()
|
|
|
|
|
|
|
|
if channel_data and channel_data.get('Stream name'):
|
|
|
|
channels.append(channel_data)
|
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"✅ Loaded {len(channels)} existing channels")
|
2025-06-28 23:41:12 +02:00
|
|
|
|
|
|
|
except Exception as e:
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"❌ Error loading channels: {e}")
|
2025-06-28 23:41:12 +02:00
|
|
|
|
|
|
|
return channels
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
def reorganize_channels_by_country_platform(channels):
|
|
|
|
"""Reorganize channels by country, then platform within country."""
|
|
|
|
print("🌍 Reorganizing channels by country + platform...")
|
2025-06-29 02:02:34 +02:00
|
|
|
|
|
|
|
changes = 0
|
2025-06-29 02:35:11 +02:00
|
|
|
group_stats = {}
|
2025-06-29 02:02:34 +02:00
|
|
|
|
|
|
|
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', '')
|
2025-06-29 02:35:11 +02:00
|
|
|
stream_url = channel.get('Stream URL', '')
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Apply country + platform detection
|
|
|
|
new_group = detect_country_and_platform(stream_name, epg_id, logo, stream_url)
|
2025-06-29 02:02:34 +02:00
|
|
|
|
|
|
|
if old_group != new_group:
|
2025-06-29 02:35:11 +02:00
|
|
|
print(f"🔄 Reorg: '{stream_name}' {old_group} → {new_group}")
|
2025-06-29 02:02:34 +02:00
|
|
|
channel['Group'] = new_group
|
|
|
|
changes += 1
|
2025-06-29 02:35:11 +02:00
|
|
|
|
|
|
|
# Count groups
|
|
|
|
group_stats[new_group] = group_stats.get(new_group, 0) + 1
|
|
|
|
|
|
|
|
print(f"✅ Reorganized {changes} channel classifications")
|
|
|
|
|
|
|
|
# Show organization results
|
|
|
|
print(f"\n🗂️ NEW ORGANIZATION:")
|
|
|
|
sorted_groups = sorted(group_stats.items(), key=lambda x: (x[0].split(' - ')[0], x[0]))
|
|
|
|
for group, count in sorted_groups:
|
|
|
|
print(f" {group}: {count} channels")
|
2025-06-29 02:02:34 +02:00
|
|
|
|
|
|
|
return channels
|
|
|
|
|
|
|
|
def save_channels(channels):
|
|
|
|
"""Save channels to channels.txt."""
|
|
|
|
if os.path.exists('channels.txt'):
|
|
|
|
backup_name = f"channels_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
|
|
|
shutil.copy2('channels.txt', backup_name)
|
|
|
|
print(f"📋 Created backup: {backup_name}")
|
|
|
|
|
2025-06-28 23:41:12 +02:00
|
|
|
try:
|
2025-06-29 02:02:34 +02:00
|
|
|
with open('channels.txt', 'w', encoding='utf-8') as f:
|
2025-06-28 23:41:12 +02:00
|
|
|
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")
|
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"✅ Saved {len(channels)} channels to channels.txt")
|
2025-06-28 23:41:12 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"❌ Error saving channels: {e}")
|
2025-06-28 23:41:12 +02:00
|
|
|
return False
|
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
def generate_m3u(channels):
|
2025-06-29 02:35:11 +02:00
|
|
|
"""Generate M3U playlist with country + platform organization."""
|
2025-06-28 23:41:12 +02:00
|
|
|
try:
|
2025-06-29 02:02:34 +02:00
|
|
|
with open('playlist.m3u', 'w', encoding='utf-8') as f:
|
2025-06-28 23:41:12 +02:00
|
|
|
f.write('#EXTM3U\n')
|
|
|
|
|
|
|
|
valid_channels = 0
|
2025-06-29 02:35:11 +02:00
|
|
|
group_stats = {}
|
2025-06-28 23:41:12 +02:00
|
|
|
|
|
|
|
for channel in channels:
|
|
|
|
stream_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 stream_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',{stream_name}\n')
|
|
|
|
f.write(f'{url}\n')
|
|
|
|
valid_channels += 1
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
group_stats[group] = group_stats.get(group, 0) + 1
|
2025-06-28 23:41:12 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"📺 Generated playlist.m3u with {valid_channels} channels")
|
2025-06-28 23:41:12 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Show organized groups
|
|
|
|
print("🌍 Organized Groups:")
|
|
|
|
sorted_groups = sorted(group_stats.items(), key=lambda x: (x[0].split(' - ')[0], x[0]))
|
|
|
|
for group, count in sorted_groups[:15]: # Show top 15
|
|
|
|
print(f" {group}: {count} channels")
|
2025-06-27 18:36:13 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
return True
|
2025-06-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(f"❌ Error generating playlist: {e}")
|
|
|
|
return False
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main execution function."""
|
2025-06-29 02:35:11 +02:00
|
|
|
print("🌍 IPTV Country + Platform Organizer")
|
|
|
|
print("=" * 60)
|
|
|
|
print("Organizing channels: Country first, then platform within country")
|
|
|
|
print("Example: 🇺🇸 USA, 🇺🇸 USA - Plex, 🇨🇦 Canada, 🇨🇦 Canada - Plex")
|
2025-06-29 02:02:34 +02:00
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
# Load existing channels
|
|
|
|
channels = load_channels()
|
|
|
|
|
|
|
|
if not channels:
|
|
|
|
print("❌ No channels found to process")
|
2025-06-27 23:26:06 +02:00
|
|
|
return False
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Reorganize by country + platform
|
|
|
|
reorganized_channels = reorganize_channels_by_country_platform(channels)
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Sort channels: Country first, then platform within country, then channel name
|
|
|
|
print("📝 Sorting channels by country + platform...")
|
|
|
|
reorganized_channels.sort(key=lambda x: (
|
|
|
|
x.get('Group', '').split(' - ')[0], # Country first
|
|
|
|
x.get('Group', ''), # Then platform within country
|
|
|
|
x.get('Stream name', '') # Then channel name
|
|
|
|
))
|
2025-06-29 02:02:34 +02:00
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
# Save reorganized channels
|
|
|
|
if not save_channels(reorganized_channels):
|
2025-06-29 02:02:34 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Generate playlist
|
2025-06-29 02:35:11 +02:00
|
|
|
if not generate_m3u(reorganized_channels):
|
2025-06-29 02:02:34 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Clear import file
|
|
|
|
try:
|
|
|
|
with open('bulk_import.m3u', 'w', encoding='utf-8') as f:
|
|
|
|
f.write('#EXTM3U\n# Import processed\n')
|
|
|
|
print("🧹 Cleared import file")
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2025-06-29 02:35:11 +02:00
|
|
|
print("\n🎉 COUNTRY + PLATFORM ORGANIZATION COMPLETED!")
|
|
|
|
print("✅ Channels organized by country first, then platform")
|
|
|
|
print("✅ TSN channels → Canada")
|
|
|
|
print("✅ CBC News → Canada")
|
|
|
|
print("✅ TV Land → USA")
|
|
|
|
print("✅ Plex/Pluto channels organized within their countries")
|
|
|
|
print("✅ Clean country-based organization achieved!")
|
2025-06-29 02:02:34 +02:00
|
|
|
|
|
|
|
return True
|
2025-06-27 16:34:52 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-06-29 02:02:34 +02:00
|
|
|
success = main()
|
2025-06-27 23:26:06 +02:00
|
|
|
exit(0 if success else 1)
|