]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/patreon.py
[extractor/FranceCulture] Fix extractor (#3874)
[yt-dlp.git] / yt_dlp / extractor / patreon.py
CommitLineData
1dd6d9ca 1import itertools
2
a00d73c8 3from .common import InfoExtractor
65af1839 4from .vimeo import VimeoIE
5
6from ..compat import compat_urllib_parse_unquote
c9d891f1
RA
7from ..utils import (
8 clean_html,
9 determine_ext,
10 int_or_none,
b77c3949
RA
11 KNOWN_EXTENSIONS,
12 mimetype2ext,
c9d891f1 13 parse_iso8601,
b77c3949
RA
14 str_or_none,
15 try_get,
1dd6d9ca 16 url_or_none,
c9d891f1 17)
a00d73c8
EJ
18
19
a00d73c8 20class PatreonIE(InfoExtractor):
c9d891f1
RA
21 _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)'
22 _TESTS = [{
23 'url': 'http://www.patreon.com/creation?hid=743933',
24 'md5': 'e25505eec1053a6e6813b8ed369875cc',
25 'info_dict': {
26 'id': '743933',
27 'ext': 'mp3',
28 'title': 'Episode 166: David Smalley of Dogma Debate',
29 'description': 'md5:713b08b772cd6271b9f3906683cfacdf',
30 'uploader': 'Cognitive Dissonance Podcast',
31 'thumbnail': 're:^https?://.*$',
32 'timestamp': 1406473987,
33 'upload_date': '20140727',
b77c3949 34 'uploader_id': '87145',
c9d891f1
RA
35 },
36 }, {
37 'url': 'http://www.patreon.com/creation?hid=754133',
38 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
39 'info_dict': {
40 'id': '754133',
41 'ext': 'mp3',
42 'title': 'CD 167 Extra',
43 'uploader': 'Cognitive Dissonance Podcast',
44 'thumbnail': 're:^https?://.*$',
a00d73c8 45 },
c9d891f1
RA
46 'skip': 'Patron-only content',
47 }, {
48 'url': 'https://www.patreon.com/creation?hid=1682498',
49 'info_dict': {
50 'id': 'SU4fj_aEMVw',
51 'ext': 'mp4',
52 'title': 'I\'m on Patreon!',
53 'uploader': 'TraciJHines',
54 'thumbnail': 're:^https?://.*$',
55 'upload_date': '20150211',
56 'description': 'md5:c5a706b1f687817a3de09db1eb93acd4',
57 'uploader_id': 'TraciJHines',
6994e706 58 },
c9d891f1
RA
59 'params': {
60 'noplaylist': True,
61 'skip_download': True,
6b961a85 62 }
c9d891f1
RA
63 }, {
64 'url': 'https://www.patreon.com/posts/episode-166-of-743933',
65 'only_matching': True,
66 }, {
67 'url': 'https://www.patreon.com/posts/743933',
68 'only_matching': True,
65af1839 69 }, {
70 'url': 'https://www.patreon.com/posts/kitchen-as-seen-51706779',
71 'md5': '96656690071f6d64895866008484251b',
72 'info_dict': {
73 'id': '555089736',
74 'ext': 'mp4',
75 'title': 'KITCHEN AS SEEN ON DEEZ NUTS EXTENDED!',
76 'uploader': 'Cold Ones',
77 'thumbnail': 're:^https?://.*$',
78 'upload_date': '20210526',
79 'description': 'md5:557a409bd79d3898689419094934ba79',
80 'uploader_id': '14936315',
81 },
82 'skip': 'Patron-only content'
c9d891f1 83 }]
a00d73c8
EJ
84
85 # Currently Patreon exposes download URL via hidden CSS, so login is not
86 # needed. Keeping this commented for when this inevitably changes.
87 '''
52efa4b3 88 def _perform_login(self, username, password):
a00d73c8
EJ
89 login_form = {
90 'redirectUrl': 'http://www.patreon.com/',
91 'email': username,
92 'password': password,
93 }
94
5c2266df 95 request = sanitized_Request(
a00d73c8 96 'https://www.patreon.com/processLogin',
15707c7e 97 compat_urllib_parse_urlencode(login_form).encode('utf-8')
a00d73c8 98 )
e4d95865 99 login_page = self._download_webpage(request, None, note='Logging in')
a00d73c8
EJ
100
101 if re.search(r'onLoginFailed', login_page):
102 raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
103
a00d73c8
EJ
104 '''
105
106 def _real_extract(self, url):
77070040 107 video_id = self._match_id(url)
c9d891f1 108 post = self._download_json(
b77c3949
RA
109 'https://www.patreon.com/api/posts/' + video_id, video_id, query={
110 'fields[media]': 'download_url,mimetype,size_bytes',
111 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title',
112 'fields[user]': 'full_name,url',
113 'json-api-use-default-includes': 'false',
114 'include': 'media,user',
115 })
c9d891f1
RA
116 attributes = post['data']['attributes']
117 title = attributes['title'].strip()
118 image = attributes.get('image') or {}
119 info = {
e05f6939 120 'id': video_id,
e05f6939 121 'title': title,
c9d891f1
RA
122 'description': clean_html(attributes.get('content')),
123 'thumbnail': image.get('large_url') or image.get('url'),
124 'timestamp': parse_iso8601(attributes.get('published_at')),
125 'like_count': int_or_none(attributes.get('like_count')),
126 'comment_count': int_or_none(attributes.get('comment_count')),
e05f6939 127 }
c9d891f1
RA
128
129 for i in post.get('included', []):
130 i_type = i.get('type')
b77c3949
RA
131 if i_type == 'media':
132 media_attributes = i.get('attributes') or {}
133 download_url = media_attributes.get('download_url')
134 ext = mimetype2ext(media_attributes.get('mimetype'))
135 if download_url and ext in KNOWN_EXTENSIONS:
136 info.update({
137 'ext': ext,
138 'filesize': int_or_none(media_attributes.get('size_bytes')),
139 'url': download_url,
140 })
c9d891f1
RA
141 elif i_type == 'user':
142 user_attributes = i.get('attributes')
143 if user_attributes:
144 info.update({
145 'uploader': user_attributes.get('full_name'),
b77c3949 146 'uploader_id': str_or_none(i.get('id')),
c9d891f1
RA
147 'uploader_url': user_attributes.get('url'),
148 })
149
65af1839 150 if not info.get('url'):
151 # handle Vimeo embeds
152 if try_get(attributes, lambda x: x['embed']['provider']) == 'Vimeo':
153 embed_html = try_get(attributes, lambda x: x['embed']['html'])
154 v_url = url_or_none(compat_urllib_parse_unquote(
f656a23c 155 self._search_regex(r'(https(?:%3A%2F%2F|://)player\.vimeo\.com.+app_id(?:=|%3D)+\d+)', embed_html, 'vimeo url', fatal=False)))
65af1839 156 if v_url:
157 info.update({
158 '_type': 'url_transparent',
159 'url': VimeoIE._smuggle_referrer(v_url, 'https://patreon.com'),
160 'ie_key': 'Vimeo',
161 })
162
19a35285 163 if not info.get('url'):
b77c3949
RA
164 embed_url = try_get(attributes, lambda x: x['embed']['url'])
165 if embed_url:
166 info.update({
167 '_type': 'url',
168 'url': embed_url,
169 })
19a35285 170
c9d891f1 171 if not info.get('url'):
b77c3949
RA
172 post_file = attributes['post_file']
173 ext = determine_ext(post_file.get('name'))
174 if ext in KNOWN_EXTENSIONS:
175 info.update({
176 'ext': ext,
177 'url': post_file['url'],
178 })
c9d891f1
RA
179
180 return info
1dd6d9ca 181
182
183class PatreonUserIE(InfoExtractor):
184
9941a1e1 185 _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?!rss)(?P<id>[-\w]+)'
1dd6d9ca 186
187 _TESTS = [{
188 'url': 'https://www.patreon.com/dissonancepod/',
189 'info_dict': {
190 'title': 'dissonancepod',
191 },
192 'playlist_mincount': 68,
193 'expected_warnings': 'Post not viewable by current user! Skipping!',
194 }, {
195 'url': 'https://www.patreon.com/dissonancepod/posts',
196 'only_matching': True
197 }, ]
198
199 @classmethod
200 def suitable(cls, url):
201 return False if PatreonIE.suitable(url) else super(PatreonUserIE, cls).suitable(url)
202
203 def _entries(self, campaign_id, user_id):
204 cursor = None
205 params = {
206 'fields[campaign]': 'show_audio_post_download_links,name,url',
207 'fields[post]': 'current_user_can_view,embed,image,is_paid,post_file,published_at,patreon_url,url,post_type,thumbnail_url,title',
208 'filter[campaign_id]': campaign_id,
209 'filter[is_draft]': 'false',
210 'sort': '-published_at',
211 'json-api-version': 1.0,
212 'json-api-use-default-includes': 'false',
213 }
214
215 for page in itertools.count(1):
216
217 params.update({'page[cursor]': cursor} if cursor else {})
218 posts_json = self._download_json('https://www.patreon.com/api/posts', user_id, note='Downloading posts page %d' % page, query=params, headers={'Cookie': '.'})
219
220 cursor = try_get(posts_json, lambda x: x['meta']['pagination']['cursors']['next'])
221
222 for post in posts_json.get('data') or []:
223 yield self.url_result(url_or_none(try_get(post, lambda x: x['attributes']['patreon_url'])), 'Patreon')
224
225 if cursor is None:
226 break
227
228 def _real_extract(self, url):
229
230 user_id = self._match_id(url)
231 webpage = self._download_webpage(url, user_id, headers={'Cookie': '.'})
232 campaign_id = self._search_regex(r'https://www.patreon.com/api/campaigns/(\d+)/?', webpage, 'Campaign ID')
233 return self.playlist_result(self._entries(campaign_id, user_id), playlist_title=user_id)