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'}
,
224 def _real_extract(self
, url
):
225 video_id
= self
._match
_id
(url
)
226 post
= self
._call
_api
(
227 f
'posts/{video_id}', video_id
, query
={
228 'fields[media]': 'download_url,mimetype,size_bytes',
229 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title,current_user_can_view',
230 'fields[user]': 'full_name,url',
231 'fields[post_tag]': 'value',
232 'fields[campaign]': 'url,name,patron_count',
233 'json-api-use-default-includes': 'false',
234 'include': 'audio,user,user_defined_tags,campaign,attachments_media',
236 attributes
= post
['data']['attributes']
237 title
= attributes
['title'].strip()
238 image
= attributes
.get('image') or {}
242 'description': clean_html(attributes
.get('content')),
243 'thumbnail': image
.get('large_url') or image
.get('url'),
244 'timestamp': parse_iso8601(attributes
.get('published_at')),
245 'like_count': int_or_none(attributes
.get('like_count')),
246 'comment_count': int_or_none(attributes
.get('comment_count')),
248 can_view_post
= traverse_obj(attributes
, 'current_user_can_view')
249 if can_view_post
and info
['comment_count']:
250 info
['__post_extractor'] = self
.extract_comments(video_id
)
252 for i
in post
.get('included', []):
253 i_type
= i
.get('type')
254 if i_type
== 'media':
255 media_attributes
= i
.get('attributes') or {}
256 download_url
= media_attributes
.get('download_url')
257 ext
= mimetype2ext(media_attributes
.get('mimetype'))
259 # if size_bytes is None, this media file is likely unavailable
260 # See: https://github.com/yt-dlp/yt-dlp/issues/4608
261 size_bytes
= int_or_none(media_attributes
.get('size_bytes'))
262 if download_url
and ext
in KNOWN_EXTENSIONS
and size_bytes
is not None:
263 # XXX: what happens if there are multiple attachments?
267 'filesize': size_bytes
,
270 elif i_type
== 'user':
271 user_attributes
= i
.get('attributes')
274 'uploader': user_attributes
.get('full_name'),
275 'uploader_id': str_or_none(i
.get('id')),
276 'uploader_url': user_attributes
.get('url'),
279 elif i_type
== 'post_tag':
280 info
.setdefault('tags', []).append(traverse_obj(i
, ('attributes', 'value')))
282 elif i_type
== 'campaign':
284 'channel': traverse_obj(i
, ('attributes', 'title')),
285 'channel_id': str_or_none(i
.get('id')),
286 'channel_url': traverse_obj(i
, ('attributes', 'url')),
287 'channel_follower_count': int_or_none(traverse_obj(i
, ('attributes', 'patron_count'))),
290 # handle Vimeo embeds
291 if traverse_obj(attributes
, ('embed', 'provider')) == 'Vimeo':
292 v_url
= urllib
.parse
.unquote(self
._html
_search
_regex
(
293 r
'(https(?:%3A%2F%2F|://)player\.vimeo\.com.+app_id(?:=|%3D)+\d+)',
294 traverse_obj(attributes
, ('embed', 'html', {str}
)), 'vimeo url', fatal
=False) or '')
295 if url_or_none(v_url
) and self
._request
_webpage
(
296 v_url
, video_id
, 'Checking Vimeo embed URL',
297 headers
={'Referer': 'https://patreon.com/'}
,
298 fatal
=False, errnote
=False):
299 return self
.url_result(
300 VimeoIE
._smuggle
_referrer
(v_url
, 'https://patreon.com/'),
301 VimeoIE
, url_transparent
=True, **info
)
303 embed_url
= traverse_obj(attributes
, ('embed', 'url', {url_or_none}
))
304 if embed_url
and self
._request
_webpage
(embed_url
, video_id
, 'Checking embed URL', fatal
=False, errnote
=False):
305 return self
.url_result(embed_url
, **info
)
307 post_file
= traverse_obj(attributes
, 'post_file')
309 name
= post_file
.get('name')
310 ext
= determine_ext(name
)
311 if ext
in KNOWN_EXTENSIONS
:
315 'url': post_file
['url'],
317 elif name
== 'video' or determine_ext(post_file
.get('url')) == 'm3u8':
318 formats
, subtitles
= self
._extract
_m
3u8_formats
_and
_subtitles
(post_file
['url'], video_id
)
322 'subtitles': subtitles
,
325 if can_view_post
is False:
326 self
.raise_no_formats('You do not have access to this post', video_id
=video_id
, expected
=True)
328 self
.raise_no_formats('No supported media found in this post', video_id
=video_id
, expected
=True)
331 def _get_comments(self
, post_id
):
336 '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',
337 'fields[comment]': 'body,created,is_by_creator',
338 'fields[user]': 'image_url,full_name,url',
339 'filter[flair]': 'image_tiny_url,name',
341 'json-api-version': 1.0,
342 'json-api-use-default-includes': 'false',
345 for page
in itertools
.count(1):
347 params
.update({'page[cursor]': cursor}
if cursor
else {})
348 response
= self
._call
_api
(
349 f
'posts/{post_id}/comments', post_id
, query
=params
, note
='Downloading comments page %d' % page
)
352 for comment
in traverse_obj(response
, (('data', ('included', lambda _
, v
: v
['type'] == 'comment')), ...)):
354 comment_id
= comment
.get('id')
355 attributes
= comment
.get('attributes') or {}
356 if comment_id
is None:
358 author_id
= traverse_obj(comment
, ('relationships', 'commenter', 'data', 'id'))
359 author_info
= traverse_obj(
360 response
, ('included', lambda _
, v
: v
['id'] == author_id
and v
['type'] == 'user', 'attributes'),
361 get_all
=False, expected_type
=dict, default
={})
365 'text': attributes
.get('body'),
366 'timestamp': parse_iso8601(attributes
.get('created')),
367 'parent': traverse_obj(comment
, ('relationships', 'parent', 'data', 'id'), default
='root'),
368 'author_is_uploader': attributes
.get('is_by_creator'),
369 'author_id': author_id
,
370 'author': author_info
.get('full_name'),
371 'author_thumbnail': author_info
.get('image_url'),
374 if count
< traverse_obj(response
, ('meta', 'count')):
375 cursor
= traverse_obj(response
, ('data', -1, 'id'))
381 class PatreonCampaignIE(PatreonBaseIE
):
383 _VALID_URL
= r
'https?://(?:www\.)?patreon\.com/(?!rss)(?:(?:m/(?P<campaign_id>\d+))|(?P<vanity>[-\w]+))'
385 'url': 'https://www.patreon.com/dissonancepod/',
387 'title': 'Cognitive Dissonance Podcast',
388 'channel_url': 'https://www.patreon.com/dissonancepod',
390 'description': 'md5:eb2fa8b83da7ab887adeac34da6b7af7',
391 'channel_id': '80642',
392 'channel': 'Cognitive Dissonance Podcast',
394 'channel_follower_count': int,
395 'uploader_id': '87145',
396 'uploader_url': 'https://www.patreon.com/dissonancepod',
397 'uploader': 'Cognitive Dissonance Podcast',
398 'thumbnail': r
're:^https?://.*$',
400 'playlist_mincount': 68,
402 'url': 'https://www.patreon.com/m/4767637/posts',
404 'title': 'Not Just Bikes',
405 'channel_follower_count': int,
407 'channel_id': '4767637',
408 'channel_url': 'https://www.patreon.com/notjustbikes',
409 'description': 'md5:595c6e7dca76ae615b1d38c298a287a1',
411 'channel': 'Not Just Bikes',
412 'uploader_url': 'https://www.patreon.com/notjustbikes',
413 'uploader': 'Not Just Bikes',
414 'uploader_id': '37306634',
415 'thumbnail': r
're:^https?://.*$',
417 'playlist_mincount': 71
419 'url': 'https://www.patreon.com/dissonancepod/posts',
420 'only_matching': True
422 'url': 'https://www.patreon.com/m/5932659',
423 'only_matching': True
427 def suitable(cls
, url
):
428 return False if PatreonIE
.suitable(url
) else super(PatreonCampaignIE
, cls
).suitable(url
)
430 def _entries(self
, campaign_id
):
433 'fields[post]': 'patreon_url,url',
434 'filter[campaign_id]': campaign_id
,
435 'filter[is_draft]': 'false',
436 'sort': '-published_at',
437 'json-api-use-default-includes': 'false',
440 for page
in itertools
.count(1):
442 params
.update({'page[cursor]': cursor}
if cursor
else {})
443 posts_json
= self
._call
_api
('posts', campaign_id
, query
=params
, note
='Downloading posts page %d' % page
)
445 cursor
= traverse_obj(posts_json
, ('meta', 'pagination', 'cursors', 'next'))
446 for post_url
in traverse_obj(posts_json
, ('data', ..., 'attributes', 'patreon_url')):
447 yield self
.url_result(urljoin('https://www.patreon.com/', post_url
), PatreonIE
)
452 def _real_extract(self
, url
):
454 campaign_id
, vanity
= self
._match
_valid
_url
(url
).group('campaign_id', 'vanity')
455 if campaign_id
is None:
456 webpage
= self
._download
_webpage
(url
, vanity
, headers
={'User-Agent': self.USER_AGENT}
)
457 campaign_id
= self
._search
_regex
(r
'https://www.patreon.com/api/campaigns/(\d+)/?', webpage
, 'Campaign ID')
460 'json-api-use-default-includes': 'false',
461 'fields[user]': 'full_name,url',
462 'fields[campaign]': 'name,summary,url,patron_count,creation_count,is_nsfw,avatar_photo_url',
466 campaign_response
= self
._call
_api
(
467 f
'campaigns/{campaign_id}', campaign_id
,
468 note
='Downloading campaign info', fatal
=False,
471 campaign_info
= campaign_response
.get('data') or {}
472 channel_name
= traverse_obj(campaign_info
, ('attributes', 'name'))
473 user_info
= traverse_obj(
474 campaign_response
, ('included', lambda _
, v
: v
['type'] == 'user'),
475 default
={}, expected_type
=dict, get_all
=False)
480 'title': channel_name
,
481 'entries': self
._entries
(campaign_id
),
482 'description': clean_html(traverse_obj(campaign_info
, ('attributes', 'summary'))),
483 'channel_url': traverse_obj(campaign_info
, ('attributes', 'url')),
484 'channel_follower_count': int_or_none(traverse_obj(campaign_info
, ('attributes', 'patron_count'))),
485 'channel_id': campaign_id
,
486 'channel': channel_name
,
487 'uploader_url': traverse_obj(user_info
, ('attributes', 'url')),
488 'uploader_id': str_or_none(user_info
.get('id')),
489 'uploader': traverse_obj(user_info
, ('attributes', 'full_name')),
490 'playlist_count': traverse_obj(campaign_info
, ('attributes', 'creation_count')),
491 'age_limit': 18 if traverse_obj(campaign_info
, ('attributes', 'is_nsfw')) else 0,
492 'thumbnail': url_or_none(traverse_obj(campaign_info
, ('attributes', 'avatar_photo_url'))),