]>
Commit | Line | Data |
---|---|---|
1 | # coding: utf-8 | |
2 | from __future__ import unicode_literals | |
3 | ||
4 | from .common import InfoExtractor | |
5 | from .vimeo import VimeoIE | |
6 | ||
7 | from ..compat import compat_urllib_parse_unquote | |
8 | from ..utils import ( | |
9 | clean_html, | |
10 | determine_ext, | |
11 | int_or_none, | |
12 | KNOWN_EXTENSIONS, | |
13 | mimetype2ext, | |
14 | parse_iso8601, | |
15 | str_or_none, | |
16 | try_get, | |
17 | url_or_none | |
18 | ) | |
19 | ||
20 | ||
21 | class PatreonIE(InfoExtractor): | |
22 | _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)' | |
23 | _TESTS = [{ | |
24 | 'url': 'http://www.patreon.com/creation?hid=743933', | |
25 | 'md5': 'e25505eec1053a6e6813b8ed369875cc', | |
26 | 'info_dict': { | |
27 | 'id': '743933', | |
28 | 'ext': 'mp3', | |
29 | 'title': 'Episode 166: David Smalley of Dogma Debate', | |
30 | 'description': 'md5:713b08b772cd6271b9f3906683cfacdf', | |
31 | 'uploader': 'Cognitive Dissonance Podcast', | |
32 | 'thumbnail': 're:^https?://.*$', | |
33 | 'timestamp': 1406473987, | |
34 | 'upload_date': '20140727', | |
35 | 'uploader_id': '87145', | |
36 | }, | |
37 | }, { | |
38 | 'url': 'http://www.patreon.com/creation?hid=754133', | |
39 | 'md5': '3eb09345bf44bf60451b8b0b81759d0a', | |
40 | 'info_dict': { | |
41 | 'id': '754133', | |
42 | 'ext': 'mp3', | |
43 | 'title': 'CD 167 Extra', | |
44 | 'uploader': 'Cognitive Dissonance Podcast', | |
45 | 'thumbnail': 're:^https?://.*$', | |
46 | }, | |
47 | 'skip': 'Patron-only content', | |
48 | }, { | |
49 | 'url': 'https://www.patreon.com/creation?hid=1682498', | |
50 | 'info_dict': { | |
51 | 'id': 'SU4fj_aEMVw', | |
52 | 'ext': 'mp4', | |
53 | 'title': 'I\'m on Patreon!', | |
54 | 'uploader': 'TraciJHines', | |
55 | 'thumbnail': 're:^https?://.*$', | |
56 | 'upload_date': '20150211', | |
57 | 'description': 'md5:c5a706b1f687817a3de09db1eb93acd4', | |
58 | 'uploader_id': 'TraciJHines', | |
59 | }, | |
60 | 'params': { | |
61 | 'noplaylist': True, | |
62 | 'skip_download': True, | |
63 | } | |
64 | }, { | |
65 | 'url': 'https://www.patreon.com/posts/episode-166-of-743933', | |
66 | 'only_matching': True, | |
67 | }, { | |
68 | 'url': 'https://www.patreon.com/posts/743933', | |
69 | 'only_matching': True, | |
70 | }, { | |
71 | 'url': 'https://www.patreon.com/posts/kitchen-as-seen-51706779', | |
72 | 'md5': '96656690071f6d64895866008484251b', | |
73 | 'info_dict': { | |
74 | 'id': '555089736', | |
75 | 'ext': 'mp4', | |
76 | 'title': 'KITCHEN AS SEEN ON DEEZ NUTS EXTENDED!', | |
77 | 'uploader': 'Cold Ones', | |
78 | 'thumbnail': 're:^https?://.*$', | |
79 | 'upload_date': '20210526', | |
80 | 'description': 'md5:557a409bd79d3898689419094934ba79', | |
81 | 'uploader_id': '14936315', | |
82 | }, | |
83 | 'skip': 'Patron-only content' | |
84 | }] | |
85 | ||
86 | # Currently Patreon exposes download URL via hidden CSS, so login is not | |
87 | # needed. Keeping this commented for when this inevitably changes. | |
88 | ''' | |
89 | def _login(self): | |
90 | username, password = self._get_login_info() | |
91 | if username is None: | |
92 | return | |
93 | ||
94 | login_form = { | |
95 | 'redirectUrl': 'http://www.patreon.com/', | |
96 | 'email': username, | |
97 | 'password': password, | |
98 | } | |
99 | ||
100 | request = sanitized_Request( | |
101 | 'https://www.patreon.com/processLogin', | |
102 | compat_urllib_parse_urlencode(login_form).encode('utf-8') | |
103 | ) | |
104 | login_page = self._download_webpage(request, None, note='Logging in') | |
105 | ||
106 | if re.search(r'onLoginFailed', login_page): | |
107 | raise ExtractorError('Unable to login, incorrect username and/or password', expected=True) | |
108 | ||
109 | def _real_initialize(self): | |
110 | self._login() | |
111 | ''' | |
112 | ||
113 | def _real_extract(self, url): | |
114 | video_id = self._match_id(url) | |
115 | post = self._download_json( | |
116 | 'https://www.patreon.com/api/posts/' + video_id, video_id, query={ | |
117 | 'fields[media]': 'download_url,mimetype,size_bytes', | |
118 | 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title', | |
119 | 'fields[user]': 'full_name,url', | |
120 | 'json-api-use-default-includes': 'false', | |
121 | 'include': 'media,user', | |
122 | }) | |
123 | attributes = post['data']['attributes'] | |
124 | title = attributes['title'].strip() | |
125 | image = attributes.get('image') or {} | |
126 | info = { | |
127 | 'id': video_id, | |
128 | 'title': title, | |
129 | 'description': clean_html(attributes.get('content')), | |
130 | 'thumbnail': image.get('large_url') or image.get('url'), | |
131 | 'timestamp': parse_iso8601(attributes.get('published_at')), | |
132 | 'like_count': int_or_none(attributes.get('like_count')), | |
133 | 'comment_count': int_or_none(attributes.get('comment_count')), | |
134 | } | |
135 | ||
136 | for i in post.get('included', []): | |
137 | i_type = i.get('type') | |
138 | if i_type == 'media': | |
139 | media_attributes = i.get('attributes') or {} | |
140 | download_url = media_attributes.get('download_url') | |
141 | ext = mimetype2ext(media_attributes.get('mimetype')) | |
142 | if download_url and ext in KNOWN_EXTENSIONS: | |
143 | info.update({ | |
144 | 'ext': ext, | |
145 | 'filesize': int_or_none(media_attributes.get('size_bytes')), | |
146 | 'url': download_url, | |
147 | }) | |
148 | elif i_type == 'user': | |
149 | user_attributes = i.get('attributes') | |
150 | if user_attributes: | |
151 | info.update({ | |
152 | 'uploader': user_attributes.get('full_name'), | |
153 | 'uploader_id': str_or_none(i.get('id')), | |
154 | 'uploader_url': user_attributes.get('url'), | |
155 | }) | |
156 | ||
157 | if not info.get('url'): | |
158 | # handle Vimeo embeds | |
159 | if try_get(attributes, lambda x: x['embed']['provider']) == 'Vimeo': | |
160 | embed_html = try_get(attributes, lambda x: x['embed']['html']) | |
161 | v_url = url_or_none(compat_urllib_parse_unquote( | |
162 | self._search_regex(r'src=(https%3A%2F%2Fplayer\.vimeo\.com.+)%3F', embed_html, 'vimeo url', fatal=False))) | |
163 | if v_url: | |
164 | info.update({ | |
165 | '_type': 'url_transparent', | |
166 | 'url': VimeoIE._smuggle_referrer(v_url, 'https://patreon.com'), | |
167 | 'ie_key': 'Vimeo', | |
168 | }) | |
169 | ||
170 | if not info.get('url'): | |
171 | embed_url = try_get(attributes, lambda x: x['embed']['url']) | |
172 | if embed_url: | |
173 | info.update({ | |
174 | '_type': 'url', | |
175 | 'url': embed_url, | |
176 | }) | |
177 | ||
178 | if not info.get('url'): | |
179 | post_file = attributes['post_file'] | |
180 | ext = determine_ext(post_file.get('name')) | |
181 | if ext in KNOWN_EXTENSIONS: | |
182 | info.update({ | |
183 | 'ext': ext, | |
184 | 'url': post_file['url'], | |
185 | }) | |
186 | ||
187 | return info |