]>
Commit | Line | Data |
---|---|---|
1dd6d9ca | 1 | import itertools |
c9ce57d9 | 2 | import urllib.parse |
1dd6d9ca | 3 | |
a00d73c8 | 4 | from .common import InfoExtractor |
65af1839 | 5 | from .vimeo import VimeoIE |
3d2623a8 | 6 | from ..networking.exceptions import HTTPError |
c9d891f1 | 7 | from ..utils import ( |
11de6fec | 8 | KNOWN_EXTENSIONS, |
9 | ExtractorError, | |
c9d891f1 RA |
10 | clean_html, |
11 | determine_ext, | |
12 | int_or_none, | |
b77c3949 | 13 | mimetype2ext, |
c9d891f1 | 14 | parse_iso8601, |
b77c3949 | 15 | str_or_none, |
4f08e586 | 16 | traverse_obj, |
1dd6d9ca | 17 | url_or_none, |
11de6fec | 18 | urljoin, |
c9d891f1 | 19 | ) |
a00d73c8 EJ |
20 | |
21 | ||
4f08e586 | 22 | class PatreonBaseIE(InfoExtractor): |
23 | USER_AGENT = 'Patreon/7.6.28 (Android; Android 11; Scale/2.10)' | |
24 | ||
25 | def _call_api(self, ep, item_id, query=None, headers=None, fatal=True, note=None): | |
26 | if headers is None: | |
27 | headers = {} | |
28 | if 'User-Agent' not in headers: | |
29 | headers['User-Agent'] = self.USER_AGENT | |
30 | if query: | |
31 | query.update({'json-api-version': 1.0}) | |
32 | ||
33 | try: | |
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: | |
3d2623a8 | 39 | if not isinstance(e.cause, HTTPError) or mimetype2ext(e.cause.response.headers.get('Content-Type')) != 'json': |
4f08e586 | 40 | raise |
3d2623a8 | 41 | err_json = self._parse_json(self._webpage_read_content(e.cause.response, None, item_id), item_id, fatal=False) |
4f08e586 | 42 | err_message = traverse_obj(err_json, ('errors', ..., 'detail'), get_all=False) |
43 | if err_message: | |
44 | raise ExtractorError(f'Patreon said: {err_message}', expected=True) | |
45 | raise | |
46 | ||
47 | ||
48 | class PatreonIE(PatreonBaseIE): | |
c9d891f1 RA |
49 | _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)' |
50 | _TESTS = [{ | |
51 | 'url': 'http://www.patreon.com/creation?hid=743933', | |
52 | 'md5': 'e25505eec1053a6e6813b8ed369875cc', | |
53 | 'info_dict': { | |
54 | 'id': '743933', | |
55 | 'ext': 'mp3', | |
56 | 'title': 'Episode 166: David Smalley of Dogma Debate', | |
4f08e586 | 57 | 'description': 'md5:34d207dd29aa90e24f1b3f58841b81c7', |
c9d891f1 RA |
58 | 'uploader': 'Cognitive Dissonance Podcast', |
59 | 'thumbnail': 're:^https?://.*$', | |
60 | 'timestamp': 1406473987, | |
61 | 'upload_date': '20140727', | |
b77c3949 | 62 | 'uploader_id': '87145', |
4f08e586 | 63 | 'like_count': int, |
64 | 'comment_count': int, | |
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, | |
c9d891f1 RA |
69 | }, |
70 | }, { | |
71 | 'url': 'http://www.patreon.com/creation?hid=754133', | |
72 | 'md5': '3eb09345bf44bf60451b8b0b81759d0a', | |
73 | 'info_dict': { | |
74 | 'id': '754133', | |
75 | 'ext': 'mp3', | |
76 | 'title': 'CD 167 Extra', | |
77 | 'uploader': 'Cognitive Dissonance Podcast', | |
78 | 'thumbnail': 're:^https?://.*$', | |
4f08e586 | 79 | 'like_count': int, |
80 | 'comment_count': int, | |
81 | 'uploader_url': 'https://www.patreon.com/dissonancepod', | |
a00d73c8 | 82 | }, |
c9d891f1 RA |
83 | 'skip': 'Patron-only content', |
84 | }, { | |
85 | 'url': 'https://www.patreon.com/creation?hid=1682498', | |
86 | 'info_dict': { | |
87 | 'id': 'SU4fj_aEMVw', | |
88 | 'ext': 'mp4', | |
89 | 'title': 'I\'m on Patreon!', | |
90 | 'uploader': 'TraciJHines', | |
91 | 'thumbnail': 're:^https?://.*$', | |
92 | 'upload_date': '20150211', | |
4f08e586 | 93 | 'description': 'md5:8af6425f50bd46fbf29f3db0fc3a8364', |
36b240f9 | 94 | 'uploader_id': '@TraciHinesMusic', |
4f08e586 | 95 | 'categories': ['Entertainment'], |
96 | 'duration': 282, | |
97 | 'view_count': int, | |
98 | 'tags': 'count:39', | |
99 | 'age_limit': 0, | |
100 | 'channel': 'TraciJHines', | |
101 | 'channel_url': 'https://www.youtube.com/channel/UCGLim4T2loE5rwCMdpCIPVg', | |
102 | 'live_status': 'not_live', | |
103 | 'like_count': int, | |
104 | 'channel_id': 'UCGLim4T2loE5rwCMdpCIPVg', | |
105 | 'availability': 'public', | |
106 | 'channel_follower_count': int, | |
107 | 'playable_in_embed': True, | |
36b240f9 | 108 | 'uploader_url': 'https://www.youtube.com/@TraciHinesMusic', |
4f08e586 | 109 | 'comment_count': int, |
36b240f9 JV |
110 | 'channel_is_verified': True, |
111 | 'chapters': 'count:4', | |
6994e706 | 112 | }, |
c9d891f1 RA |
113 | 'params': { |
114 | 'noplaylist': True, | |
115 | 'skip_download': True, | |
6b961a85 | 116 | } |
c9d891f1 RA |
117 | }, { |
118 | 'url': 'https://www.patreon.com/posts/episode-166-of-743933', | |
119 | 'only_matching': True, | |
120 | }, { | |
121 | 'url': 'https://www.patreon.com/posts/743933', | |
122 | 'only_matching': True, | |
65af1839 | 123 | }, { |
124 | 'url': 'https://www.patreon.com/posts/kitchen-as-seen-51706779', | |
125 | 'md5': '96656690071f6d64895866008484251b', | |
126 | 'info_dict': { | |
127 | 'id': '555089736', | |
128 | 'ext': 'mp4', | |
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', | |
135 | }, | |
136 | 'skip': 'Patron-only content' | |
4f08e586 | 137 | }, { |
138 | # m3u8 video (https://github.com/yt-dlp/yt-dlp/issues/2277) | |
139 | 'url': 'https://www.patreon.com/posts/video-sketchbook-32452882', | |
140 | 'info_dict': { | |
141 | 'id': '32452882', | |
142 | 'ext': 'mp4', | |
143 | 'comment_count': int, | |
144 | 'uploader_id': '4301314', | |
145 | 'like_count': int, | |
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, | |
a00d73c8 | 157 | } |
cea4b857 | 158 | }, { |
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', | |
161 | 'info_dict': { | |
162 | 'id': '70282931', | |
163 | 'ext': 'mp4', | |
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, | |
174 | 'like_count': int, | |
175 | 'timestamp': 1660052820, | |
176 | 'tags': ['The Office', 'early access', 'uncut'], | |
177 | 'uploader_url': 'https://www.patreon.com/thenormies', | |
178 | }, | |
179 | 'skip': 'Patron-only content', | |
36b240f9 JV |
180 | }, { |
181 | # dead vimeo and embed URLs, need to extract post_file | |
182 | 'url': 'https://www.patreon.com/posts/hunter-x-hunter-34007913', | |
183 | 'info_dict': { | |
184 | 'id': '34007913', | |
185 | 'ext': 'mp4', | |
186 | 'title': 'Hunter x Hunter | Kurapika DESTROYS Uvogin!!!', | |
187 | 'like_count': int, | |
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', | |
200 | }, | |
c9ce57d9 | 201 | }, { |
202 | # NSFW vimeo embed URL | |
203 | 'url': 'https://www.patreon.com/posts/4k-spiderman-4k-96414599', | |
204 | 'info_dict': { | |
205 | 'id': '902250943', | |
206 | 'ext': 'mp4', | |
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', | |
214 | 'duration': 70, | |
215 | 'timestamp': 1705150153, | |
216 | 'upload_date': '20240113', | |
217 | 'comment_count': int, | |
218 | 'like_count': int, | |
219 | 'thumbnail': r're:^https?://.+', | |
220 | }, | |
221 | 'params': {'skip_download': 'm3u8'}, | |
4f08e586 | 222 | }] |
a00d73c8 EJ |
223 | |
224 | def _real_extract(self, url): | |
77070040 | 225 | video_id = self._match_id(url) |
4f08e586 | 226 | post = self._call_api( |
227 | f'posts/{video_id}', video_id, query={ | |
b77c3949 | 228 | 'fields[media]': 'download_url,mimetype,size_bytes', |
4f08e586 | 229 | 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title,current_user_can_view', |
b77c3949 | 230 | 'fields[user]': 'full_name,url', |
4f08e586 | 231 | 'fields[post_tag]': 'value', |
232 | 'fields[campaign]': 'url,name,patron_count', | |
b77c3949 | 233 | 'json-api-use-default-includes': 'false', |
cea4b857 | 234 | 'include': 'audio,user,user_defined_tags,campaign,attachments_media', |
b77c3949 | 235 | }) |
c9d891f1 RA |
236 | attributes = post['data']['attributes'] |
237 | title = attributes['title'].strip() | |
238 | image = attributes.get('image') or {} | |
239 | info = { | |
e05f6939 | 240 | 'id': video_id, |
e05f6939 | 241 | 'title': title, |
c9d891f1 RA |
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')), | |
e05f6939 | 247 | } |
4f08e586 | 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) | |
c9d891f1 RA |
251 | |
252 | for i in post.get('included', []): | |
253 | i_type = i.get('type') | |
b77c3949 RA |
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')) | |
cea4b857 | 258 | |
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? | |
4f08e586 | 264 | return { |
265 | **info, | |
b77c3949 | 266 | 'ext': ext, |
cea4b857 | 267 | 'filesize': size_bytes, |
b77c3949 | 268 | 'url': download_url, |
4f08e586 | 269 | } |
c9d891f1 RA |
270 | elif i_type == 'user': |
271 | user_attributes = i.get('attributes') | |
272 | if user_attributes: | |
273 | info.update({ | |
274 | 'uploader': user_attributes.get('full_name'), | |
b77c3949 | 275 | 'uploader_id': str_or_none(i.get('id')), |
c9d891f1 RA |
276 | 'uploader_url': user_attributes.get('url'), |
277 | }) | |
278 | ||
4f08e586 | 279 | elif i_type == 'post_tag': |
280 | info.setdefault('tags', []).append(traverse_obj(i, ('attributes', 'value'))) | |
65af1839 | 281 | |
4f08e586 | 282 | elif i_type == 'campaign': |
b77c3949 | 283 | info.update({ |
4f08e586 | 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'))), | |
b77c3949 | 288 | }) |
19a35285 | 289 | |
4f08e586 | 290 | # handle Vimeo embeds |
c9ce57d9 | 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) | |
4f08e586 | 302 | |
c9ce57d9 | 303 | embed_url = traverse_obj(attributes, ('embed', 'url', {url_or_none})) |
36b240f9 JV |
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) | |
4f08e586 | 306 | |
307 | post_file = traverse_obj(attributes, 'post_file') | |
308 | if post_file: | |
309 | name = post_file.get('name') | |
310 | ext = determine_ext(name) | |
b77c3949 | 311 | if ext in KNOWN_EXTENSIONS: |
4f08e586 | 312 | return { |
313 | **info, | |
b77c3949 RA |
314 | 'ext': ext, |
315 | 'url': post_file['url'], | |
4f08e586 | 316 | } |
f0e8bc7c | 317 | elif name == 'video' or determine_ext(post_file.get('url')) == 'm3u8': |
4f08e586 | 318 | formats, subtitles = self._extract_m3u8_formats_and_subtitles(post_file['url'], video_id) |
319 | return { | |
320 | **info, | |
321 | 'formats': formats, | |
322 | 'subtitles': subtitles, | |
323 | } | |
c9d891f1 | 324 | |
4f08e586 | 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) | |
327 | else: | |
328 | self.raise_no_formats('No supported media found in this post', video_id=video_id, expected=True) | |
c9d891f1 | 329 | return info |
1dd6d9ca | 330 | |
4f08e586 | 331 | def _get_comments(self, post_id): |
332 | cursor = None | |
333 | count = 0 | |
334 | params = { | |
335 | 'page[count]': 50, | |
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', | |
340 | 'sort': '-created', | |
341 | 'json-api-version': 1.0, | |
342 | 'json-api-use-default-includes': 'false', | |
343 | } | |
344 | ||
345 | for page in itertools.count(1): | |
346 | ||
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) | |
350 | ||
351 | cursor = None | |
6839ae1f | 352 | for comment in traverse_obj(response, (('data', ('included', lambda _, v: v['type'] == 'comment')), ...)): |
4f08e586 | 353 | count += 1 |
354 | comment_id = comment.get('id') | |
355 | attributes = comment.get('attributes') or {} | |
356 | if comment_id is None: | |
357 | continue | |
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={}) | |
362 | ||
363 | yield { | |
364 | 'id': comment_id, | |
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'), | |
372 | } | |
373 | ||
374 | if count < traverse_obj(response, ('meta', 'count')): | |
375 | cursor = traverse_obj(response, ('data', -1, 'id')) | |
376 | ||
377 | if cursor is None: | |
378 | break | |
1dd6d9ca | 379 | |
1dd6d9ca | 380 | |
4f08e586 | 381 | class PatreonCampaignIE(PatreonBaseIE): |
1dd6d9ca | 382 | |
4f08e586 | 383 | _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?!rss)(?:(?:m/(?P<campaign_id>\d+))|(?P<vanity>[-\w]+))' |
1dd6d9ca | 384 | _TESTS = [{ |
385 | 'url': 'https://www.patreon.com/dissonancepod/', | |
386 | 'info_dict': { | |
4f08e586 | 387 | 'title': 'Cognitive Dissonance Podcast', |
388 | 'channel_url': 'https://www.patreon.com/dissonancepod', | |
389 | 'id': '80642', | |
390 | 'description': 'md5:eb2fa8b83da7ab887adeac34da6b7af7', | |
391 | 'channel_id': '80642', | |
392 | 'channel': 'Cognitive Dissonance Podcast', | |
393 | 'age_limit': 0, | |
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?://.*$', | |
1dd6d9ca | 399 | }, |
400 | 'playlist_mincount': 68, | |
4f08e586 | 401 | }, { |
402 | 'url': 'https://www.patreon.com/m/4767637/posts', | |
403 | 'info_dict': { | |
404 | 'title': 'Not Just Bikes', | |
405 | 'channel_follower_count': int, | |
406 | 'id': '4767637', | |
407 | 'channel_id': '4767637', | |
408 | 'channel_url': 'https://www.patreon.com/notjustbikes', | |
409 | 'description': 'md5:595c6e7dca76ae615b1d38c298a287a1', | |
410 | 'age_limit': 0, | |
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?://.*$', | |
416 | }, | |
417 | 'playlist_mincount': 71 | |
1dd6d9ca | 418 | }, { |
419 | 'url': 'https://www.patreon.com/dissonancepod/posts', | |
420 | 'only_matching': True | |
4f08e586 | 421 | }, { |
422 | 'url': 'https://www.patreon.com/m/5932659', | |
423 | 'only_matching': True | |
424 | }] | |
1dd6d9ca | 425 | |
426 | @classmethod | |
427 | def suitable(cls, url): | |
4f08e586 | 428 | return False if PatreonIE.suitable(url) else super(PatreonCampaignIE, cls).suitable(url) |
1dd6d9ca | 429 | |
4f08e586 | 430 | def _entries(self, campaign_id): |
1dd6d9ca | 431 | cursor = None |
432 | params = { | |
4f08e586 | 433 | 'fields[post]': 'patreon_url,url', |
1dd6d9ca | 434 | 'filter[campaign_id]': campaign_id, |
435 | 'filter[is_draft]': 'false', | |
436 | 'sort': '-published_at', | |
1dd6d9ca | 437 | 'json-api-use-default-includes': 'false', |
438 | } | |
439 | ||
440 | for page in itertools.count(1): | |
441 | ||
442 | params.update({'page[cursor]': cursor} if cursor else {}) | |
4f08e586 | 443 | posts_json = self._call_api('posts', campaign_id, query=params, note='Downloading posts page %d' % page) |
1dd6d9ca | 444 | |
4f08e586 | 445 | cursor = traverse_obj(posts_json, ('meta', 'pagination', 'cursors', 'next')) |
11de6fec | 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) | |
1dd6d9ca | 448 | |
449 | if cursor is None: | |
450 | break | |
451 | ||
452 | def _real_extract(self, url): | |
453 | ||
4f08e586 | 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') | |
458 | ||
459 | params = { | |
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', | |
463 | 'include': 'creator' | |
464 | } | |
465 | ||
466 | campaign_response = self._call_api( | |
467 | f'campaigns/{campaign_id}', campaign_id, | |
468 | note='Downloading campaign info', fatal=False, | |
469 | query=params) or {} | |
470 | ||
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) | |
476 | ||
477 | return { | |
478 | '_type': 'playlist', | |
479 | 'id': campaign_id, | |
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'))), | |
493 | } |