Fix @handle URL resolution using --flat-playlist

- Updated extract_channel_id() to use --flat-playlist instead of --playlist-items 0
- Append /videos to URLs to ensure playlist metadata is available
- Extract playlist_channel_id and playlist_channel from flat-playlist output
- Increased timeout to 15 seconds for network fetch
- All URL formats now working: direct IDs, /channel/ URLs, @handle URLs

Tested and verified in Docker container.
This commit is contained in:
2026-05-08 19:29:48 -04:00
parent 3726cb5775
commit 71ba63ffe3

21
app.py
View File

@@ -69,19 +69,28 @@ def extract_channel_id(url_or_id):
# Format: youtube.com/@handle or youtube.com/c/name or youtube.com/user/name
# These require fetching the page to get the actual channel ID
if parsed.netloc in ['youtube.com', 'www.youtube.com', 'm.youtube.com']:
# Use yt-dlp to extract channel ID from URL
# Use yt-dlp with --flat-playlist to extract channel ID without downloading
# Append /videos if not already present to ensure we get playlist metadata
fetch_url = url_or_id
if not fetch_url.endswith('/videos'):
fetch_url = fetch_url.rstrip('/') + '/videos'
try:
result = subprocess.run(
['yt-dlp', '--dump-json', '--playlist-items', '0', url_or_id],
['yt-dlp', '--flat-playlist', '--dump-json', '--playlist-items', '1', fetch_url],
capture_output=True,
text=True,
timeout=10
timeout=15
)
if result.returncode == 0 and result.stdout:
data = json.loads(result.stdout.split('\n')[0])
channel_id = data.get('channel_id')
channel_name = data.get('channel')
# Get first line of JSON output
lines = [l for l in result.stdout.split('\n') if l.strip()]
if lines:
data = json.loads(lines[0])
# flat-playlist returns playlist_channel_id instead of channel_id
channel_id = data.get('playlist_channel_id') or data.get('channel_id')
channel_name = data.get('playlist_channel') or data.get('channel')
if channel_id:
return channel_id, channel_name