Merge remote-tracking branch 'dstftw/escape-non-ascii-in-urls'
[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 )
10
11
12 class TumblrIE(InfoExtractor):
13     _VALID_URL = r'http://(?P<blog_name>.*?)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
14     _TESTS = [{
15         'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
16         'md5': '479bb068e5b16462f5176a6828829767',
17         'info_dict': {
18             'id': '54196191430',
19             'ext': 'mp4',
20             'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
21             'description': 'md5:dfac39636969fe6bf1caa2d50405f069',
22             'thumbnail': 're:http://.*\.jpg',
23         }
24     }, {
25         'url': 'http://5sostrum.tumblr.com/post/90208453769/yall-forgetting-the-greatest-keek-of-them-all',
26         'md5': 'bf348ef8c0ef84fbf1cbd6fa6e000359',
27         'info_dict': {
28             'id': '90208453769',
29             'ext': 'mp4',
30             'title': '5SOS STRUM ;)',
31             'description': 'md5:dba62ac8639482759c8eb10ce474586a',
32             'thumbnail': 're:http://.*\.jpg',
33         }
34     }]
35
36     def _real_extract(self, url):
37         m_url = re.match(self._VALID_URL, url)
38         video_id = m_url.group('id')
39         blog = m_url.group('blog_name')
40
41         url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
42         webpage = self._download_webpage(url, video_id)
43
44         re_video = r'src=\\x22(?P<video_url>http://%s\.tumblr\.com/video_file/%s/(.*?))\\x22 type=\\x22video/(?P<ext>.*?)\\x22' % (blog, video_id)
45         video = re.search(re_video, webpage)
46         if video is None:
47             raise ExtractorError('Unable to extract video')
48         video_url = video.group('video_url')
49         ext = video.group('ext')
50
51         video_thumbnail = self._search_regex(
52             r'posters.*?\[\\x22(.*?)\\x22',
53             webpage, 'thumbnail', fatal=False)  # We pick the first poster
54         if video_thumbnail:
55             video_thumbnail = video_thumbnail.replace('\\\\/', '/')
56
57         # The only place where you can get a title, it's not complete,
58         # but searching in other places doesn't work for all videos
59         video_title = self._html_search_regex(
60             r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
61             webpage, 'title')
62
63         return {
64             'id': video_id,
65              'url': video_url,
66              'title': video_title,
67              'description': self._html_search_meta('description', webpage),
68              'thumbnail': video_thumbnail,
69              'ext': ext,
70         }