]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/patreon.py
[youtube] Force `hl=en` for comments (#594)
[yt-dlp.git] / yt_dlp / extractor / patreon.py
CommitLineData
dcdb292f 1# coding: utf-8
a00d73c8
EJ
2from __future__ import unicode_literals
3
a00d73c8 4from .common import InfoExtractor
65af1839 5from .vimeo import VimeoIE
6
7from ..compat import compat_urllib_parse_unquote
c9d891f1
RA
8from ..utils import (
9 clean_html,
10 determine_ext,
11 int_or_none,
b77c3949
RA
12 KNOWN_EXTENSIONS,
13 mimetype2ext,
c9d891f1 14 parse_iso8601,
b77c3949
RA
15 str_or_none,
16 try_get,
65af1839 17 url_or_none
c9d891f1 18)
a00d73c8
EJ
19
20
a00d73c8 21class PatreonIE(InfoExtractor):
c9d891f1
RA
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',
b77c3949 35 'uploader_id': '87145',
c9d891f1
RA
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?://.*$',
a00d73c8 46 },
c9d891f1
RA
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',
6994e706 59 },
c9d891f1
RA
60 'params': {
61 'noplaylist': True,
62 'skip_download': True,
6b961a85 63 }
c9d891f1
RA
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,
65af1839 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'
c9d891f1 84 }]
a00d73c8
EJ
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):
68217024 90 username, password = self._get_login_info()
a00d73c8
EJ
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
5c2266df 100 request = sanitized_Request(
a00d73c8 101 'https://www.patreon.com/processLogin',
15707c7e 102 compat_urllib_parse_urlencode(login_form).encode('utf-8')
a00d73c8 103 )
e4d95865 104 login_page = self._download_webpage(request, None, note='Logging in')
a00d73c8
EJ
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):
77070040 114 video_id = self._match_id(url)
c9d891f1 115 post = self._download_json(
b77c3949
RA
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 })
c9d891f1
RA
123 attributes = post['data']['attributes']
124 title = attributes['title'].strip()
125 image = attributes.get('image') or {}
126 info = {
e05f6939 127 'id': video_id,
e05f6939 128 'title': title,
c9d891f1
RA
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')),
e05f6939 134 }
c9d891f1
RA
135
136 for i in post.get('included', []):
137 i_type = i.get('type')
b77c3949
RA
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 })
c9d891f1
RA
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'),
b77c3949 153 'uploader_id': str_or_none(i.get('id')),
c9d891f1
RA
154 'uploader_url': user_attributes.get('url'),
155 })
156
65af1839 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
19a35285 170 if not info.get('url'):
b77c3949
RA
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 })
19a35285 177
c9d891f1 178 if not info.get('url'):
b77c3949
RA
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 })
c9d891f1
RA
186
187 return info