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