]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tumblr.py
[extractor] Common function `_match_valid_url`
[yt-dlp.git] / yt_dlp / extractor / tumblr.py
CommitLineData
dcdb292f 1# coding: utf-8
c060b774
PH
2from __future__ import unicode_literals
3
ae287755
PH
4
5from .common import InfoExtractor
c678192a
ZF
6from ..utils import (
7 ExtractorError,
8 int_or_none,
c678192a
ZF
9 urlencode_postdata
10)
ae287755
PH
11
12
13class TumblrIE(InfoExtractor):
afca767d 14 _VALID_URL = r'https?://(?P<blog_name>[^/?#&]+)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
c678192a
ZF
15 _NETRC_MACHINE = 'tumblr'
16 _LOGIN_URL = 'https://www.tumblr.com/login'
62f1f950 17 _TESTS = [{
c060b774 18 'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
c060b774
PH
19 'md5': '479bb068e5b16462f5176a6828829767',
20 'info_dict': {
62f1f950
PP
21 'id': '54196191430',
22 'ext': 'mp4',
23 'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
681b9caa 24 'description': 'md5:37db8211e40b50c7c44e95da14f630b7',
ec85ded8 25 'thumbnail': r're:http://.*\.jpg',
6f5ac90c 26 }
62f1f950
PP
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',
681b9caa 33 'title': '5SOS STRUM ;]',
62f1f950 34 'description': 'md5:dba62ac8639482759c8eb10ce474586a',
ec85ded8 35 'thumbnail': r're:http://.*\.jpg',
62f1f950 36 }
2a27e662
OA
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',
ec85ded8 45 'thumbnail': r're:http://.*\.jpg',
2a27e662
OA
46 },
47 'params': {
48 'format': 'hd',
49 },
8f947841
YCH
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,
88c86d21
S
59 'age_limit': 18,
60 'uploader_id': '1638622',
61 'uploader': 'naked-yogi',
8f947841
YCH
62 },
63 'add_ie': ['Vidme'],
c5895d5d
YCH
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'],
fc27ea94
YCH
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'],
32d88410
YCH
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',
ec85ded8 100 'thumbnail': r're:^https?://.*\.jpg'
32d88410
YCH
101 },
102 'add_ie': ['Instagram'],
62f1f950 103 }]
ae287755 104
c678192a
ZF
105 def _real_initialize(self):
106 self._login()
107
108 def _login(self):
68217024 109 username, password = self._get_login_info()
c678192a
ZF
110 if username is None:
111 return
56cd31f3
S
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({
c678192a
ZF
118 'user[email]': username,
119 'user[password]': password
120 })
56cd31f3
S
121
122 response, urlh = self._download_webpage_handle(
123 self._LOGIN_URL, None, 'Logging in',
124 data=urlencode_postdata(login_form), headers={
c678192a 125 'Content-Type': 'application/x-www-form-urlencoded',
56cd31f3
S
126 'Referer': self._LOGIN_URL,
127 })
c678192a 128
56cd31f3
S
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)
c678192a 138 if login_errors:
56cd31f3
S
139 raise ExtractorError(
140 'Unable to login: %s' % login_errors[0], expected=True)
141
142 self.report_warning('Login has probably failed')
c678192a 143
ae287755 144 def _real_extract(self, url):
5ad28e7f 145 m_url = self._match_valid_url(url)
ae287755
PH
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)
c909e582 150 webpage, urlh = self._download_webpage_handle(url, video_id)
c5895d5d 151
7947a1f7 152 redirect_url = urlh.geturl()
97b01144
S
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
681b9caa
JMF
160 iframe_url = self._search_regex(
161 r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
c909e582
YCH
162 webpage, 'iframe url', default=None)
163 if iframe_url is None:
97b01144 164 return self.url_result(redirect_url, 'Generic')
c909e582 165
140ac739
S
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'))
2a27e662 187
140ac739
S
188 formats = [{
189 'url': video_url,
2a27e662 190 'ext': 'mp4',
140ac739
S
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)]
2a27e662 196
140ac739 197 self._sort_formats(formats)
ae287755
PH
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
3da0e1f8
PH
201 video_title = self._html_search_regex(
202 r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
203 webpage, 'title')
ae287755 204
3da0e1f8
PH
205 return {
206 'id': video_id,
681b9caa 207 'title': video_title,
ae849ca1
YCH
208 'description': self._og_search_description(webpage, default=None),
209 'thumbnail': self._og_search_thumbnail(webpage, default=None),
140ac739
S
210 'duration': duration,
211 'formats': formats,
3da0e1f8 212 }