From e5b4e9dbc0eb418827f3fac308dcd8b58118911c Mon Sep 17 00:00:00 2001 From: tcely Date: Wed, 22 Jan 2025 20:07:59 -0500 Subject: [PATCH] Add get_channel_id --- tubesync/sync/youtube.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tubesync/sync/youtube.py b/tubesync/sync/youtube.py index 5fdef3cb..5371c937 100644 --- a/tubesync/sync/youtube.py +++ b/tubesync/sync/youtube.py @@ -45,6 +45,31 @@ def get_yt_opts(): opts.update({'cookiefile': cookie_file_path}) return opts +def get_channel_id(url): + # yt-dlp --simulate --no-check-formats --playlist-items 1 + # --print 'pre_process:%(playlist_channel_id,playlist_id,channel_id)s' + opts = get_yt_opts() + opts.update({ + 'skip_download': True, + 'simulate': True, + 'logger': log, + 'extract_flat': True, # Change to False to get detailed info + 'check_formats': False, + 'playlist_items': '1', + }) + + with yt_dlp.YoutubeDL(opts) as y: + try: + response = y.extract_info(url, download=False) + + channel_id = response['channel_id'] + playlist_id = response['playlist_id'] + playlist_channel_id = response['playlist_channel_id'] + except yt_dlp.utils.DownloadError as e: + raise YouTubeError(f'Failed to extract channel ID for "{url}": {e}') from e + else: + return playlist_channel_id or playlist_id or channel_id + def get_channel_image_info(url): opts = get_yt_opts() opts.update({