]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/tumblr.py
[tumblr] Simplify and extract duration
[yt-dlp.git] / 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'http://(?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': '99a84522f60972bf064a0b80f87bcbb5',
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': 'sd',
44 },
45 }, {
46 'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
47 'md5': '7ae503065ad150122dc3089f8cf1546c',
48 'info_dict': {
49 'id': '130323439814',
50 'ext': 'mp4',
51 'title': 'HD Video Testing \u2014 Test description for my HD video',
52 'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
53 'thumbnail': 're:http://.*\.jpg',
54 },
55 'params': {
56 'format': 'hd',
57 },
58 }, {
59 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
60 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
61 'info_dict': {
62 'id': 'Wmur',
63 'ext': 'mp4',
64 'title': 'naked smoking & stretching',
65 'upload_date': '20150506',
66 'timestamp': 1430931613,
67 },
68 'add_ie': ['Vidme'],
69 }, {
70 'url': 'http://camdamage.tumblr.com/post/98846056295/',
71 'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
72 'info_dict': {
73 'id': '105463834',
74 'ext': 'mp4',
75 'title': 'Cam Damage-HD 720p',
76 'uploader': 'John Moyer',
77 'uploader_id': 'user32021558',
78 },
79 'add_ie': ['Vimeo'],
80 }]
81
82 def _real_extract(self, url):
83 m_url = re.match(self._VALID_URL, url)
84 video_id = m_url.group('id')
85 blog = m_url.group('blog_name')
86
87 url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
88 webpage, urlh = self._download_webpage_handle(url, video_id)
89
90 iframe_url = self._search_regex(
91 r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
92 webpage, 'iframe url', default=None)
93 if iframe_url is None:
94 return self.url_result(urlh.geturl(), 'Generic')
95
96 iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
97
98 duration = None
99 sources = []
100
101 sd_url = self._search_regex(
102 r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
103 'sd video url', default=None, group='url')
104 if sd_url:
105 sources.append((sd_url, 'sd'))
106
107 options = self._parse_json(
108 self._search_regex(
109 r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
110 'hd video url', default='', group='options'),
111 video_id, fatal=False)
112 if options:
113 duration = int_or_none(options.get('duration'))
114 hd_url = options.get('hdUrl')
115 if hd_url:
116 sources.append((hd_url, 'hd'))
117
118 formats = [{
119 'url': video_url,
120 'ext': 'mp4',
121 'format_id': format_id,
122 'height': int_or_none(self._search_regex(
123 r'/(\d{3,4})$', video_url, 'height', default=None)),
124 'quality': quality,
125 } for quality, (video_url, format_id) in enumerate(sources)]
126
127 self._sort_formats(formats)
128
129 # The only place where you can get a title, it's not complete,
130 # but searching in other places doesn't work for all videos
131 video_title = self._html_search_regex(
132 r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
133 webpage, 'title')
134
135 return {
136 'id': video_id,
137 'ext': 'mp4',
138 'title': video_title,
139 'description': self._og_search_description(webpage, default=None),
140 'thumbnail': self._og_search_thumbnail(webpage, default=None),
141 'duration': duration,
142 'formats': formats,
143 }