From e3afe3947c13074e93e51db8bb8dced0f7219882 Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 10 Sep 2021 15:07:38 +0700 Subject: [PATCH] allowing to add playlist to queue --- tubearchivist/home/src/download.py | 15 +++++++++++++++ tubearchivist/home/src/helper.py | 10 +++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tubearchivist/home/src/download.py b/tubearchivist/home/src/download.py index 2f185fd..0631ddc 100644 --- a/tubearchivist/home/src/download.py +++ b/tubearchivist/home/src/download.py @@ -41,6 +41,9 @@ class PendingList: url, limit=False ) missing_videos = missing_videos + youtube_ids + elif url_type == 'playlist': + youtube_ids = playlist_extractor(url) + missing_videos = missing_videos + youtube_ids return missing_videos @@ -366,6 +369,18 @@ class ChannelSubscription: channel_handler.sync_to_videos() +def playlist_extractor(playlist_id): + """ return youtube_ids from a playlist_id """ + url = 'https://www.youtube.com/playlist?list=' + playlist_id + obs = { + 'default_search': 'ytsearch', 'quiet': True, 'ignoreerrors': True, + 'skip_download': True, 'extract_flat': True + } + playlist = youtube_dl.YoutubeDL(obs).extract_info(url, download=False) + playlist_vids = [(i['id'], i['title']) for i in playlist['entries']] + return playlist_vids + + class VideoDownloader: """ handle the video download functionality """ diff --git a/tubearchivist/home/src/helper.py b/tubearchivist/home/src/helper.py index ba8a920..b4210dd 100644 --- a/tubearchivist/home/src/helper.py +++ b/tubearchivist/home/src/helper.py @@ -42,16 +42,24 @@ def clean_string(file_name): def process_url_list(url_str): """ parse url_list to find valid youtube video or channel ids """ + to_replace = ['watch?v=', 'playlist?list='] url_list = re.split('\n+', url_str[0]) youtube_ids = [] for url in url_list: - url_clean = url.strip().split('/')[-1].replace('watch?v=', '') + url_clean = url.strip().split('/')[-1] + for i in to_replace: + url_clean = url_clean.replace(i, '') url_no_param = url_clean.split('&')[0] str_len = len(url_no_param) if str_len == 11: link_type = 'video' elif str_len == 24: link_type = 'channel' + elif str_len == 34: + link_type = 'playlist' + else: + # unable to parse + return False youtube_ids.append({"url": url_no_param, "type": link_type})