2 from __future__
import unicode_literals
5 from .common
import InfoExtractor
14 class TumblrIE(InfoExtractor
):
15 _VALID_URL
= r
'https?://(?P<blog_name>[^/?#&]+)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
16 _NETRC_MACHINE
= 'tumblr'
17 _LOGIN_URL
= 'https://www.tumblr.com/login'
18 _OAUTH_URL
= 'https://www.tumblr.com/api/v2/oauth2/token'
20 'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
21 'md5': '479bb068e5b16462f5176a6828829767',
25 'title': 'md5:dfac39636969fe6bf1caa2d50405f069',
26 'description': 'md5:390ab77358960235b6937ab3b8528956',
27 'uploader_id': 'tatianamaslanydaily',
28 'uploader_url': 'https://tatianamaslanydaily.tumblr.com/',
29 'thumbnail': r
're:^https?://.*\.jpg',
34 'tags': ['Orphan Black', 'Tatiana Maslany', 'Interview', 'Video', 'OB S1 DVD Extras'],
37 'note': 'multiple formats',
38 'url': 'https://maskofthedragon.tumblr.com/post/626907179849564160/mona-talking-in-english',
39 'md5': 'f43ff8a8861712b6cf0e0c2bd84cfc68',
41 'id': '626907179849564160',
43 'title': 'Mona\xa0“talking” in\xa0“english”',
44 'description': 'md5:082a3a621530cb786ad2b7592a6d9e2c',
45 'uploader_id': 'maskofthedragon',
46 'uploader_url': 'https://maskofthedragon.tumblr.com/',
47 'thumbnail': r
're:^https?://.*\.jpg',
58 'note': 'non-iframe video (with related posts)',
59 'url': 'https://shieldfoss.tumblr.com/post/675519763813908480',
60 'md5': '12bdb75661ef443bffe5a4dac1dbf118',
62 'id': '675519763813908480',
64 'title': 'Shieldfoss',
65 'uploader_id': 'nerviovago',
66 'uploader_url': 'https://nerviovago.tumblr.com/',
67 'thumbnail': r
're:^https?://.*\.jpg',
74 'note': 'dashboard only (original post)',
75 'url': 'https://jujanon.tumblr.com/post/159704441298/my-baby-eating',
76 'md5': '029f7c91ab386701b211e3d494d2d95e',
80 'title': 'md5:ba79365861101f4911452728d2950561',
81 'description': 'md5:773738196cea76b6996ec71e285bdabc',
82 'uploader_id': 'jujanon',
83 'uploader_url': 'https://jujanon.tumblr.com/',
84 'thumbnail': r
're:^https?://.*\.jpg',
88 'tags': ['crabs', 'my video', 'my pets'],
91 'note': 'dashboard only (reblog)',
92 'url': 'https://bartlebyshop.tumblr.com/post/180294460076/duality-of-bird',
93 'md5': '04334e7cadb1af680d162912559f51a5',
97 'title': 'duality of bird',
98 'description': 'duality of bird',
99 'uploader_id': 'todaysbird',
100 'uploader_url': 'https://todaysbird.tumblr.com/',
101 'thumbnail': r
're:^https?://.*\.jpg',
108 'note': 'dashboard only (external)',
109 'url': 'https://afloweroutofstone.tumblr.com/post/675661759168823296/the-blues-remembers-everything-the-country-forgot',
113 'title': 'The Blues Remembers Everything the Country Forgot',
114 'alt_title': 'The Blues Remembers Everything the Country Forgot',
115 'description': 'md5:1a6b4097e451216835a24c1023707c79',
116 'release_date': '20201224',
117 'creator': 'md5:c2239ba15430e87c3b971ba450773272',
118 'uploader': 'Moor Mother - Topic',
119 'upload_date': '20201223',
120 'uploader_id': 'UCxrMtFBRkFvQJ_vVM4il08w',
121 'uploader_url': 'http://www.youtube.com/channel/UCxrMtFBRkFvQJ_vVM4il08w',
122 'thumbnail': r
're:^https?://i.ytimg.com/.*',
123 'channel': 'Moor Mother - Topic',
124 'channel_id': 'UCxrMtFBRkFvQJ_vVM4il08w',
125 'channel_url': 'https://www.youtube.com/channel/UCxrMtFBRkFvQJ_vVM4il08w',
126 'channel_follower_count': int,
131 'categories': ['Music'],
133 'live_status': 'not_live',
134 'playable_in_embed': True,
135 'availability': 'public',
136 'track': 'The Blues Remembers Everything the Country Forgot',
137 'artist': 'md5:c2239ba15430e87c3b971ba450773272',
139 'release_year': 2020,
141 'add_ie': ['Youtube'],
143 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
144 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
148 'title': 'naked smoking & stretching',
149 'upload_date': '20150506',
150 'timestamp': 1430931613,
152 'uploader_id': '1638622',
153 'uploader': 'naked-yogi',
155 # 'add_ie': ['Vidme'],
156 'skip': 'dead embedded video host'
158 'url': 'https://prozdvoices.tumblr.com/post/673201091169681408/what-recording-voice-acting-sounds-like',
159 'md5': 'a0063fc8110e6c9afe44065b4ea68177',
163 'title': 'what recording voice acting sounds like',
164 'description': 'md5:1da3faa22d0e0b1d8b50216c284ee798',
166 'upload_date': '20220112',
167 'uploader_id': 'ProZD',
168 'uploader_url': 'http://www.youtube.com/user/ProZD',
169 'thumbnail': r
're:^https?://i.ytimg.com/.*',
171 'channel_id': 'UC6MFZAOHXlKK1FI7V0XQVeA',
172 'channel_url': 'https://www.youtube.com/channel/UC6MFZAOHXlKK1FI7V0XQVeA',
173 'channel_follower_count': int,
178 'categories': ['Film & Animation'],
180 'live_status': 'not_live',
181 'playable_in_embed': True,
182 'availability': 'public',
184 'add_ie': ['Youtube'],
186 'url': 'https://dominustempori.tumblr.com/post/673572712813297664/youtubes-all-right-for-some-pretty-cool',
187 'md5': '203e9eb8077e3f45bfaeb4c86c1467b8',
191 'title': 'Harold Ramis',
192 'description': 'md5:be8e68cbf56ce0785c77f0c6c6dfaf2c',
193 'uploader': 'Resolution Productions Group',
194 'uploader_id': 'resolutionproductions',
195 'uploader_url': 'https://vimeo.com/resolutionproductions',
196 'upload_date': '20140227',
197 'thumbnail': r
're:^https?://i.vimeocdn.com/video/.*',
198 'timestamp': 1393523719,
203 'url': 'http://sutiblr.tumblr.com/post/139638707273',
204 'md5': '2dd184b3669e049ba40563a7d423f95c',
208 'title': 'Vine by sutiblr',
209 'alt_title': 'Vine by sutiblr',
210 'uploader': 'sutiblr',
211 'uploader_id': '1198993975374495744',
212 'upload_date': '20160220',
214 'comment_count': int,
216 'thumbnail': r
're:^https?://.*\.jpg',
217 'timestamp': 1455940159,
222 'url': 'https://silami.tumblr.com/post/84250043974/my-bad-river-flows-in-you-impression-on-maschine',
223 'md5': '3c92d7c3d867f14ccbeefa2119022277',
227 'title': 'Video by silbulterman',
228 'description': '#maschine',
229 'uploader_id': '242859024',
230 'thumbnail': r
're:^https?://.*\.jpg',
231 'timestamp': 1398801174,
234 'channel': 'silbulterman',
235 'comment_count': int,
236 'upload_date': '20140429',
238 'add_ie': ['Instagram'],
242 'instagram': 'Instagram',
245 'youtube': 'Youtube',
250 def _initialize_pre_login(self
):
251 login_page
= self
._download
_webpage
(
252 self
._LOGIN
_URL
, None, 'Downloading login page', fatal
=False)
254 self
._ACCESS
_TOKEN
= self
._search
_regex
(
255 r
'"API_TOKEN":\s*"(\w+)"', login_page
, 'API access token', fatal
=False)
256 if not self
._ACCESS
_TOKEN
:
257 self
.report_warning('Failed to get access token; metadata will be missing and some videos may not work')
259 def _perform_login(self
, username
, password
):
260 if not self
._ACCESS
_TOKEN
:
264 self
._OAUTH
_URL
, None, 'Logging in',
265 data
=urlencode_postdata({
266 'password': password
,
267 'grant_type': 'password',
268 'username': username
,
270 'Content-Type': 'application/x-www-form-urlencoded',
271 'Authorization': f
'Bearer {self._ACCESS_TOKEN}',
273 errnote
='Login failed', fatal
=False)
275 def _real_extract(self
, url
):
276 blog
, video_id
= self
._match
_valid
_url
(url
).groups()
278 url
= f
'http://{blog}.tumblr.com/post/{video_id}/'
279 webpage
, urlh
= self
._download
_webpage
_handle
(url
, video_id
)
281 redirect_url
= urlh
.geturl()
283 api_only
= bool(self
._search
_regex
(
284 r
'(tumblr.com|^)/(safe-mode|login_required|blog/view)',
285 redirect_url
, 'redirect', default
=None))
287 if api_only
and not self
._ACCESS
_TOKEN
:
288 raise ExtractorError('Cannot get data for dashboard-only post without access token')
291 if self
._ACCESS
_TOKEN
:
292 post_json
= traverse_obj(
294 f
'https://www.tumblr.com/api/v2/blog/{blog}/posts/{video_id}/permalink',
295 video_id
, headers
={'Authorization': f'Bearer {self._ACCESS_TOKEN}
'}, fatal=False),
296 ('response
', 'timeline
', 'elements
', 0)) or {}
297 content_json = traverse_obj(post_json, ('trail
', 0, 'content
'), ('content
')) or []
299 (item for item in content_json if item.get('type') == 'video
'), {})
300 media_json = video_json.get('media
') or {}
301 if api_only and not media_json.get('url
') and not video_json.get('url
'):
302 raise ExtractorError('Failed to find video data
for dashboard
-only post
')
304 if not media_json.get('url
') and video_json.get('url
'):
305 # external video host
306 return self.url_result(
308 self._providers.get(video_json.get('provider
'), 'Generic
'))
310 video_url = self._og_search_video_url(webpage, default=None)
314 # iframes can supply duration and sometimes additional formats, so check for one
315 iframe_url = self._search_regex(
316 fr'src
=\'(https?
://www\
.tumblr\
.com
/video
/{blog}
/{video_id}
/[^
\']+)\'',
317 webpage, 'iframe url
', default=None)
319 iframe = self._download_webpage(
320 iframe_url, video_id, 'Downloading iframe page
',
321 headers={'Referer': redirect_url})
323 options = self._parse_json(
325 r'data
-crt
-options
=(["\'])(?P<options>.+?)\1', iframe,
326 'hd video url', default='', group='options'),
327 video_id, fatal=False)
329 duration = int_or_none(options.get('duration'))
331 hd_url = options.get('hdUrl')
333 # there are multiple formats; extract them
334 # ignore other sources of width/height data as they may be wrong
336 sd_url = self._search_regex(
337 r'<source[^>]+src=(["\'])(?P
<url
>.+?
)\
1', iframe,
338 'sd video url
', default=None, group='url
')
340 sources.append((sd_url, 'sd
'))
341 sources.append((hd_url, 'hd
'))
345 'format_id
': format_id,
346 'height
': int_or_none(self._search_regex(
347 r'_(\d
+)\
.\w
+$
', video_url, 'height
', default=None)),
349 } for quality, (video_url, format_id) in enumerate(sources)]
351 if not media_json.get('url
') and not video_url and not iframe_url:
352 # external video host (but we weren't able to figure it out
from the api
)
353 iframe_url
= self
._search
_regex
(
354 r
'src=["\'](https?
://safe\
.txmblr\
.com
/svc
/embed
/inline
/[^
"\']+)["\']',
355 webpage, 'embed iframe url
', default=None)
356 return self.url_result(iframe_url or redirect_url, 'Generic
')
358 formats = formats or [{
359 'url
': media_json.get('url
') or video_url,
360 'width
': int_or_none(
361 media_json.get('width
') or self._og_search_property('video
:width
', webpage, default=None)),
362 'height
': int_or_none(
363 media_json.get('height
') or self._og_search_property('video
:height
', webpage, default=None)),
365 self._sort_formats(formats)
367 # the url we're extracting
from might be an original post
or it might be a reblog
.
368 # if it's a reblog, og:description will be the reblogger's comment, not the uploader's.
369 # content_json is always the op, so if it exists but has no text, there's no description
371 description
= '\n\n'.join((
372 item
.get('text') for item
in content_json
if item
.get('type') == 'text')) or None
374 description
= self
._og
_search
_description
(webpage
, default
=None)
375 uploader_id
= traverse_obj(post_json
, 'reblogged_root_name', 'blog_name')
379 'title': post_json
.get('summary') or (blog
if api_only
else self
._html
_search
_regex
(
380 r
'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>', webpage
, 'title')),
381 'description': description
,
382 'thumbnail': (traverse_obj(video_json
, ('poster', 0, 'url'))
383 or self
._og
_search
_thumbnail
(webpage
, default
=None)),
384 'uploader_id': uploader_id
,
385 'uploader_url': f
'https://{uploader_id}.tumblr.com/' if uploader_id
else None,
386 'duration': duration
,
387 'like_count': post_json
.get('like_count'),
388 'repost_count': post_json
.get('reblog_count'),
389 'age_limit': {True: 18, False: 0}
.get(post_json
.get('is_nsfw')),
390 'tags': post_json
.get('tags'),