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