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