[tumblr] Added support for HD video where available (#7036)
[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
8
9 class TumblrIE(InfoExtractor):
10     _VALID_URL = r'http://(?P<blog_name>.*?)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
11     _TESTS = [{
12         'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
13         'md5': '479bb068e5b16462f5176a6828829767',
14         'info_dict': {
15             'id': '54196191430',
16             'ext': 'mp4',
17             'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
18             'description': 'md5:37db8211e40b50c7c44e95da14f630b7',
19             'thumbnail': 're:http://.*\.jpg',
20         }
21     }, {
22         'url': 'http://5sostrum.tumblr.com/post/90208453769/yall-forgetting-the-greatest-keek-of-them-all',
23         'md5': 'bf348ef8c0ef84fbf1cbd6fa6e000359',
24         'info_dict': {
25             'id': '90208453769',
26             'ext': 'mp4',
27             'title': '5SOS STRUM ;]',
28             'description': 'md5:dba62ac8639482759c8eb10ce474586a',
29             'thumbnail': 're:http://.*\.jpg',
30         }
31     }, {
32         'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
33         'md5': '99a84522f60972bf064a0b80f87bcbb5',
34         'info_dict': {
35             'id': '130323439814',
36             'ext': 'mp4',
37             'title': 'HD Video Testing \u2014 Test description for my HD video',
38             'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
39             'thumbnail': 're:http://.*\.jpg',
40         },
41         'params': {
42             'format': 'sd',
43         },
44     }, {
45         'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
46         'md5': '7ae503065ad150122dc3089f8cf1546c',
47         'info_dict': {
48             'id': '130323439814',
49             'ext': 'mp4',
50             'title': 'HD Video Testing \u2014 Test description for my HD video',
51             'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
52             'thumbnail': 're:http://.*\.jpg',
53         },
54         'params': {
55             'format': 'hd',
56         },
57     }, {
58         'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
59         'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
60         'info_dict': {
61             'id': 'Wmur',
62             'ext': 'mp4',
63             'title': 'naked smoking & stretching',
64             'upload_date': '20150506',
65             'timestamp': 1430931613,
66         },
67         'add_ie': ['Vidme'],
68     }, {
69         'url': 'http://camdamage.tumblr.com/post/98846056295/',
70         'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
71         'info_dict': {
72             'id': '105463834',
73             'ext': 'mp4',
74             'title': 'Cam Damage-HD 720p',
75             'uploader': 'John Moyer',
76             'uploader_id': 'user32021558',
77         },
78         'add_ie': ['Vimeo'],
79     }]
80
81     def _real_extract(self, url):
82         m_url = re.match(self._VALID_URL, url)
83         video_id = m_url.group('id')
84         blog = m_url.group('blog_name')
85
86         video_urls = []
87
88         url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
89         webpage, urlh = self._download_webpage_handle(url, video_id)
90
91         iframe_url = self._search_regex(
92             r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
93             webpage, 'iframe url', default=None)
94         if iframe_url is None:
95             return self.url_result(urlh.geturl(), 'Generic')
96
97         iframe = self._download_webpage(iframe_url, video_id,
98                                         'Downloading iframe page')
99
100         sd_video_url = self._search_regex(r'<source src="([^"]+)"',
101                                           iframe, 'sd video url')
102         resolution_id = sd_video_url.split("/")[-1] + 'p'
103         if len(resolution_id) != 4:
104             resolution_id = None
105         video_urls.append({
106             'ext': 'mp4',
107             'format_id': 'sd',
108             'url': sd_video_url,
109             'resolution': resolution_id,
110         })
111
112         hd_video_url = self._search_regex(r'hdUrl":"([^"]+)"', iframe,
113                                           'hd video url', default=None)
114         if hd_video_url:
115             hd_video_url = hd_video_url.replace("\\", "")
116             resolution_id = hd_video_url.split("/")[-1] + 'p'
117             if len(resolution_id) != 4:
118                 resolution_id = None
119             video_urls.append({
120                 'ext': 'mp4',
121                 'format_id': 'hd',
122                 'url': hd_video_url,
123                 'resolution': resolution_id,
124             })
125
126         # The only place where you can get a title, it's not complete,
127         # but searching in other places doesn't work for all videos
128         video_title = self._html_search_regex(
129             r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
130             webpage, 'title')
131
132         return {
133             'id': video_id,
134             'formats': video_urls,
135             'ext': 'mp4',
136             'title': video_title,
137             'description': self._og_search_description(webpage, default=None),
138             'thumbnail': self._og_search_thumbnail(webpage, default=None),
139         }