4 from .common
import InfoExtractor
5 from .vimeo
import VimeoIE
6 from ..networking
.exceptions
import HTTPError
22 class PatreonBaseIE(InfoExtractor
):
23 USER_AGENT
= 'Patreon/7.6.28 (Android; Android 11; Scale/2.10)'
25 def _call_api(self
, ep
, item_id
, query
=None, headers
=None, fatal
=True, note
=None):
28 if 'User-Agent' not in headers
:
29 headers
['User-Agent'] = self
.USER_AGENT
31 query
.update({'json-api-version': 1.0}
)
34 return self
._download
_json
(
35 f
'https://www.patreon.com/api/{ep}',
36 item_id
, note
='Downloading API JSON' if not note
else note
,
37 query
=query
, fatal
=fatal
, headers
=headers
)
38 except ExtractorError
as e
:
39 if not isinstance(e
.cause
, HTTPError
) or mimetype2ext(e
.cause
.response
.headers
.get('Content-Type')) != 'json':
41 err_json
= self
._parse
_json
(self
._webpage
_read
_content
(e
.cause
.response
, None, item_id
), item_id
, fatal
=False)
42 err_message
= traverse_obj(err_json
, ('errors', ..., 'detail'), get_all
=False)
44 raise ExtractorError(f
'Patreon said: {err_message}', expected
=True)
48 class PatreonIE(PatreonBaseIE
):
49 _VALID_URL
= r
'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)'
51 'url': 'http://www.patreon.com/creation?hid=743933',
52 'md5': 'e25505eec1053a6e6813b8ed369875cc',
56 'title': 'Episode 166: David Smalley of Dogma Debate',
57 'description': 'md5:34d207dd29aa90e24f1b3f58841b81c7',
58 'uploader': 'Cognitive Dissonance Podcast',
59 'thumbnail': 're:^https?://.*$',
60 'timestamp': 1406473987,
61 'upload_date': '20140727',
62 'uploader_id': '87145',
65 'uploader_url': 'https://www.patreon.com/dissonancepod',
66 'channel_id': '80642',
67 'channel_url': 'https://www.patreon.com/dissonancepod',
68 'channel_follower_count': int,
71 'url': 'http://www.patreon.com/creation?hid=754133',
72 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
76 'title': 'CD 167 Extra',
77 'uploader': 'Cognitive Dissonance Podcast',
78 'thumbnail': 're:^https?://.*$',
81 'uploader_url': 'https://www.patreon.com/dissonancepod',
83 'skip': 'Patron-only content',
85 'url': 'https://www.patreon.com/creation?hid=1682498',
89 'title': 'I\'m on Patreon!',
90 'uploader': 'TraciJHines',
91 'thumbnail': 're:^https?://.*$',
92 'upload_date': '20150211',
93 'description': 'md5:8af6425f50bd46fbf29f3db0fc3a8364',
94 'uploader_id': '@TraciHinesMusic',
95 'categories': ['Entertainment'],
100 'channel': 'TraciJHines',
101 'channel_url': 'https://www.youtube.com/channel/UCGLim4T2loE5rwCMdpCIPVg',
102 'live_status': 'not_live',
104 'channel_id': 'UCGLim4T2loE5rwCMdpCIPVg',
105 'availability': 'public',
106 'channel_follower_count': int,
107 'playable_in_embed': True,
108 'uploader_url': 'https://www.youtube.com/@TraciHinesMusic',
109 'comment_count': int,
110 'channel_is_verified': True,
111 'chapters': 'count:4',
115 'skip_download': True,
118 'url': 'https://www.patreon.com/posts/episode-166-of-743933',
119 'only_matching': True,
121 'url': 'https://www.patreon.com/posts/743933',
122 'only_matching': True,
124 'url': 'https://www.patreon.com/posts/kitchen-as-seen-51706779',
125 'md5': '96656690071f6d64895866008484251b',
129 'title': 'KITCHEN AS SEEN ON DEEZ NUTS EXTENDED!',
130 'uploader': 'Cold Ones',
131 'thumbnail': 're:^https?://.*$',
132 'upload_date': '20210526',
133 'description': 'md5:557a409bd79d3898689419094934ba79',
134 'uploader_id': '14936315',
136 'skip': 'Patron-only content'
138 # m3u8 video (https://github.com/yt-dlp/yt-dlp/issues/2277)
139 'url': 'https://www.patreon.com/posts/video-sketchbook-32452882',
143 'comment_count': int,
144 'uploader_id': '4301314',
146 'timestamp': 1576696962,
147 'upload_date': '20191218',
148 'thumbnail': r
're:^https?://.*$',
149 'uploader_url': 'https://www.patreon.com/loish',
150 'description': 'md5:e2693e97ee299c8ece47ffdb67e7d9d2',
151 'title': 'VIDEO // sketchbook flipthrough',
152 'uploader': 'Loish ',
153 'tags': ['sketchbook', 'video'],
154 'channel_id': '1641751',
155 'channel_url': 'https://www.patreon.com/loish',
156 'channel_follower_count': int,
159 # bad videos under media (if media is included). Real one is under post_file
160 'url': 'https://www.patreon.com/posts/premium-access-70282931',
164 'title': '[Premium Access + Uncut] The Office - 2x6 The Fight - Group Reaction',
165 'channel_url': 'https://www.patreon.com/thenormies',
166 'channel_id': '573397',
167 'uploader_id': '2929435',
168 'uploader': 'The Normies',
169 'description': 'md5:79c9fd8778e2cef84049a94c058a5e23',
170 'comment_count': int,
171 'upload_date': '20220809',
172 'thumbnail': r
're:^https?://.*$',
173 'channel_follower_count': int,
175 'timestamp': 1660052820,
176 'tags': ['The Office', 'early access', 'uncut'],
177 'uploader_url': 'https://www.patreon.com/thenormies',
179 'skip': 'Patron-only content',
181 # dead vimeo and embed URLs, need to extract post_file
182 'url': 'https://www.patreon.com/posts/hunter-x-hunter-34007913',
186 'title': 'Hunter x Hunter | Kurapika DESTROYS Uvogin!!!',
188 'uploader': 'YaBoyRoshi',
189 'timestamp': 1581636833,
190 'channel_url': 'https://www.patreon.com/yaboyroshi',
191 'thumbnail': r
're:^https?://.*$',
192 'tags': ['Hunter x Hunter'],
193 'uploader_id': '14264111',
194 'comment_count': int,
195 'channel_follower_count': int,
196 'description': 'Kurapika is a walking cheat code!',
197 'upload_date': '20200213',
198 'channel_id': '2147162',
199 'uploader_url': 'https://www.patreon.com/yaboyroshi',
202 # NSFW vimeo embed URL
203 'url': 'https://www.patreon.com/posts/4k-spiderman-4k-96414599',
207 'title': '❤️(4K) Spiderman Girl Yeonhwa’s Gift ❤️(4K) 스파이더맨걸 연화의 선물',
208 'description': '❤️(4K) Spiderman Girl Yeonhwa’s Gift \n❤️(4K) 스파이더맨걸 연화의 선물',
209 'uploader': 'Npickyeonhwa',
210 'uploader_id': '90574422',
211 'uploader_url': 'https://www.patreon.com/Yeonhwa726',
212 'channel_id': '10237902',
213 'channel_url': 'https://www.patreon.com/Yeonhwa726',
215 'timestamp': 1705150153,
216 'upload_date': '20240113',
217 'comment_count': int,
219 'thumbnail': r
're:^https?://.+',
221 'params': {'skip_download': 'm3u8'}
,
223 # multiple attachments/embeds
224 'url': 'https://www.patreon.com/posts/holy-wars-solos-100601977',
228 'title': '"Holy Wars" (Megadeth) Solos Transcription & Lesson/Analysis',
229 'description': 'md5:d099ab976edfce6de2a65c2b169a88d3',
230 'uploader': 'Bradley Hall',
231 'uploader_id': '24401883',
232 'uploader_url': 'https://www.patreon.com/bradleyhallguitar',
233 'channel_id': '3193932',
234 'channel_url': 'https://www.patreon.com/bradleyhallguitar',
235 'channel_follower_count': int,
236 'timestamp': 1710777855,
237 'upload_date': '20240318',
239 'comment_count': int,
240 'thumbnail': r
're:^https?://.+',
242 'skip': 'Patron-only content',
244 _RETURN_TYPE
= 'video'
246 def _real_extract(self
, url
):
247 video_id
= self
._match
_id
(url
)
248 post
= self
._call
_api
(
249 f
'posts/{video_id}', video_id
, query
={
250 'fields[media]': 'download_url,mimetype,size_bytes',
251 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title,current_user_can_view',
252 'fields[user]': 'full_name,url',
253 'fields[post_tag]': 'value',
254 'fields[campaign]': 'url,name,patron_count',
255 'json-api-use-default-includes': 'false',
256 'include': 'audio,user,user_defined_tags,campaign,attachments_media',
258 attributes
= post
['data']['attributes']
259 info
= traverse_obj(attributes
, {
260 'title': ('title', {str.strip}
),
261 'description': ('content', {clean_html}
),
262 'thumbnail': ('image', ('large_url', 'url'), {url_or_none}
, any
),
263 'timestamp': ('published_at', {parse_iso8601}
),
264 'like_count': ('like_count', {int_or_none}
),
265 'comment_count': ('comment_count', {int_or_none}
),
270 for include
in traverse_obj(post
, ('included', lambda _
, v
: v
['type'])):
271 include_type
= include
['type']
272 if include_type
== 'media':
273 media_attributes
= traverse_obj(include
, ('attributes', {dict}
)) or {}
274 download_url
= url_or_none(media_attributes
.get('download_url'))
275 ext
= mimetype2ext(media_attributes
.get('mimetype'))
277 # if size_bytes is None, this media file is likely unavailable
278 # See: https://github.com/yt-dlp/yt-dlp/issues/4608
279 size_bytes
= int_or_none(media_attributes
.get('size_bytes'))
280 if download_url
and ext
in KNOWN_EXTENSIONS
and size_bytes
is not None:
283 'id': f
'{video_id}-{idx}',
285 'filesize': size_bytes
,
289 elif include_type
== 'user':
290 info
.update(traverse_obj(include
, {
291 'uploader': ('attributes', 'full_name', {str}
),
292 'uploader_id': ('id', {str_or_none}
),
293 'uploader_url': ('attributes', 'url', {url_or_none}
),
296 elif include_type
== 'post_tag':
297 if post_tag
:= traverse_obj(include
, ('attributes', 'value', {str}
)):
298 info
.setdefault('tags', []).append(post_tag
)
300 elif include_type
== 'campaign':
301 info
.update(traverse_obj(include
, {
302 'channel': ('attributes', 'title', {str}
),
303 'channel_id': ('id', {str_or_none}
),
304 'channel_url': ('attributes', 'url', {url_or_none}
),
305 'channel_follower_count': ('attributes', 'patron_count', {int_or_none}
),
308 # handle Vimeo embeds
309 if traverse_obj(attributes
, ('embed', 'provider')) == 'Vimeo':
310 v_url
= urllib
.parse
.unquote(self
._html
_search
_regex
(
311 r
'(https(?:%3A%2F%2F|://)player\.vimeo\.com.+app_id(?:=|%3D)+\d+)',
312 traverse_obj(attributes
, ('embed', 'html', {str}
)), 'vimeo url', fatal
=False) or '')
313 if url_or_none(v_url
) and self
._request
_webpage
(
314 v_url
, video_id
, 'Checking Vimeo embed URL',
315 headers
={'Referer': 'https://patreon.com/'}
,
316 fatal
=False, errnote
=False):
317 entries
.append(self
.url_result(
318 VimeoIE
._smuggle
_referrer
(v_url
, 'https://patreon.com/'),
319 VimeoIE
, url_transparent
=True))
321 embed_url
= traverse_obj(attributes
, ('embed', 'url', {url_or_none}
))
322 if embed_url
and self
._request
_webpage
(embed_url
, video_id
, 'Checking embed URL', fatal
=False, errnote
=False):
323 entries
.append(self
.url_result(embed_url
))
325 post_file
= traverse_obj(attributes
, ('post_file', {dict}
))
327 name
= post_file
.get('name')
328 ext
= determine_ext(name
)
329 if ext
in KNOWN_EXTENSIONS
:
333 'url': post_file
['url'],
335 elif name
== 'video' or determine_ext(post_file
.get('url')) == 'm3u8':
336 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(post_file
['url'], video_id
)
340 'subtitles': subtitles
,
343 can_view_post
= traverse_obj(attributes
, 'current_user_can_view')
345 if can_view_post
and info
.get('comment_count'):
346 comments
= self
.extract_comments(video_id
)
348 if not entries
and can_view_post
is False:
349 self
.raise_no_formats('You do not have access to this post', video_id
=video_id
, expected
=True)
351 self
.raise_no_formats('No supported media found in this post', video_id
=video_id
, expected
=True)
352 elif len(entries
) == 1:
353 info
.update(entries
[0])
355 for entry
in entries
:
357 return self
.playlist_result(entries
, video_id
, **info
, __post_extractor
=comments
)
359 info
['id'] = video_id
360 info
['__post_extractor'] = comments
363 def _get_comments(self
, post_id
):
368 'include': 'parent.commenter.campaign,parent.post.user,parent.post.campaign.creator,parent.replies.parent,parent.replies.commenter.campaign,parent.replies.post.user,parent.replies.post.campaign.creator,commenter.campaign,post.user,post.campaign.creator,replies.parent,replies.commenter.campaign,replies.post.user,replies.post.campaign.creator,on_behalf_of_campaign',
369 'fields[comment]': 'body,created,is_by_creator',
370 'fields[user]': 'image_url,full_name,url',
371 'filter[flair]': 'image_tiny_url,name',
373 'json-api-version': 1.0,
374 'json-api-use-default-includes': 'false',
377 for page
in itertools
.count(1):
379 params
.update({'page[cursor]': cursor}
if cursor
else {})
380 response
= self
._call
_api
(
381 f
'posts/{post_id}/comments', post_id
, query
=params
, note
='Downloading comments page %d' % page
)
384 for comment
in traverse_obj(response
, (('data', ('included', lambda _
, v
: v
['type'] == 'comment')), ...)):
386 comment_id
= comment
.get('id')
387 attributes
= comment
.get('attributes') or {}
388 if comment_id
is None:
390 author_id
= traverse_obj(comment
, ('relationships', 'commenter', 'data', 'id'))
391 author_info
= traverse_obj(
392 response
, ('included', lambda _
, v
: v
['id'] == author_id
and v
['type'] == 'user', 'attributes'),
393 get_all
=False, expected_type
=dict, default
={})
397 'text': attributes
.get('body'),
398 'timestamp': parse_iso8601(attributes
.get('created')),
399 'parent': traverse_obj(comment
, ('relationships', 'parent', 'data', 'id'), default
='root'),
400 'author_is_uploader': attributes
.get('is_by_creator'),
401 'author_id': author_id
,
402 'author': author_info
.get('full_name'),
403 'author_thumbnail': author_info
.get('image_url'),
406 if count
< traverse_obj(response
, ('meta', 'count')):
407 cursor
= traverse_obj(response
, ('data', -1, 'id'))
413 class PatreonCampaignIE(PatreonBaseIE
):
415 _VALID_URL
= r
'https?://(?:www\.)?patreon\.com/(?!rss)(?:(?:m/(?P<campaign_id>\d+))|(?P<vanity>[-\w]+))'
417 'url': 'https://www.patreon.com/dissonancepod/',
419 'title': 'Cognitive Dissonance Podcast',
420 'channel_url': 'https://www.patreon.com/dissonancepod',
422 'description': 'md5:eb2fa8b83da7ab887adeac34da6b7af7',
423 'channel_id': '80642',
424 'channel': 'Cognitive Dissonance Podcast',
426 'channel_follower_count': int,
427 'uploader_id': '87145',
428 'uploader_url': 'https://www.patreon.com/dissonancepod',
429 'uploader': 'Cognitive Dissonance Podcast',
430 'thumbnail': r
're:^https?://.*$',
432 'playlist_mincount': 68,
434 'url': 'https://www.patreon.com/m/4767637/posts',
436 'title': 'Not Just Bikes',
437 'channel_follower_count': int,
439 'channel_id': '4767637',
440 'channel_url': 'https://www.patreon.com/notjustbikes',
441 'description': 'md5:595c6e7dca76ae615b1d38c298a287a1',
443 'channel': 'Not Just Bikes',
444 'uploader_url': 'https://www.patreon.com/notjustbikes',
445 'uploader': 'Not Just Bikes',
446 'uploader_id': '37306634',
447 'thumbnail': r
're:^https?://.*$',
449 'playlist_mincount': 71
451 'url': 'https://www.patreon.com/dissonancepod/posts',
452 'only_matching': True
454 'url': 'https://www.patreon.com/m/5932659',
455 'only_matching': True
459 def suitable(cls
, url
):
460 return False if PatreonIE
.suitable(url
) else super(PatreonCampaignIE
, cls
).suitable(url
)
462 def _entries(self
, campaign_id
):
465 'fields[post]': 'patreon_url,url',
466 'filter[campaign_id]': campaign_id
,
467 'filter[is_draft]': 'false',
468 'sort': '-published_at',
469 'json-api-use-default-includes': 'false',
472 for page
in itertools
.count(1):
474 params
.update({'page[cursor]': cursor}
if cursor
else {})
475 posts_json
= self
._call
_api
('posts', campaign_id
, query
=params
, note
='Downloading posts page %d' % page
)
477 cursor
= traverse_obj(posts_json
, ('meta', 'pagination', 'cursors', 'next'))
478 for post_url
in traverse_obj(posts_json
, ('data', ..., 'attributes', 'patreon_url')):
479 yield self
.url_result(urljoin('https://www.patreon.com/', post_url
), PatreonIE
)
484 def _real_extract(self
, url
):
486 campaign_id
, vanity
= self
._match
_valid
_url
(url
).group('campaign_id', 'vanity')
487 if campaign_id
is None:
488 webpage
= self
._download
_webpage
(url
, vanity
, headers
={'User-Agent': self.USER_AGENT}
)
489 campaign_id
= self
._search
_regex
(r
'https://www.patreon.com/api/campaigns/(\d+)/?', webpage
, 'Campaign ID')
492 'json-api-use-default-includes': 'false',
493 'fields[user]': 'full_name,url',
494 'fields[campaign]': 'name,summary,url,patron_count,creation_count,is_nsfw,avatar_photo_url',
498 campaign_response
= self
._call
_api
(
499 f
'campaigns/{campaign_id}', campaign_id
,
500 note
='Downloading campaign info', fatal
=False,
503 campaign_info
= campaign_response
.get('data') or {}
504 channel_name
= traverse_obj(campaign_info
, ('attributes', 'name'))
505 user_info
= traverse_obj(
506 campaign_response
, ('included', lambda _
, v
: v
['type'] == 'user'),
507 default
={}, expected_type
=dict, get_all
=False)
512 'title': channel_name
,
513 'entries': self
._entries
(campaign_id
),
514 'description': clean_html(traverse_obj(campaign_info
, ('attributes', 'summary'))),
515 'channel_url': traverse_obj(campaign_info
, ('attributes', 'url')),
516 'channel_follower_count': int_or_none(traverse_obj(campaign_info
, ('attributes', 'patron_count'))),
517 'channel_id': campaign_id
,
518 'channel': channel_name
,
519 'uploader_url': traverse_obj(user_info
, ('attributes', 'url')),
520 'uploader_id': str_or_none(user_info
.get('id')),
521 'uploader': traverse_obj(user_info
, ('attributes', 'full_name')),
522 'playlist_count': traverse_obj(campaign_info
, ('attributes', 'creation_count')),
523 'age_limit': 18 if traverse_obj(campaign_info
, ('attributes', 'is_nsfw')) else 0,
524 'thumbnail': url_or_none(traverse_obj(campaign_info
, ('attributes', 'avatar_photo_url'))),