#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
VAVOO M3U Batch Cleanup Script - FIXED VERSION
Bereinigt alle M3U-Dateien für VLC-Kompatibilität
"""

import os
import re
import sys
import glob
from pathlib import Path
from datetime import datetime

def cleanup_m3u_content(content, filename=""):
    """
    Bereinigt den Inhalt einer M3U-Datei für VLC
    
    Korrigiert:
    1. Entfernt http-user-agent und http-referrer aus EXTINF-Tags
    2. Stellt sicher, dass beide Header in #EXTVLCOPT vorhanden sind
    3. Entfernt doppelte #EXTVLCOPT-Tags
    
    Args:
        content: Originaler Dateiinhalt
        filename: Dateiname für Logging
    
    Returns:
        Tuple (bereinigter_inhalt, anzahl_veraenderungen)
    """
    if not content:
        return content, 0
    
    original_content = content
    changes = 0
    
    # 1. Entferne http-user-agent und http-referrer aus EXTINF-Tags
    pattern_extinf = r'(#EXTINF:-1[^,]+) http-user-agent="[^"]+" http-referrer="[^"]+",'
    extinf_matches = list(re.finditer(pattern_extinf, content))
    changes += len(extinf_matches)
    
    # Ersetze die gefundenen Muster
    cleaned_content = re.sub(pattern_extinf, r'\1,', content)
    
    # 2. Korrigiere #EXTVLCOPT-Tags für VLC
    # VLC benötigt BEIDE Header in separaten #EXTVLCOPT-Zeilen
    lines = cleaned_content.split('\n')
    corrected_lines = []
    
    i = 0
    while i < len(lines):
        line = lines[i]
        
        if line.startswith('#EXTINF:'):
            # EXTINF-Zeile - behalten
            corrected_lines.append(line)
            
            # Prüfe die nächsten Zeilen für Header-Tags
            user_agent_found = False
            referrer_found = False
            tags_to_skip = []
            
            # Schaue bis zu 5 Zeilen voraus
            for j in range(1, 6):
                if i + j >= len(lines):
                    break
                    
                next_line = lines[i + j]
                
                if next_line.startswith('#EXTVLCOPT:'):
                    if 'http-user-agent' in next_line:
                        user_agent_found = True
                    elif 'http-referrer' in next_line:
                        referrer_found = True
                    tags_to_skip.append(i + j)
                
                elif next_line.startswith('#KODIPROP:'):
                    tags_to_skip.append(i + j)
                
                elif next_line.startswith('#EXTHTTP:'):
                    tags_to_skip.append(i + j)
                
                elif next_line.startswith('http'):  # URL-Zeile
                    break
            
            # Füge korrigierte #EXTVLCOPT-Tags hinzu
            if not user_agent_found:
                corrected_lines.append('#EXTVLCOPT:http-user-agent=VAVOO/1.0')
                changes += 1
            
            if not referrer_found:
                corrected_lines.append('#EXTVLCOPT:http-referrer=https://vavoo.to/')
                changes += 1
            
            # Überspringe die bereits verarbeiteten Tag-Zeilen
            i += 1
            while i in tags_to_skip:
                i += 1
        
        elif line.startswith('#EXTVLCOPT:'):
            # Diese sollten bereits oben verarbeitet worden sein
            # Wenn nicht, behalte sie bei
            if line not in corrected_lines:
                corrected_lines.append(line)
            i += 1
        
        else:
            # Alle anderen Zeilen behalten
            corrected_lines.append(line)
            i += 1
    
    # 3. Entferne doppelte aufeinanderfolgende #EXTVLCOPT-Tags
    final_lines = []
    i = 0
    while i < len(corrected_lines):
        line = corrected_lines[i]
        
        if line.startswith('#EXTVLCOPT:'):
            # Prüfe ob nächste Zeile auch EXTVLCOPT ist
            if i + 1 < len(corrected_lines) and corrected_lines[i + 1].startswith('#EXTVLCOPT:'):
                # Beide behalten (sollten User-Agent und Referrer sein)
                final_lines.append(line)
                final_lines.append(corrected_lines[i + 1])
                i += 2
                continue
        
        final_lines.append(line)
        i += 1
    
    cleaned_content = '\n'.join(final_lines)
    
    return cleaned_content, changes

def process_m3u_file(input_path, output_path):
    """
    Verarbeitet eine einzelne M3U-Datei
    
    Args:
        input_path: Pfad zur Eingabedatei
        output_path: Pfad für Ausgabedatei
    
    Returns:
        Tuple (success, stats_dict)
    """
    stats = {
        'filename': os.path.basename(input_path),
        'original_size': 0,
        'new_size': 0,
        'channel_count': 0,
        'changes_made': 0,
        'success': False,
        'error': None
    }
    
    try:
        # Datei lesen
        with open(input_path, 'r', encoding='utf-8') as f:
            content = f.read()
        
        stats['original_size'] = len(content)
        
        # Kanäle zählen
        stats['channel_count'] = content.count('#EXTINF:')
        
        # Inhalt bereinigen
        cleaned_content, changes = cleanup_m3u_content(content, stats['filename'])
        stats['changes_made'] = changes
        
        # Sicherstellen, dass Ausgabeordner existiert
        os.makedirs(os.path.dirname(output_path), exist_ok=True)
        
        # Bereinigte Datei speichern
        with open(output_path, 'w', encoding='utf-8') as f:
            f.write(cleaned_content)
        
        stats['new_size'] = len(cleaned_content)
        stats['success'] = True
        
        return True, stats
        
    except Exception as e:
        stats['error'] = str(e)
        return False, stats

def find_m3u_files(source_dir, recursive=False):
    """
    Findet alle M3U-Dateien im Verzeichnis
    
    Args:
        source_dir: Quellverzeichnis
        recursive: Auch Unterordner durchsuchen
    
    Returns:
        Liste von M3U-Dateipfaden
    """
    m3u_files = []
    
    # Suche nach .m3u und .m3u8 Dateien
    patterns = ['*.m3u', '*.m3u8']
    
    for pattern in patterns:
        if recursive:
            search_pattern = os.path.join(source_dir, '**', pattern)
        else:
            search_pattern = os.path.join(source_dir, pattern)
        
        for filepath in glob.glob(search_pattern, recursive=recursive):
            # Ignoriere bereits modifizierte Dateien
            if '_modded' not in os.path.basename(filepath):
                m3u_files.append(filepath)
    
    return sorted(m3u_files)

def create_summary_report(stats_list, output_dir):
    """
    Erstellt eine Zusammenfassung der Verarbeitung
    """
    report_path = os.path.join(output_dir, 'cleanup_report.txt')
    
    total_files = len(stats_list)
    successful_files = sum(1 for s in stats_list if s['success'])
    failed_files = total_files - successful_files
    total_channels = sum(s['channel_count'] for s in stats_list if s['success'])
    total_changes = sum(s['changes_made'] for s in stats_list if s['success'])
    
    with open(report_path, 'w', encoding='utf-8') as f:
        f.write("=" * 70 + "\n")
        f.write("VAVOO M3U CLEANUP REPORT - VLC FIXED VERSION\n")
        f.write("=" * 70 + "\n")
        f.write(f"Erstellungszeit: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
        f.write(f"Verarbeitete Dateien: {total_files}\n")
        f.write(f"Erfolgreich: {successful_files}\n")
        f.write(f"Fehlgeschlagen: {failed_files}\n")
        f.write(f"Gesamte Kanäle: {total_channels}\n")
        f.write(f"Gesamte Änderungen: {total_changes}\n")
        f.write("\n" + "=" * 70 + "\n")
        f.write("KORREKTUREN DURCHGEFÜHRT:\n")
        f.write("=" * 70 + "\n")
        f.write("1. http-user-agent und http-referrer aus EXTINF-Tags entfernt\n")
        f.write("2. #EXTVLCOPT:http-user-agent=VAVOO/1.0 hinzugefügt (falls fehlend)\n")
        f.write("3. #EXTVLCOPT:http-referrer=https://vavoo.to/ hinzugefügt (falls fehlend)\n")
        f.write("4. Doppelte Tags entfernt\n")
        f.write("\n" + "=" * 70 + "\n")
        f.write("BEISPIEL EINES KORRIGIERTEN EINTRAGS:\n")
        f.write("=" * 70 + "\n")
        f.write("#EXTINF:-1 tvg-id=\"None\" tvg-name=\"ARD DAS ERSTE\" group-title=\"Germany\",ARD DAS ERSTE\n")
        f.write("#EXTVLCOPT:http-user-agent=VAVOO/1.0\n")
        f.write("#EXTVLCOPT:http-referrer=https://vavoo.to/\n")
        f.write("https://vavoo.to/play3/550012320.m3u8/index.m3u8\n")
        f.write("\n" + "=" * 70 + "\n")
        f.write("DETAILIERTE STATISTIK\n")
        f.write("=" * 70 + "\n\n")
        
        for stats in stats_list:
            f.write(f"Datei: {stats['filename']}\n")
            if stats['success']:
                f.write(f"  Status: ERFOLGREICH\n")
                f.write(f"  Kanäle: {stats['channel_count']}\n")
                f.write(f"  Änderungen: {stats['changes_made']}\n")
                f.write(f"  Originalgröße: {stats['original_size']:,} Bytes\n")
                f.write(f"  Neue Größe: {stats['new_size']:,} Bytes\n")
                if stats['original_size'] > 0:
                    reduction = stats['original_size'] - stats['new_size']
                    reduction_pct = (reduction / stats['original_size'] * 100)
                    f.write(f"  Reduktion: {reduction:,} Bytes ({reduction_pct:.1f}%)\n")
            else:
                f.write(f"  Status: FEHLGESCHLAGEN\n")
                f.write(f"  Fehler: {stats['error']}\n")
            f.write("\n")
    
    print(f"\nReport gespeichert: {report_path}")
    return report_path

def show_example_entry(content, filename):
    """
    Zeigt einen Beispiel-Eintrag aus der Datei
    """
    lines = content.split('\n')
    
    print(f"\nBeispiel aus {filename}:")
    print("-" * 50)
    
    # Finde ersten vollständigen Kanal-Eintrag
    for i, line in enumerate(lines):
        if line.startswith('#EXTINF:'):
            # Zeige diesen Eintrag und die nächsten 4 Zeilen
            for j in range(i, min(i + 5, len(lines))):
                print(lines[j])
            print("-" * 50)
            break

def main():
    """Hauptfunktion"""
    print("=" * 70)
    print("VAVOO M3U BATCH CLEANUP - VLC FIXED VERSION")
    print("=" * 70)
    
    # Standard-Pfade
    default_source_dir = "/home/philipp/raid/vavoo"
    default_output_dir = "/home/philipp/raid/vavoo/modded"
    
    # Kommandozeilen-Argumente verarbeiten
    if len(sys.argv) > 1:
        source_dir = sys.argv[1]
    else:
        source_dir = default_source_dir
    
    if len(sys.argv) > 2:
        output_dir = sys.argv[2]
    else:
        output_dir = default_output_dir
    
    # Prüfe Quellverzeichnis
    if not os.path.exists(source_dir):
        print(f"FEHLER: Quellverzeichnis existiert nicht: {source_dir}")
        return
    
    # M3U-Dateien finden (inkl. .m3u8)
    print(f"\nSuche M3U/M3U8-Dateien in: {source_dir}")
    m3u_files = find_m3u_files(source_dir, recursive=False)
    
    if not m3u_files:
        print("Keine M3U/M3U8-Dateien gefunden!")
        return
    
    print(f"Gefundene Dateien: {len(m3u_files)}")
    for i, filepath in enumerate(m3u_files, 1):
        print(f"  {i:2d}. {os.path.basename(filepath)}")
    
    # Bestätigung
    print(f"\nAusgabeverzeichnis: {output_dir}")
    proceed = input("\nFortfahren? (j/n): ").lower()
    if proceed != 'j':
        print("Abgebrochen.")
        return
    
    # Verarbeite alle Dateien
    print("\n" + "=" * 70)
    print("VERARBEITUNG")
    print("=" * 70)
    
    all_stats = []
    processed_count = 0
    
    for input_path in m3u_files:
        filename = os.path.basename(input_path)
        filename_no_ext = os.path.splitext(filename)[0]
        
        # Behalte .m3u Erweiterung für Ausgabe
        output_filename = f"{filename_no_ext}_modded.m3u"
        output_path_full = os.path.join(output_dir, output_filename)
        
        print(f"\n[{processed_count + 1}/{len(m3u_files)}] Verarbeite: {filename}")
        
        success, stats = process_m3u_file(input_path, output_path_full)
        all_stats.append(stats)
        
        if success:
            print(f"  ✓ Erfolgreich: {stats['channel_count']} Kanäle, {stats['changes_made']} Änderungen")
            print(f"  → Gespeichert als: {output_filename}")
            
            # Beispiel anzeigen
            with open(output_path_full, 'r', encoding='utf-8') as f:
                output_content = f.read()
            show_example_entry(output_content, output_filename)
            
            processed_count += 1
        else:
            print(f"  ✗ Fehler: {stats['error']}")
    
    # Zusammenfassung
    print("\n" + "=" * 70)
    print("ZUSAMMENFASSUNG")
    print("=" * 70)
    
    successful_stats = [s for s in all_stats if s['success']]
    
    if successful_stats:
        total_channels = sum(s['channel_count'] for s in successful_stats)
        total_changes = sum(s['changes_made'] for s in successful_stats)
        
        print(f"Erfolgreich verarbeitet: {len(successful_stats)}/{len(m3u_files)} Dateien")
        print(f"Gesamte Kanäle: {total_channels}")
        print(f"Gesamte Änderungen: {total_changes}")
        
        # Report erstellen
        report_path = create_summary_report(all_stats, output_dir)
        
        # URLs anzeigen
        print(f"\n" + "=" * 70)
        print("ZUGRIFFS-URLS")
        print("=" * 70)
        print("Modifizierte Playlists sind verfügbar unter:")
        print(f"  http://vavoo.weltzien.ddns.net/modded/")
        
        for stats in successful_stats[:3]:  # Erste 3 anzeigen
            if 'index' in stats['filename'].lower():
                output_file = f"{os.path.splitext(stats['filename'])[0]}_modded.m3u"
                print(f"  http://vavoo.weltzien.ddns.net/modded/{output_file}")
    
    print("\n" + "=" * 70)
    print("FERTIG! Die Playlists sind jetzt VLC-kompatibel.")
    print("=" * 70)

if __name__ == "__main__":
    main()
