]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tumblr.py
[Theta] Fix valid URL (#2323)
[yt-dlp.git] / yt_dlp / extractor / tumblr.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 from .common import InfoExtractor
6 from ..utils import (
7 ExtractorError,
8 int_or_none,
9 urlencode_postdata
10 )
11
12
13 class TumblrIE(InfoExtractor):
14 _VALID_URL = r'https?://(?P<blog_name>[^/?#&]+)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
15 _NETRC_MACHINE = 'tumblr'
16 _LOGIN_URL = 'https://www.tumblr.com/login'
17 _TESTS = [{
18 'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
19 'md5': '479bb068e5b16462f5176a6828829767',
20 'info_dict': {
21 'id': '54196191430',
22 'ext': 'mp4',
23 'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
24 'description': 'md5:37db8211e40b50c7c44e95da14f630b7',
25 'thumbnail': r're:http://.*\.jpg',
26 }
27 }, {
28 'url': 'http://5sostrum.tumblr.com/post/90208453769/yall-forgetting-the-greatest-keek-of-them-all',
29 'md5': 'bf348ef8c0ef84fbf1cbd6fa6e000359',
30 'info_dict': {
31 'id': '90208453769',
32 'ext': 'mp4',
33 'title': '5SOS STRUM ;]',
34 'description': 'md5:dba62ac8639482759c8eb10ce474586a',
35 'thumbnail': r're:http://.*\.jpg',
36 }
37 }, {
38 'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
39 'md5': '7ae503065ad150122dc3089f8cf1546c',
40 'info_dict': {
41 'id': '130323439814',
42 'ext': 'mp4',
43 'title': 'HD Video Testing \u2014 Test description for my HD video',
44 'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
45 'thumbnail': r're:http://.*\.jpg',
46 },
47 'params': {
48 'format': 'hd',
49 },
50 }, {
51 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
52 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
53 'info_dict': {
54 'id': 'Wmur',
55 'ext': 'mp4',
56 'title': 'naked smoking & stretching',
57 'upload_date': '20150506',
58 'timestamp': 1430931613,
59 'age_limit': 18,
60 'uploader_id': '1638622',
61 'uploader': 'naked-yogi',
62 },
63 'add_ie': ['Vidme'],
64 }, {
65 'url': 'http://camdamage.tumblr.com/post/98846056295/',
66 'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
67 'info_dict': {
68 'id': '105463834',
69 'ext': 'mp4',
70 'title': 'Cam Damage-HD 720p',
71 'uploader': 'John Moyer',
72 'uploader_id': 'user32021558',
73 },
74 'add_ie': ['Vimeo'],
75 }, {
76 'url': 'http://sutiblr.tumblr.com/post/139638707273',
77 'md5': '2dd184b3669e049ba40563a7d423f95c',
78 'info_dict': {
79 'id': 'ir7qBEIKqvq',
80 'ext': 'mp4',
81 'title': 'Vine by sutiblr',
82 'alt_title': 'Vine by sutiblr',
83 'uploader': 'sutiblr',
84 'uploader_id': '1198993975374495744',
85 'upload_date': '20160220',
86 'like_count': int,
87 'comment_count': int,
88 'repost_count': int,
89 },
90 'add_ie': ['Vine'],
91 }, {
92 'url': 'http://vitasidorkina.tumblr.com/post/134652425014/joskriver-victoriassecret-invisibility-or',
93 'md5': '01c12ceb82cbf6b2fe0703aa56b3ad72',
94 'info_dict': {
95 'id': '-7LnUPGlSo',
96 'ext': 'mp4',
97 'title': 'Video by victoriassecret',
98 'description': 'Invisibility or flight…which superpower would YOU choose? #VSFashionShow #ThisOrThat',
99 'uploader_id': 'victoriassecret',
100 'thumbnail': r're:^https?://.*\.jpg'
101 },
102 'add_ie': ['Instagram'],
103 }]
104
105 def _real_initialize(self):
106 self._login()
107
108 def _login(self):
109 username, password = self._get_login_info()
110 if username is None:
111 return
112
113 login_page = self._download_webpage(
114 self._LOGIN_URL, None, 'Downloading login page')
115
116 login_form = self._hidden_inputs(login_page)
117 login_form.update({
118 'user[email]': username,
119 'user[password]': password
120 })
121
122 response, urlh = self._download_webpage_handle(
123 self._LOGIN_URL, None, 'Logging in',
124 data=urlencode_postdata(login_form), headers={
125 'Content-Type': 'application/x-www-form-urlencoded',
126 'Referer': self._LOGIN_URL,
127 })
128
129 # Successful login
130 if '/dashboard' in urlh.geturl():
131 return
132
133 login_errors = self._parse_json(
134 self._search_regex(
135 r'RegistrationForm\.errors\s*=\s*(\[.+?\])\s*;', response,
136 'login errors', default='[]'),
137 None, fatal=False)
138 if login_errors:
139 raise ExtractorError(
140 'Unable to login: %s' % login_errors[0], expected=True)
141
142 self.report_warning('Login has probably failed')
143
144 def _real_extract(self, url):
145 m_url = self._match_valid_url(url)
146 video_id = m_url.group('id')
147 blog = m_url.group('blog_name')
148
149 url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
150 webpage, urlh = self._download_webpage_handle(url, video_id)
151
152 redirect_url = urlh.geturl()
153 if 'tumblr.com/safe-mode' in redirect_url or redirect_url.startswith('/safe-mode'):
154 raise ExtractorError(
155 'This Tumblr may contain sensitive media. '
156 'Disable safe mode in your account settings '
157 'at https://www.tumblr.com/settings/account#safe_mode',
158 expected=True)
159
160 iframe_url = self._search_regex(
161 r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
162 webpage, 'iframe url', default=None)
163 if iframe_url is None:
164 return self.url_result(redirect_url, 'Generic')
165
166 iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
167
168 duration = None
169 sources = []
170
171 sd_url = self._search_regex(
172 r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
173 'sd video url', default=None, group='url')
174 if sd_url:
175 sources.append((sd_url, 'sd'))
176
177 options = self._parse_json(
178 self._search_regex(
179 r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
180 'hd video url', default='', group='options'),
181 video_id, fatal=False)
182 if options:
183 duration = int_or_none(options.get('duration'))
184 hd_url = options.get('hdUrl')
185 if hd_url:
186 sources.append((hd_url, 'hd'))
187
188 formats = [{
189 'url': video_url,
190 'ext': 'mp4',
191 'format_id': format_id,
192 'height': int_or_none(self._search_regex(
193 r'/(\d{3,4})$', video_url, 'height', default=None)),
194 'quality': quality,
195 } for quality, (video_url, format_id) in enumerate(sources)]
196
197 self._sort_formats(formats)
198
199 # The only place where you can get a title, it's not complete,
200 # but searching in other places doesn't work for all videos
201 video_title = self._html_search_regex(
202 r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
203 webpage, 'title')
204
205 return {
206 'id': video_id,
207 'title': video_title,
208 'description': self._og_search_description(webpage, default=None),
209 'thumbnail': self._og_search_thumbnail(webpage, default=None),
210 'duration': duration,
211 'formats': formats,
212 }