Update .forgejo/workflows/generate-m3u.yml

This commit is contained in:
stoney420 2025-06-29 05:24:32 +02:00
parent 477de72c75
commit 4ff00de812

View file

@ -1,16 +1,4 @@
name: Generate M3U Playlist - Enhanced Country Detection name: Generate M3U Playlist - Enhanced Country Detection
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build-and-organize:
runs-on: ubuntu-22.04
name: Generate M3U Playlist with Auto-Organization
on: on:
push: push:
branches: branches:
@ -40,10 +28,7 @@ jobs:
- name: Create Required Directories - name: Create Required Directories
run: | run: |
echo "🏗️ Setting up directory structure..." echo "🏗️ Setting up directory structure..."
mkdir -p reports/daily mkdir -p reports/daily reports/archive backups logs
mkdir -p reports/archive
mkdir -p backups
mkdir -p logs
echo "✅ Directory structure created" echo "✅ Directory structure created"
- name: Install Python Dependencies - name: Install Python Dependencies
@ -59,7 +44,6 @@ jobs:
if [ -f bulk_import.m3u ]; then if [ -f bulk_import.m3u ]; then
LINES=$(wc -l < bulk_import.m3u) LINES=$(wc -l < bulk_import.m3u)
echo "📺 Found bulk_import.m3u with $LINES lines" echo "📺 Found bulk_import.m3u with $LINES lines"
# Show first few lines for debugging # Show first few lines for debugging
echo "📄 First 10 lines of import file:" echo "📄 First 10 lines of import file:"
head -10 bulk_import.m3u head -10 bulk_import.m3u
@ -73,13 +57,10 @@ jobs:
run: | run: |
echo "🚀 Starting playlist generation..." echo "🚀 Starting playlist generation..."
cd scripts cd scripts
# Make the script executable # Make the script executable
chmod +x generate_playlist.py chmod +x generate_playlist.py
# Run the main script with enhanced logging # Run the main script with enhanced logging
python3 generate_playlist.py 2>&1 | tee ../logs/generation.log python3 generate_playlist.py 2>&1 | tee ../logs/generation.log
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "✅ Playlist generation completed successfully" echo "✅ Playlist generation completed successfully"
else else
@ -92,19 +73,16 @@ jobs:
- name: Validate Generated Files - name: Validate Generated Files
run: | run: |
echo "🔍 Validating generated files..." echo "🔍 Validating generated files..."
# Check if playlist was generated # Check if playlist was generated
if [ -f playlist.m3u ]; then if [ -f playlist.m3u ]; then
CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0")
echo "✅ Generated playlist.m3u with $CHANNELS channels" echo "✅ Generated playlist.m3u with $CHANNELS channels"
# Show playlist statistics # Show playlist statistics
GROUPS=$(grep -o 'group-title="[^"]*"' playlist.m3u | sort -u | wc -l || echo "0") GROUPS=$(grep -o 'group-title="[^"]*"' playlist.m3u | sort -u | wc -l || echo "0")
echo "📊 Organized into $GROUPS groups" echo "📊 Organized into $GROUPS groups"
else else
echo "⚠️ No playlist.m3u generated" echo "⚠️ No playlist.m3u generated"
fi fi
# Check channels.txt # Check channels.txt
if [ -f channels.txt ]; then if [ -f channels.txt ]; then
CHANNEL_BLOCKS=$(grep -c "^Stream name =" channels.txt || echo "0") CHANNEL_BLOCKS=$(grep -c "^Stream name =" channels.txt || echo "0")
@ -159,26 +137,34 @@ jobs:
fi fi
- name: Cleanup and Archive - name: Cleanup and Archive
run:
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Configure Git
run: | run: |
git config --local user.email "actions@forgejo.plainrock127.xyz" echo "🧹 Running cleanup and archiving tasks..."
git config --local user.name "IPTV Playlist Bot"
- name: Create Required Directories # Clean Old Reports
run: | echo "Cleaning old daily reports..."
echo "🏗️ Setting up directories..." REPORT_DIR="reports/daily"
mkdir -p reports/daily reports/archive backups logs if [ -d "$REPORT_DIR" ]; then
echo "✅ Directories created" cd "$REPORT_DIR"
if ls *.md >/dev/null 2>&1; then
COUNT=$(ls *.md | wc -l)
echo "Found $COUNT reports"
if [ "$COUNT" -gt 3 ]; then
echo "Removing excess reports..."
ls -t *.md | tail -n +4 | xargs rm -f
echo "Cleanup done"
fi
else
echo "No reports to clean in $REPORT_DIR"
fi
cd - > /dev/null # Go back to original directory
else
echo "Daily reports directory $REPORT_DIR not found."
fi
# Reset Import File
echo "Resetting bulk_import.m3u..."
echo '#EXTM3U' > bulk_import.m3u
echo "Import file reset"
- name: Create Country Fix Script - name: Create Country Fix Script
run: | run: |
@ -189,10 +175,8 @@ jobs:
import re import re
import shutil import shutil
from datetime import datetime from datetime import datetime
def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url=""): def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url=""):
text = f"{channel_name} {epg_id} {logo_url} {stream_url}".lower() text = f"{channel_name} {epg_id} {logo_url} {stream_url}".lower()
# EPG ID suffix detection (highest priority) # EPG ID suffix detection (highest priority)
if ".ca" in epg_id.lower(): if ".ca" in epg_id.lower():
return "🇨🇦 Canada" return "🇨🇦 Canada"
@ -206,9 +190,7 @@ def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url="")
return "🇦🇺 Australia" return "🇦🇺 Australia"
elif ".jp" in epg_id.lower(): elif ".jp" in epg_id.lower():
return "🇯🇵 Japan" return "🇯🇵 Japan"
channel_lower = channel_name.lower() channel_lower = channel_name.lower()
# Specific fixes for misclassified channels # Specific fixes for misclassified channels
if any(x in channel_lower for x in ["tsn 1", "tsn 2", "tsn 3", "tsn 4", "tsn 5", "cbc news toronto"]): if any(x in channel_lower for x in ["tsn 1", "tsn 2", "tsn 3", "tsn 4", "tsn 5", "cbc news toronto"]):
return "🇨🇦 Canada" return "🇨🇦 Canada"
@ -222,7 +204,6 @@ def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url="")
return "🇵🇭 Philippines" return "🇵🇭 Philippines"
if "animax" in channel_lower: if "animax" in channel_lower:
return "🇯🇵 Japan" return "🇯🇵 Japan"
# Platform-based detection # Platform-based detection
if "pluto.tv" in text or "images.pluto.tv" in text: if "pluto.tv" in text or "images.pluto.tv" in text:
pluto_overrides = { pluto_overrides = {
@ -233,10 +214,8 @@ def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url="")
if pattern in channel_lower: if pattern in channel_lower:
return country return country
return "🇺🇸 United States" return "🇺🇸 United States"
if "plex.tv" in text: if "plex.tv" in text:
return "🇺🇸 United States" return "🇺🇸 United States"
# Country pattern matching # Country pattern matching
patterns = { 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"], "🇺🇸 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"],
@ -255,87 +234,65 @@ def detect_country_enhanced(channel_name, epg_id="", logo_url="", stream_url="")
"🇲🇽 Mexico": ["mexico", "mexican", "televisa", "tv azteca"], "🇲🇽 Mexico": ["mexico", "mexican", "televisa", "tv azteca"],
"🇷🇺 Russia": ["russia", "russian", "первый", "россия", "нтв"] "🇷🇺 Russia": ["russia", "russian", "первый", "россия", "нтв"]
} }
for country, keywords in patterns.items(): for country, keywords in patterns.items():
if any(keyword in text for keyword in keywords): if any(keyword in text for keyword in keywords):
return country return country
return "🌍 International" return "🌍 International"
def fix_channels(): def fix_channels():
backup_name = f"channels_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt" backup_name = f"channels_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
shutil.copy2("channels.txt", backup_name) shutil.copy2("channels.txt", backup_name)
print(f"📋 Created backup: {backup_name}") print(f"📋 Created backup: {backup_name}")
with open("channels.txt", 'r', encoding='utf-8') as f: with open("channels.txt", 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
blocks = content.split('\n\n') blocks = content.split('\n\n')
fixed_channels = [] fixed_channels = []
changes_made = 0 changes_made = 0
country_stats = {} country_stats = {}
for block in blocks: for block in blocks:
if not block.strip(): if not block.strip():
continue continue
lines = block.strip().split('\n') lines = block.strip().split('\n')
channel_data = {} channel_data = {}
for line in lines: for line in lines:
if '=' in line: if '=' in line:
key, value = line.split('=', 1) key, value = line.split('=', 1)
channel_data[key.strip()] = value.strip() channel_data[key.strip()] = value.strip()
if not channel_data: if not channel_data:
continue continue
current_group = channel_data.get('Group', 'Uncategorized') current_group = channel_data.get('Group', 'Uncategorized')
stream_name = channel_data.get('Stream name', '') stream_name = channel_data.get('Stream name', '')
epg_id = channel_data.get('EPG id', '') epg_id = channel_data.get('EPG id', '')
logo = channel_data.get('Logo', '') logo = channel_data.get('Logo', '')
stream_url = channel_data.get('Stream URL', '') stream_url = channel_data.get('Stream URL', '')
detected_country = detect_country_enhanced(stream_name, epg_id, logo, stream_url) detected_country = detect_country_enhanced(stream_name, epg_id, logo, stream_url)
if current_group != detected_country: if current_group != detected_country:
print(f"🔄 Fix: '{stream_name}' {current_group} → {detected_country}") print(f"🔄 Fix: '{stream_name}' {current_group} → {detected_country}")
channel_data['Group'] = detected_country channel_data['Group'] = detected_country
changes_made += 1 changes_made += 1
country_stats[channel_data['Group']] = country_stats.get(channel_data['Group'], 0) + 1 country_stats[channel_data['Group']] = country_stats.get(channel_data['Group'], 0) + 1
fixed_block = [] fixed_block = []
for key in ['Group', 'Stream name', 'Logo', 'EPG id', 'Stream URL']: for key in ['Group', 'Stream name', 'Logo', 'EPG id', 'Stream URL']:
fixed_block.append(f"{key} = {channel_data.get(key, '')}") fixed_block.append(f"{key} = {channel_data.get(key, '')}")
fixed_channels.append('\n'.join(fixed_block)) fixed_channels.append('\n'.join(fixed_block))
with open("channels.txt", 'w', encoding='utf-8') as f: with open("channels.txt", 'w', encoding='utf-8') as f:
f.write('\n\n'.join(fixed_channels)) f.write('\n\n'.join(fixed_channels))
print(f"✅ Changes made: {changes_made}") print(f"✅ Changes made: {changes_made}")
print(f"📺 Total channels: {len(fixed_channels)}") print(f"📺 Total channels: {len(fixed_channels)}")
# Show top countries # Show top countries
sorted_countries = sorted(country_stats.items(), key=lambda x: x[1], reverse=True) sorted_countries = sorted(country_stats.items(), key=lambda x: x[1], reverse=True)
print("🌍 Top Countries:") print("🌍 Top Countries:")
for country, count in sorted_countries[:10]: for country, count in sorted_countries[:10]:
percentage = (count / len(fixed_channels) * 100) if len(fixed_channels) > 0 else 0 percentage = (count / len(fixed_channels) * 100) if len(fixed_channels) > 0 else 0
print(f" {country}: {count} ({percentage:.1f}%)") print(f" {country}: {count} ({percentage:.1f}%)")
# Calculate success rate # Calculate success rate
unclassified = country_stats.get('Uncategorized', 0) + country_stats.get('🌍 International', 0) unclassified = country_stats.get('Uncategorized', 0) + country_stats.get('🌍 International', 0)
success_rate = ((len(fixed_channels) - unclassified) / len(fixed_channels) * 100) if len(fixed_channels) > 0 else 0 success_rate = ((len(fixed_channels) - unclassified) / len(fixed_channels) * 100) if len(fixed_channels) > 0 else 0
print(f"🎯 Detection Success Rate: {success_rate:.1f}%") print(f"🎯 Detection Success Rate: {success_rate:.1f}%")
return len(fixed_channels), changes_made return len(fixed_channels), changes_made
if __name__ == "__main__": if __name__ == "__main__":
total, changes = fix_channels() total, changes = fix_channels()
print(f"🎉 Country fix completed! {changes} channels reclassified.") print(f"🎉 Country fix completed! {changes} channels reclassified.")
EOF EOF
chmod +x country_fix.py chmod +x country_fix.py
echo "✅ Country fix script created" echo "✅ Country fix script created"
@ -343,7 +300,6 @@ EOF
run: | run: |
echo "🔧 Running enhanced country classification fix..." echo "🔧 Running enhanced country classification fix..."
python3 country_fix.py python3 country_fix.py
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "✅ Country classification fix completed successfully" echo "✅ Country classification fix completed successfully"
else else
@ -354,41 +310,30 @@ EOF
- name: Generate Enhanced M3U Playlist - name: Generate Enhanced M3U Playlist
run: | run: |
echo "📺 Generating enhanced M3U playlist..." echo "📺 Generating enhanced M3U playlist..."
python3 -c "import sys
python3 -c "
import sys
print('🚀 Generating M3U from fixed channels...') print('🚀 Generating M3U from fixed channels...')
# Read channels.txt # Read channels.txt
with open('channels.txt', 'r', encoding='utf-8') as f: with open('channels.txt', 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
blocks = content.split('\n\n') blocks = content.split('\n\n')
# Generate M3U # Generate M3U
with open('playlist.m3u', 'w', encoding='utf-8') as f: with open('playlist.m3u', 'w', encoding='utf-8') as f:
f.write('#EXTM3U\n') f.write('#EXTM3U\n')
valid_channels = 0 valid_channels = 0
for block in blocks: for block in blocks:
if not block.strip(): if not block.strip():
continue continue
lines = block.strip().split('\n') lines = block.strip().split('\n')
channel_data = {} channel_data = {}
for line in lines: for line in lines:
if '=' in line: if '=' in line:
key, value = line.split('=', 1) key, value = line.split('=', 1)
channel_data[key.strip()] = value.strip() channel_data[key.strip()] = value.strip()
stream_name = channel_data.get('Stream name', '') stream_name = channel_data.get('Stream name', '')
group = channel_data.get('Group', '') group = channel_data.get('Group', '')
logo = channel_data.get('Logo', '') logo = channel_data.get('Logo', '')
epg_id = channel_data.get('EPG id', '') epg_id = channel_data.get('EPG id', '')
url = channel_data.get('Stream URL', '') url = channel_data.get('Stream URL', '')
if stream_name and url: if stream_name and url:
f.write(f'#EXTINF:-1 group-title=\"{group}\"') f.write(f'#EXTINF:-1 group-title=\"{group}\"')
if logo: if logo:
@ -398,14 +343,11 @@ with open('playlist.m3u', 'w', encoding='utf-8') as f:
f.write(f',{stream_name}\n') f.write(f',{stream_name}\n')
f.write(f'{url}\n') f.write(f'{url}\n')
valid_channels += 1 valid_channels += 1
print(f'✅ Generated playlist.m3u with {valid_channels} channels')"
print(f'✅ Generated playlist.m3u with {valid_channels} channels')
"
- name: Validate Results and Generate Report - name: Validate Results and Generate Report
run: | run: |
echo "🔍 Validating results..." echo "🔍 Validating results..."
if [ -f channels.txt ]; then if [ -f channels.txt ]; then
CHANNEL_COUNT=$(grep -c "^Stream name =" channels.txt || echo "0") CHANNEL_COUNT=$(grep -c "^Stream name =" channels.txt || echo "0")
echo "✅ channels.txt: $CHANNEL_COUNT channels" echo "✅ channels.txt: $CHANNEL_COUNT channels"
@ -413,7 +355,6 @@ print(f'✅ Generated playlist.m3u with {valid_channels} channels')
echo "❌ channels.txt not found" echo "❌ channels.txt not found"
exit 1 exit 1
fi fi
if [ -f playlist.m3u ]; then if [ -f playlist.m3u ]; then
PLAYLIST_COUNT=$(grep -c "^#EXTINF" playlist.m3u || echo "0") PLAYLIST_COUNT=$(grep -c "^#EXTINF" playlist.m3u || echo "0")
echo "✅ playlist.m3u: $PLAYLIST_COUNT channels" echo "✅ playlist.m3u: $PLAYLIST_COUNT channels"
@ -441,11 +382,9 @@ print(f'✅ Generated playlist.m3u with {valid_channels} channels')
echo "- **$country:** $count channels" >> "$REPORT" echo "- **$country:** $count channels" >> "$REPORT"
done done
fi fi
echo "" >> "$REPORT" echo "" >> "$REPORT"
echo "---" >> "$REPORT" echo "---" >> "$REPORT"
echo "*Enhanced country detection with 99%+ accuracy*" >> "$REPORT" echo "*Enhanced country detection with 99%+ accuracy*" >> "$REPORT"
echo "📊 Report created: $REPORT" echo "📊 Report created: $REPORT"
- name: Commit Enhanced Results - name: Commit Enhanced Results
@ -467,7 +406,8 @@ print(f'✅ Generated playlist.m3u with {valid_channels} channels')
- name: Final Summary - name: Final Summary
run: | run: |
echo "🎉 ENHANCED COUNTRY DETECTION COMPLETE!" echo "🎉 ENHANCED COUNTRY DETECTION COMPLETE!"
echo "=" * 50 # Fixed the echo syntax for repeating characters
echo "=================================================="
if [ -f playlist.m3u ]; then if [ -f playlist.m3u ]; then
CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0") CHANNELS=$(grep -c "^#EXTINF" playlist.m3u || echo "0")
COUNTRIES=$(grep -o 'group-title="[^"]*"' playlist.m3u | sed 's/group-title="//;s/"//' | sort -u | wc -l || echo "0") COUNTRIES=$(grep -o 'group-title="[^"]*"' playlist.m3u | sed 's/group-title="//;s/"//' | sort -u | wc -l || echo "0")