]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/patreon.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / patreon.py
index 9381c7eab834ffc0e044303fae4885f20f1f94b9..5dc46e3171b43cc2b67452793cd4d9165b06a695 100644 (file)
@@ -2,6 +2,7 @@
 import urllib.parse
 
 from .common import InfoExtractor
+from .sproutvideo import VidsIoIE
 from .vimeo import VimeoIE
 from ..networking.exceptions import HTTPError
 from ..utils import (
@@ -12,6 +13,7 @@
     int_or_none,
     mimetype2ext,
     parse_iso8601,
+    smuggle_url,
     str_or_none,
     traverse_obj,
     url_or_none,
@@ -33,7 +35,7 @@ def _call_api(self, ep, item_id, query=None, headers=None, fatal=True, note=None
         try:
             return self._download_json(
                 f'https://www.patreon.com/api/{ep}',
-                item_id, note='Downloading API JSON' if not note else note,
+                item_id, note=note if note else 'Downloading API JSON',
                 query=query, fatal=fatal, headers=headers)
         except ExtractorError as e:
             if not isinstance(e.cause, HTTPError) or mimetype2ext(e.cause.response.headers.get('Content-Type')) != 'json':
@@ -113,7 +115,7 @@ class PatreonIE(PatreonBaseIE):
         'params': {
             'noplaylist': True,
             'skip_download': True,
-        }
+        },
     }, {
         'url': 'https://www.patreon.com/posts/episode-166-of-743933',
         'only_matching': True,
@@ -133,7 +135,7 @@ class PatreonIE(PatreonBaseIE):
             'description': 'md5:557a409bd79d3898689419094934ba79',
             'uploader_id': '14936315',
         },
-        'skip': 'Patron-only content'
+        'skip': 'Patron-only content',
     }, {
         # m3u8 video (https://github.com/yt-dlp/yt-dlp/issues/2277)
         'url': 'https://www.patreon.com/posts/video-sketchbook-32452882',
@@ -154,7 +156,7 @@ class PatreonIE(PatreonBaseIE):
             'channel_id': '1641751',
             'channel_url': 'https://www.patreon.com/loish',
             'channel_follower_count': int,
-        }
+        },
     }, {
         # bad videos under media (if media is included). Real one is under post_file
         'url': 'https://www.patreon.com/posts/premium-access-70282931',
@@ -219,7 +221,29 @@ 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?://.+',
+        },
+        'skip': 'Patron-only content',
     }]
+    _RETURN_TYPE = 'video'
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
@@ -234,58 +258,57 @@ def _real_extract(self, url):
                 'include': 'audio,user,user_defined_tags,campaign,attachments_media',
             })
         attributes = post['data']['attributes']
-        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'),
-            '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')),
-        }
-        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 {}
-                download_url = media_attributes.get('download_url')
+        info = traverse_obj(attributes, {
+            'title': ('title', {str.strip}),
+            'description': ('content', {clean_html}),
+            '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}),
+        })
+
+        entries = []
+        idx = 0
+        for include in traverse_obj(post, ('included', lambda _, v: v['type'])):
+            include_type = include['type']
+            if include_type == 'media':
+                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
                 # 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,
+                    idx += 1
+                    entries.append({
+                        'id': f'{video_id}-{idx}',
                         'ext': ext,
                         'filesize': size_bytes,
                         'url': download_url,
-                    }
-            elif i_type == 'user':
-                user_attributes = i.get('attributes')
-                if user_attributes:
-                    info.update({
-                        'uploader': user_attributes.get('full_name'),
-                        'uploader_id': str_or_none(i.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 == 'user':
+                info.update(traverse_obj(include, {
+                    'uploader': ('attributes', 'full_name', {str}),
+                    'uploader_id': ('id', {str_or_none}),
+                    'uploader_url': ('attributes', 'url', {url_or_none}),
+                }))
 
-            elif i_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'))),
-                })
+            elif include_type == 'post_tag':
+                if post_tag := traverse_obj(include, ('attributes', 'value', {str})):
+                    info.setdefault('tags', []).append(post_tag)
+
+            elif include_type == 'campaign':
+                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}),
+                }))
+
+        # all-lowercase 'referer' so we can smuggle it to Generic, SproutVideo, Vimeo
+        headers = {'referer': 'https://patreon.com/'}
 
         # handle Vimeo embeds
         if traverse_obj(attributes, ('embed', 'provider')) == 'Vimeo':
@@ -293,39 +316,55 @@ def _real_extract(self, url):
                 r'(https(?:%3A%2F%2F|://)player\.vimeo\.com.+app_id(?:=|%3D)+\d+)',
                 traverse_obj(attributes, ('embed', 'html', {str})), 'vimeo url', fatal=False) or '')
             if url_or_none(v_url) and self._request_webpage(
-                    v_url, video_id, 'Checking Vimeo embed URL',
-                    headers={'Referer': 'https://patreon.com/'},
-                    fatal=False, errnote=False):
-                return self.url_result(
+                    v_url, video_id, 'Checking Vimeo embed URL', headers=headers, fatal=False, errnote=False):
+                entries.append(self.url_result(
                     VimeoIE._smuggle_referrer(v_url, 'https://patreon.com/'),
-                    VimeoIE, url_transparent=True, **info)
+                    VimeoIE, url_transparent=True))
 
         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)
-
-        post_file = traverse_obj(attributes, 'post_file')
+        if embed_url and (urlh := self._request_webpage(
+                embed_url, video_id, 'Checking embed URL', headers=headers,
+                fatal=False, errnote=False, expected_status=403)):
+            # Password-protected vids.io embeds return 403 errors w/o --video-password or session cookie
+            if urlh.status != 403 or VidsIoIE.suitable(embed_url):
+                entries.append(self.url_result(smuggle_url(embed_url, headers)))
+
+        post_file = traverse_obj(attributes, ('post_file', {dict}))
         if post_file:
             name = post_file.get('name')
             ext = determine_ext(name)
             if ext in KNOWN_EXTENSIONS:
-                return {
-                    **info,
+                entries.append({
+                    'id': video_id,
                     '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 {
-                    **info,
+                entries.append({
+                    'id': video_id,
                     'formats': formats,
                     'subtitles': subtitles,
-                }
+                })
 
-        if can_view_post is False:
+        can_view_post = traverse_obj(attributes, 'current_user_can_view')
+        comments = None
+        if can_view_post and info.get('comment_count'):
+            comments = 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:
+            info.update(entries[0])
+        else:
+            for entry in entries:
+                entry.update(info)
+            return self.playlist_result(entries, video_id, **info, __post_extractor=comments)
+
+        info['id'] = video_id
+        info['__post_extractor'] = comments
         return info
 
     def _get_comments(self, post_id):
@@ -346,7 +385,7 @@ def _get_comments(self, post_id):
 
             params.update({'page[cursor]': cursor} if cursor else {})
             response = self._call_api(
-                f'posts/{post_id}/comments', post_id, query=params, note='Downloading comments page %d' % page)
+                f'posts/{post_id}/comments', post_id, query=params, note=f'Downloading comments page {page}')
 
             cursor = None
             for comment in traverse_obj(response, (('data', ('included', lambda _, v: v['type'] == 'comment')), ...)):
@@ -414,18 +453,18 @@ class PatreonCampaignIE(PatreonBaseIE):
             'uploader_id': '37306634',
             'thumbnail': r're:^https?://.*$',
         },
-        'playlist_mincount': 71
+        'playlist_mincount': 71,
     }, {
         'url': 'https://www.patreon.com/dissonancepod/posts',
-        'only_matching': True
+        'only_matching': True,
     }, {
         'url': 'https://www.patreon.com/m/5932659',
-        'only_matching': True
+        'only_matching': True,
     }]
 
     @classmethod
     def suitable(cls, url):
-        return False if PatreonIE.suitable(url) else super(PatreonCampaignIE, cls).suitable(url)
+        return False if PatreonIE.suitable(url) else super().suitable(url)
 
     def _entries(self, campaign_id):
         cursor = None
@@ -440,7 +479,7 @@ def _entries(self, campaign_id):
         for page in itertools.count(1):
 
             params.update({'page[cursor]': cursor} if cursor else {})
-            posts_json = self._call_api('posts', campaign_id, query=params, note='Downloading posts page %d' % page)
+            posts_json = self._call_api('posts', campaign_id, query=params, note=f'Downloading posts page {page}')
 
             cursor = traverse_obj(posts_json, ('meta', 'pagination', 'cursors', 'next'))
             for post_url in traverse_obj(posts_json, ('data', ..., 'attributes', 'patreon_url')):
@@ -454,13 +493,14 @@ def _real_extract(self, url):
         campaign_id, vanity = self._match_valid_url(url).group('campaign_id', 'vanity')
         if campaign_id is None:
             webpage = self._download_webpage(url, vanity, headers={'User-Agent': self.USER_AGENT})
-            campaign_id = self._search_regex(r'https://www.patreon.com/api/campaigns/(\d+)/?', webpage, 'Campaign ID')
+            campaign_id = self._search_nextjs_data(
+                webpage, vanity)['props']['pageProps']['bootstrapEnvelope']['pageBootstrap']['campaign']['data']['id']
 
         params = {
             'json-api-use-default-includes': 'false',
             'fields[user]': 'full_name,url',
             'fields[campaign]': 'name,summary,url,patron_count,creation_count,is_nsfw,avatar_photo_url',
-            'include': 'creator'
+            'include': 'creator',
         }
 
         campaign_response = self._call_api(