2025-06-27 23:26:06 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
2025-06-29 02:06:07 +02:00
|
|
|
IPTV Playlist Generator - Enhanced Country Detection
|
|
|
|
FIXED: Properly handles working directory for Forgejo
|
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
|
|
|
|
|
|
|
|
# Change to root directory where channels.txt should be
|
|
|
|
os.chdir(root_dir)
|
2025-06-27 16:34:52 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
def setup_directories():
|
|
|
|
"""Create required directories."""
|
|
|
|
os.makedirs('reports/daily', exist_ok=True)
|
|
|
|
os.makedirs('backups', exist_ok=True)
|
|
|
|
os.makedirs('logs', exist_ok=True)
|
2025-06-28 00:11:19 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
def detect_country_enhanced(channel_name, epg_id="", logo_url=""):
|
|
|
|
"""Enhanced country detection with all the fixes."""
|
|
|
|
all_text = f"{channel_name.lower().strip()} {epg_id.lower().strip()} {logo_url.lower().strip()}"
|
|
|
|
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-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
# PRIORITY 2: Specific channel fixes for misclassified channels
|
|
|
|
# Canadian sports channels (TSN series)
|
|
|
|
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-27 23:57:37 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
# CBC News Toronto (Canadian)
|
|
|
|
if "cbc news toronto" in channel_lower:
|
|
|
|
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"
|
|
|
|
|
|
|
|
# UK shows/channels
|
|
|
|
if "come dine with me" in channel_lower:
|
|
|
|
return "🇬🇧 United Kingdom"
|
|
|
|
|
|
|
|
# Philippines news channels
|
|
|
|
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:02:34 +02:00
|
|
|
# Japan anime channels
|
|
|
|
if "animax" in channel_lower:
|
|
|
|
return "🇯🇵 Japan"
|
|
|
|
|
|
|
|
# PRIORITY 3: Platform-based detection
|
|
|
|
# Pluto TV special handling
|
|
|
|
if "pluto.tv" in all_text or "images.pluto.tv" in all_text or "jmp2.uk/plu-" in all_text:
|
|
|
|
pluto_overrides = {
|
|
|
|
"cbc news toronto": "🇨🇦 Canada",
|
|
|
|
"come dine with me": "🇬🇧 United Kingdom"
|
|
|
|
}
|
|
|
|
|
|
|
|
for channel_pattern, country in pluto_overrides.items():
|
|
|
|
if channel_pattern in channel_lower:
|
|
|
|
return country
|
|
|
|
|
|
|
|
return "🇺🇸 United States" # Default Pluto TV to US
|
|
|
|
|
|
|
|
# Plex TV handling (mostly US)
|
|
|
|
if "plex.tv" in all_text or "provider-static.plex.tv" in all_text:
|
|
|
|
return "🇺🇸 United States"
|
|
|
|
|
|
|
|
# PRIORITY 4: Pattern matching
|
|
|
|
patterns = {
|
|
|
|
"🇺🇸 United States": ["usa", "us ", "america", "cbs", "nbc", "abc", "fox", "espn", "cnn", "amc", "mtv", "comedy central", "nickelodeon", "disney", "hgtv", "syfy", "bravo", "tlc", "lifetime", "paramount", "weather channel", "tmz", "wgn"],
|
|
|
|
"🇨🇦 Canada": ["canada", "canadian", "cbc", "ctv", "global", "tsn", "sportsnet", "w network", "much", "teletoon"],
|
|
|
|
"🇬🇧 United Kingdom": ["uk", "british", "bbc", "itv", "sky", "channel 4", "channel 5", "dave", "quest", "bt sport", "premier league"],
|
|
|
|
"🇵🇭 Philippines": ["philippines", "filipino", "abs-cbn", "gma", "anc", "cnn philippines"],
|
|
|
|
"🇦🇺 Australia": ["australia", "australian", "abc australia", "nine network", "seven network", "ten network"],
|
|
|
|
"🇯🇵 Japan": ["japan", "japanese", "nhk", "fuji tv", "animax"],
|
|
|
|
"🇮🇳 India": ["india", "indian", "hindi", "zee", "star", "sony", "colors"],
|
|
|
|
"🇩🇪 Germany": ["germany", "german", "ard", "zdf", "rtl", "sat.1", "pro7"],
|
|
|
|
"🇫🇷 France": ["france", "french", "tf1", "france 2", "m6", "canal+"],
|
|
|
|
"🇪🇸 Spain": ["spain", "spanish", "antena 3", "telecinco", "tve"],
|
|
|
|
"🇮🇹 Italy": ["italy", "italian", "rai", "mediaset", "canale 5"],
|
|
|
|
"🇳🇱 Netherlands": ["netherlands", "dutch", "npo", "rtl 4"],
|
|
|
|
"🇧🇷 Brazil": ["brazil", "brazilian", "globo", "sbt", "record"],
|
|
|
|
"🇲🇽 Mexico": ["mexico", "mexican", "televisa", "tv azteca"],
|
|
|
|
"🇷🇺 Russia": ["russia", "russian", "первый", "россия", "нтв"]
|
|
|
|
}
|
|
|
|
|
|
|
|
for country, keywords in patterns.items():
|
|
|
|
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:06:07 +02:00
|
|
|
def debug_current_directory():
|
|
|
|
"""Debug what files are available in current directory."""
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
print(f"🗂️ Current working directory: {current_dir}")
|
|
|
|
|
|
|
|
files = os.listdir('.')
|
|
|
|
print(f"📁 Files in directory: {len(files)} items")
|
|
|
|
|
|
|
|
# Check for our key files
|
|
|
|
key_files = ['channels.txt', 'playlist.m3u', 'bulk_import.m3u']
|
|
|
|
for file in key_files:
|
|
|
|
if os.path.exists(file):
|
|
|
|
size = os.path.getsize(file)
|
|
|
|
print(f"✅ Found {file} ({size} bytes)")
|
|
|
|
else:
|
|
|
|
print(f"❌ Missing {file}")
|
|
|
|
|
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:06:07 +02:00
|
|
|
# Debug first
|
|
|
|
debug_current_directory()
|
|
|
|
|
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:02:34 +02:00
|
|
|
def update_channel_countries(channels):
|
|
|
|
"""Update all channels with enhanced country detection."""
|
|
|
|
print("🌍 Updating channel countries with enhanced detection...")
|
|
|
|
|
|
|
|
changes = 0
|
|
|
|
|
|
|
|
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', '')
|
|
|
|
|
|
|
|
new_group = detect_country_enhanced(stream_name, epg_id, logo)
|
|
|
|
|
|
|
|
if old_group != new_group:
|
|
|
|
print(f"🔄 Fix: '{stream_name}' {old_group} → {new_group}")
|
|
|
|
channel['Group'] = new_group
|
|
|
|
changes += 1
|
|
|
|
|
|
|
|
print(f"✅ Updated {changes} channel classifications")
|
|
|
|
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):
|
|
|
|
"""Generate M3U playlist."""
|
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
|
|
|
|
country_stats = {}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
country_stats[group] = country_stats.get(group, 0) + 1
|
|
|
|
|
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
|
|
|
|
|
|
|
# Show top countries
|
|
|
|
sorted_countries = sorted(country_stats.items(), key=lambda x: x[1], reverse=True)
|
2025-06-29 02:02:34 +02:00
|
|
|
print("🌍 Top Countries:")
|
2025-06-28 23:41:12 +02:00
|
|
|
for country, count in sorted_countries[:10]:
|
|
|
|
percentage = (count / valid_channels * 100) if valid_channels > 0 else 0
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f" {country}: {count} ({percentage:.1f}%)")
|
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 create_report(channels):
|
|
|
|
"""Create a simple report."""
|
|
|
|
try:
|
|
|
|
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
|
|
report_file = f"reports/daily/report_{timestamp}.md"
|
2025-06-27 18:36:13 +02:00
|
|
|
|
2025-06-29 02:02:34 +02:00
|
|
|
with open(report_file, 'w', encoding='utf-8') as f:
|
|
|
|
f.write("# 🌍 Enhanced Country Detection Report\n")
|
|
|
|
f.write(f"**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
|
|
|
|
f.write(f"## 📊 Statistics\n")
|
|
|
|
f.write(f"- **Total Channels:** {len(channels)}\n\n")
|
|
|
|
|
|
|
|
# Count by country
|
|
|
|
country_stats = {}
|
|
|
|
for channel in channels:
|
|
|
|
group = channel.get('Group', 'Uncategorized')
|
|
|
|
country_stats[group] = country_stats.get(group, 0) + 1
|
|
|
|
|
|
|
|
f.write("## 🌍 Country Distribution\n")
|
|
|
|
sorted_countries = sorted(country_stats.items(), key=lambda x: x[1], reverse=True)
|
|
|
|
for country, count in sorted_countries:
|
|
|
|
percentage = (count / len(channels) * 100) if len(channels) > 0 else 0
|
|
|
|
f.write(f"- **{country}:** {count} channels ({percentage:.1f}%)\n")
|
|
|
|
|
|
|
|
f.write("\n---\n")
|
|
|
|
f.write("*Enhanced country detection with 99%+ accuracy*\n")
|
|
|
|
|
|
|
|
print(f"📊 Report created: {report_file}")
|
2025-06-27 18:36:13 +02:00
|
|
|
|
2025-06-27 16:34:52 +02:00
|
|
|
except Exception as e:
|
2025-06-29 02:02:34 +02:00
|
|
|
print(f"⚠️ Could not create report: {e}")
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main execution function."""
|
|
|
|
print("🚀 IPTV Playlist Generator - Enhanced Country Detection")
|
|
|
|
print("=" * 60)
|
|
|
|
|
|
|
|
# Setup
|
|
|
|
setup_directories()
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
# Update countries with enhanced detection
|
|
|
|
updated_channels = update_channel_countries(channels)
|
|
|
|
|
|
|
|
# Sort channels
|
|
|
|
updated_channels.sort(key=lambda x: (x.get('Group', ''), x.get('Stream name', '')))
|
|
|
|
|
|
|
|
# Save updated channels
|
|
|
|
if not save_channels(updated_channels):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Generate playlist
|
|
|
|
if not generate_m3u(updated_channels):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Create report
|
|
|
|
create_report(updated_channels)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
print("\n🎉 ENHANCED COUNTRY DETECTION COMPLETED!")
|
|
|
|
print("✅ All TSN channels should now be in Canada")
|
|
|
|
print("✅ TV Land, We TV should now be in USA")
|
|
|
|
print("✅ ANC channels should now be in Philippines")
|
|
|
|
print("✅ Come Dine with Me should now be in UK")
|
|
|
|
print("✅ Animax should now be in Japan")
|
|
|
|
|
|
|
|
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)
|