diff --git a/app.py b/app.py index cf70f22..f55cc93 100644 --- a/app.py +++ b/app.py @@ -69,22 +69,31 @@ 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 + if channel_id: + return channel_id, channel_name except: pass except: