Merge remote-tracking branch 'mike/tumblr-url'
[youtube-dl] / youtube_dl / extractor / tumblr.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     ExtractorError,
8 )
9
10
11 class TumblrIE(InfoExtractor):
12     _VALID_URL = r'http://(?P<blog_name>.*?)\.tumblr\.com/((post)|(video))/(?P<id>\d*)($|/)'
13     _TEST = {
14         'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
15         'file': '54196191430.mp4',
16         'md5': '479bb068e5b16462f5176a6828829767',
17         'info_dict': {
18             "title": "tatiana maslany news"
19         }
20     }
21
22     def _real_extract(self, url):
23         m_url = re.match(self._VALID_URL, url)
24         video_id = m_url.group('id')
25         blog = m_url.group('blog_name')
26
27         url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
28         webpage = self._download_webpage(url, video_id)
29
30         re_video = r'src=\\x22(?P<video_url>http://%s\.tumblr\.com/video_file/%s/(.*?))\\x22 type=\\x22video/(?P<ext>.*?)\\x22' % (blog, video_id)
31         video = re.search(re_video, webpage)
32         if video is None:
33             raise ExtractorError('Unable to extract video')
34         video_url = video.group('video_url')
35         ext = video.group('ext')
36
37         video_thumbnail = self._search_regex(
38             r'posters.*?\[\\x22(.*?)\\x22',
39             webpage, 'thumbnail', fatal=False)  # We pick the first poster
40         if video_thumbnail:
41             video_thumbnail = video_thumbnail.replace('\\\\/', '/')
42
43         # The only place where you can get a title, it's not complete,
44         # but searching in other places doesn't work for all videos
45         video_title = self._html_search_regex(r'<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
46             webpage, 'title', flags=re.DOTALL)
47
48         return [{'id': video_id,
49                  'url': video_url,
50                  'title': video_title,
51                  'thumbnail': video_thumbnail,
52                  'ext': ext
53                  }]