From d13a34dfe8c7d635c9f279a816bb879df7002e5f Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 10:36:52 -0500 Subject: [PATCH 1/7] [ie/patreon] Extract multiple embeds Authored by: bashonly --- yt_dlp/extractor/patreon.py | 80 ++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 9381c7eab..8f99ff2fc 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -10,6 +10,7 @@ from ..utils import ( clean_html, determine_ext, int_or_none, + make_archive_id, mimetype2ext, parse_iso8601, str_or_none, @@ -51,7 +52,7 @@ class PatreonIE(PatreonBaseIE): 'url': 'http://www.patreon.com/creation?hid=743933', 'md5': 'e25505eec1053a6e6813b8ed369875cc', 'info_dict': { - 'id': '743933', + 'id': '743933-1', 'ext': 'mp3', 'title': 'Episode 166: David Smalley of Dogma Debate', 'description': 'md5:34d207dd29aa90e24f1b3f58841b81c7', @@ -66,6 +67,7 @@ class PatreonIE(PatreonBaseIE): 'channel_id': '80642', 'channel_url': 'https://www.patreon.com/dissonancepod', 'channel_follower_count': int, + '_old_archive_ids': 'patreon 743933', }, }, { 'url': 'http://www.patreon.com/creation?hid=754133', @@ -237,7 +239,6 @@ class PatreonIE(PatreonBaseIE): title = attributes['title'].strip() image = attributes.get('image') or {} info = { - 'id': video_id, 'title': title, 'description': clean_html(attributes.get('content')), 'thumbnail': image.get('large_url') or image.get('url'), @@ -245,14 +246,15 @@ class PatreonIE(PatreonBaseIE): 'like_count': int_or_none(attributes.get('like_count')), 'comment_count': int_or_none(attributes.get('comment_count')), } - can_view_post = traverse_obj(attributes, 'current_user_can_view') - if can_view_post and info['comment_count']: - info['__post_extractor'] = self.extract_comments(video_id) - for i in post.get('included', []): - i_type = i.get('type') - if i_type == 'media': - media_attributes = i.get('attributes') or {} + entries = [] + idx = 0 + + for include in traverse_obj(post, ('included', ...)): + include_type = include.get('type') + if include_type == 'media': + idx += 1 + media_attributes = include.get('attributes') or {} download_url = media_attributes.get('download_url') ext = mimetype2ext(media_attributes.get('mimetype')) @@ -260,33 +262,38 @@ class PatreonIE(PatreonBaseIE): # See: https://github.com/yt-dlp/yt-dlp/issues/4608 size_bytes = int_or_none(media_attributes.get('size_bytes')) if download_url and ext in KNOWN_EXTENSIONS and size_bytes is not None: - # XXX: what happens if there are multiple attachments? - return { - **info, + entries.append({ + 'id': f'{video_id}-{idx}', 'ext': ext, 'filesize': size_bytes, 'url': download_url, - } - elif i_type == 'user': - user_attributes = i.get('attributes') + '_old_archive_ids': make_archive_id(PatreonIE, video_id), + }) + elif include_type == 'user': + user_attributes = include.get('attributes') if user_attributes: info.update({ 'uploader': user_attributes.get('full_name'), - 'uploader_id': str_or_none(i.get('id')), + 'uploader_id': str_or_none(include.get('id')), 'uploader_url': user_attributes.get('url'), }) - elif i_type == 'post_tag': - info.setdefault('tags', []).append(traverse_obj(i, ('attributes', 'value'))) + elif include_type == 'post_tag': + info.setdefault('tags', []).append(traverse_obj(include, ('attributes', 'value'))) - elif i_type == 'campaign': + elif include_type == 'campaign': info.update({ - 'channel': traverse_obj(i, ('attributes', 'title')), - 'channel_id': str_or_none(i.get('id')), - 'channel_url': traverse_obj(i, ('attributes', 'url')), - 'channel_follower_count': int_or_none(traverse_obj(i, ('attributes', 'patron_count'))), + 'channel': traverse_obj(include, ('attributes', 'title')), + 'channel_id': str_or_none(include.get('id')), + 'channel_url': traverse_obj(include, ('attributes', 'url')), + 'channel_follower_count': int_or_none(traverse_obj(include, ('attributes', 'patron_count'))), }) + for entry in entries: + entry.update(info) + + info['id'] = video_id + # handle Vimeo embeds if traverse_obj(attributes, ('embed', 'provider')) == 'Vimeo': v_url = urllib.parse.unquote(self._html_search_regex( @@ -296,36 +303,45 @@ class PatreonIE(PatreonBaseIE): v_url, video_id, 'Checking Vimeo embed URL', headers={'Referer': 'https://patreon.com/'}, fatal=False, errnote=False): - return self.url_result( + entries.append(self.url_result( VimeoIE._smuggle_referrer(v_url, 'https://patreon.com/'), - VimeoIE, url_transparent=True, **info) + VimeoIE, url_transparent=True, **info)) embed_url = traverse_obj(attributes, ('embed', 'url', {url_or_none})) if embed_url and self._request_webpage(embed_url, video_id, 'Checking embed URL', fatal=False, errnote=False): - return self.url_result(embed_url, **info) + entries.append(self.url_result(embed_url, **info)) post_file = traverse_obj(attributes, 'post_file') if post_file: name = post_file.get('name') ext = determine_ext(name) if ext in KNOWN_EXTENSIONS: - return { + entries.append({ **info, 'ext': ext, 'url': post_file['url'], - } + }) elif name == 'video' or determine_ext(post_file.get('url')) == 'm3u8': formats, subtitles = self._extract_m3u8_formats_and_subtitles(post_file['url'], video_id) - return { + entries.append({ **info, 'formats': formats, 'subtitles': subtitles, - } + }) - if can_view_post is False: + can_view_post = traverse_obj(attributes, 'current_user_can_view') + if can_view_post and info['comment_count']: + info['__post_extractor'] = self.extract_comments(video_id) + + if not entries and can_view_post is False: self.raise_no_formats('You do not have access to this post', video_id=video_id, expected=True) - else: + elif not entries: self.raise_no_formats('No supported media found in this post', video_id=video_id, expected=True) + elif len(entries) == 1: + return {**info, **entries[0]} + else: + return self.playlist_result(entries, **info) + # return only metadata for --ignore-no-formats-error return info def _get_comments(self, post_id): From 23d159f4250e52a8c2905e8b0496890d57e50114 Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 10:58:46 -0500 Subject: [PATCH 2/7] Keep old video id when possible Authored by: bashonly --- yt_dlp/extractor/patreon.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 8f99ff2fc..7bb107877 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -10,7 +10,6 @@ from ..utils import ( clean_html, determine_ext, int_or_none, - make_archive_id, mimetype2ext, parse_iso8601, str_or_none, @@ -52,7 +51,7 @@ class PatreonIE(PatreonBaseIE): 'url': 'http://www.patreon.com/creation?hid=743933', 'md5': 'e25505eec1053a6e6813b8ed369875cc', 'info_dict': { - 'id': '743933-1', + 'id': '743933', 'ext': 'mp3', 'title': 'Episode 166: David Smalley of Dogma Debate', 'description': 'md5:34d207dd29aa90e24f1b3f58841b81c7', @@ -67,7 +66,6 @@ class PatreonIE(PatreonBaseIE): 'channel_id': '80642', 'channel_url': 'https://www.patreon.com/dissonancepod', 'channel_follower_count': int, - '_old_archive_ids': 'patreon 743933', }, }, { 'url': 'http://www.patreon.com/creation?hid=754133', @@ -267,7 +265,6 @@ class PatreonIE(PatreonBaseIE): 'ext': ext, 'filesize': size_bytes, 'url': download_url, - '_old_archive_ids': make_archive_id(PatreonIE, video_id), }) elif include_type == 'user': user_attributes = include.get('attributes') @@ -338,7 +335,7 @@ class PatreonIE(PatreonBaseIE): elif not entries: self.raise_no_formats('No supported media found in this post', video_id=video_id, expected=True) elif len(entries) == 1: - return {**info, **entries[0]} + return {**entries[0], **info} else: return self.playlist_result(entries, **info) # return only metadata for --ignore-no-formats-error From fd8b4fbea096adf142fb42c53be10dfe57789f2a Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 11:03:17 -0500 Subject: [PATCH 3/7] Cleanup Authored by: bashonly --- yt_dlp/extractor/patreon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 7bb107877..3ff0e1e2b 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -251,7 +251,6 @@ class PatreonIE(PatreonBaseIE): for include in traverse_obj(post, ('included', ...)): include_type = include.get('type') if include_type == 'media': - idx += 1 media_attributes = include.get('attributes') or {} download_url = media_attributes.get('download_url') ext = mimetype2ext(media_attributes.get('mimetype')) @@ -260,6 +259,7 @@ class PatreonIE(PatreonBaseIE): # See: https://github.com/yt-dlp/yt-dlp/issues/4608 size_bytes = int_or_none(media_attributes.get('size_bytes')) if download_url and ext in KNOWN_EXTENSIONS and size_bytes is not None: + idx += 1 entries.append({ 'id': f'{video_id}-{idx}', 'ext': ext, From 96d002e335c32c4667b82ce22824e8d02fefdde3 Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 11:47:24 -0500 Subject: [PATCH 4/7] Add test Authored by: bashonly --- yt_dlp/extractor/patreon.py | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 3ff0e1e2b..a35b83b00 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -219,6 +219,82 @@ class PatreonIE(PatreonBaseIE): 'thumbnail': r're:^https?://.+', }, 'params': {'skip_download': 'm3u8'}, + }, { + # multiple attachments/embeds + 'url': 'https://www.patreon.com/posts/holy-wars-solos-100601977', + 'playlist_count': 3, + 'info_dict': { + 'id': '100601977', + 'title': '"Holy Wars" (Megadeth) Solos Transcription & Lesson/Analysis', + 'description': 'md5:d099ab976edfce6de2a65c2b169a88d3', + 'uploader': 'Bradley Hall', + 'uploader_id': '24401883', + 'uploader_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_id': '3193932', + 'channel_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_follower_count': int, + 'timestamp': 1710777855, + 'upload_date': '20240318', + 'like_count': int, + 'comment_count': int, + 'thumbnail': r're:^https?://.+', + }, + 'playlist': [{ + 'info_dict': { + 'id': '100601977-1', + 'ext': 'wav', + 'title': '"Holy Wars" (Megadeth) Solos Transcription & Lesson/Analysis', + 'description': 'md5:d099ab976edfce6de2a65c2b169a88d3', + 'uploader': 'Bradley Hall', + 'uploader_id': '24401883', + 'uploader_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_id': '3193932', + 'channel_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_follower_count': int, + 'timestamp': 1710777855, + 'upload_date': '20240318', + 'like_count': int, + 'comment_count': int, + 'thumbnail': r're:^https?://.+', + }, + }, { + 'info_dict': { + 'id': '100601977-2', + 'ext': 'wav', + 'title': '"Holy Wars" (Megadeth) Solos Transcription & Lesson/Analysis', + 'description': 'md5:d099ab976edfce6de2a65c2b169a88d3', + 'uploader': 'Bradley Hall', + 'uploader_id': '24401883', + 'uploader_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_id': '3193932', + 'channel_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_follower_count': int, + 'timestamp': 1710777855, + 'upload_date': '20240318', + 'like_count': int, + 'comment_count': int, + 'thumbnail': r're:^https?://.+', + }, + }, { + 'info_dict': { + 'id': '100601977', + 'ext': 'mp4', + 'title': '"Holy Wars" (Megadeth) Solos Transcription & Lesson/Analysis', + 'description': 'md5:d099ab976edfce6de2a65c2b169a88d3', + 'uploader': 'Bradley Hall', + 'uploader_id': '24401883', + 'uploader_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_id': '3193932', + 'channel_url': 'https://www.patreon.com/bradleyhallguitar', + 'channel_follower_count': int, + 'timestamp': 1710777855, + 'upload_date': '20240318', + 'like_count': int, + 'comment_count': int, + 'thumbnail': r're:^https?://.+', + }, + }], + 'skip': 'Patron-only content', }] def _real_extract(self, url): From 0f4e86d5cc3fa6bee54f945484e3c263e635617b Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 12:31:55 -0500 Subject: [PATCH 5/7] set `_RETURN_TYPE` Authored by: bashonly --- yt_dlp/extractor/patreon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index a35b83b00..0cbec6624 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -296,6 +296,7 @@ class PatreonIE(PatreonBaseIE): }], 'skip': 'Patron-only content', }] + _RETURN_TYPE = 'video' def _real_extract(self, url): video_id = self._match_id(url) From 012da49769468c610e85b242f10c971e7708ccf4 Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 12:50:36 -0500 Subject: [PATCH 6/7] modernize / make safer Authored by: bashonly --- yt_dlp/extractor/patreon.py | 59 ++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 0cbec6624..9f1bb0916 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -311,25 +311,22 @@ class PatreonIE(PatreonBaseIE): 'include': 'audio,user,user_defined_tags,campaign,attachments_media', }) attributes = post['data']['attributes'] - title = attributes['title'].strip() - image = attributes.get('image') or {} - info = { - 'title': title, - 'description': clean_html(attributes.get('content')), - 'thumbnail': image.get('large_url') or image.get('url'), - 'timestamp': parse_iso8601(attributes.get('published_at')), - 'like_count': int_or_none(attributes.get('like_count')), - 'comment_count': int_or_none(attributes.get('comment_count')), - } + info = traverse_obj(attributes, { + 'title': ('title', {str.strip}), + 'description': ('content', {clean_html}), + 'thumbnail': ('image', ('large_url', 'url'), {url_or_none}), + 'timestamp': ('published_at', {parse_iso8601}), + 'like_count': ('like_count', {int_or_none}), + 'comment_count': ('comment_count', {int_or_none}), + }, get_all=False) entries = [] idx = 0 - - for include in traverse_obj(post, ('included', ...)): - include_type = include.get('type') + for include in traverse_obj(post, ('included', lambda _, v: v['type'])): + include_type = include['type'] if include_type == 'media': - media_attributes = include.get('attributes') or {} - download_url = media_attributes.get('download_url') + media_attributes = traverse_obj(include, ('attributes', {dict})) or {} + download_url = url_or_none(media_attributes.get('download_url')) ext = mimetype2ext(media_attributes.get('mimetype')) # if size_bytes is None, this media file is likely unavailable @@ -343,25 +340,25 @@ class PatreonIE(PatreonBaseIE): 'filesize': size_bytes, 'url': download_url, }) + elif include_type == 'user': - user_attributes = include.get('attributes') - if user_attributes: - info.update({ - 'uploader': user_attributes.get('full_name'), - 'uploader_id': str_or_none(include.get('id')), - 'uploader_url': user_attributes.get('url'), - }) + info.update(traverse_obj(include, { + 'uploader': ('attributes', 'full_name', {str}), + 'uploader_id': ('id', {str_or_none}), + 'uploader_url': ('attributes', 'url', {url_or_none}), + })) elif include_type == 'post_tag': - info.setdefault('tags', []).append(traverse_obj(include, ('attributes', 'value'))) + if post_tag := traverse_obj(include, ('attributes', 'value', {str})): + info.setdefault('tags', []).append(post_tag) elif include_type == 'campaign': - info.update({ - 'channel': traverse_obj(include, ('attributes', 'title')), - 'channel_id': str_or_none(include.get('id')), - 'channel_url': traverse_obj(include, ('attributes', 'url')), - 'channel_follower_count': int_or_none(traverse_obj(include, ('attributes', 'patron_count'))), - }) + info.update(traverse_obj(include, { + 'channel': ('attributes', 'title', {str}), + 'channel_id': ('id', {str_or_none}), + 'channel_url': ('attributes', 'url', {url_or_none}), + 'channel_follower_count': ('attributes', 'patron_count', {int_or_none}), + })) for entry in entries: entry.update(info) @@ -385,7 +382,7 @@ class PatreonIE(PatreonBaseIE): if embed_url and self._request_webpage(embed_url, video_id, 'Checking embed URL', fatal=False, errnote=False): entries.append(self.url_result(embed_url, **info)) - post_file = traverse_obj(attributes, 'post_file') + post_file = traverse_obj(attributes, ('post_file', {dict})) if post_file: name = post_file.get('name') ext = determine_ext(name) @@ -404,7 +401,7 @@ class PatreonIE(PatreonBaseIE): }) can_view_post = traverse_obj(attributes, 'current_user_can_view') - if can_view_post and info['comment_count']: + if can_view_post and info.get('comment_count'): info['__post_extractor'] = self.extract_comments(video_id) if not entries and can_view_post is False: From c62a5e5890b83bfe9cf57310a6404483c46a08b9 Mon Sep 17 00:00:00 2001 From: bashonly Date: Fri, 3 May 2024 13:48:51 -0500 Subject: [PATCH 7/7] `any` Authored by: bashonly --- yt_dlp/extractor/patreon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yt_dlp/extractor/patreon.py b/yt_dlp/extractor/patreon.py index 9f1bb0916..b77dcba02 100644 --- a/yt_dlp/extractor/patreon.py +++ b/yt_dlp/extractor/patreon.py @@ -314,11 +314,11 @@ class PatreonIE(PatreonBaseIE): info = traverse_obj(attributes, { 'title': ('title', {str.strip}), 'description': ('content', {clean_html}), - 'thumbnail': ('image', ('large_url', 'url'), {url_or_none}), + 'thumbnail': ('image', ('large_url', 'url'), {url_or_none}, any), 'timestamp': ('published_at', {parse_iso8601}), 'like_count': ('like_count', {int_or_none}), 'comment_count': ('comment_count', {int_or_none}), - }, get_all=False) + }) entries = [] idx = 0