#!/usr/bin/env python3
import subprocess
import json
from pathlib import Path

BASE_DIR = Path("/var/lib/ffplayout/tv-media/Shuffle-for-tv")
SOURCES = [
    "https://communitymedia.video/c/communitymediavideos/videos",
    "https://communitymedia.video/a/video@vod.newellijay.tv/video-channels"
]

def get_local_titles():
    titles = set()
    for f in BASE_DIR.rglob("*.mp4"):
        titles.add(f.stem.lower())
    return titles

def get_peertube_videos():
    videos = []
    for url in SOURCES:
        print(f"Fetching from: {url}")
        try:
            cmd = ["yt-dlp", "--flat-playlist", "--dump-json", "--playlist-items", "1-100", url]  # limit to first 100
            output = subprocess.check_output(cmd, text=True, timeout=60)
            for line in output.strip().split("\n"):
                if line.strip():
                    info = json.loads(line)
                    videos.append({
                        "title": info.get("title", "Unknown"),
                        "url": info.get("webpage_url")
                    })
        except Exception as e:
            print(f"Error: {e}")
    return videos

print("Scanning local files (fast title only)...")
local_titles = get_local_titles()
print(f"Found {len(local_titles)} local video titles.")

print("\nFetching Peertube videos (limited to newest 100 per source)...")
peertube = get_peertube_videos()

print(f"\n=== Missing / New Videos ({len(peertube)} checked) ===")
for v in peertube:
    title_lower = v["title"].lower()
    if not any(title_lower in local or local in title_lower for local in local_titles):
        print(f"  • {v['title']}")
    else:
        print(f"  ✓ Present: {v['title']}")

print("\nDone. This version is much faster.")
