Merge branch 'youku' of https://github.com/PeterDing/youtube-dl into PeterDing-youku
[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 .pornhub import PornHubIE
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://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
34         'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
35         'info_dict': {
36             'id': 'Wmur',
37             'ext': 'mp4',
38             'title': 'naked smoking & stretching',
39             'upload_date': '20150506',
40             'timestamp': 1430931613,
41         },
42         'add_ie': ['Vidme'],
43     }]
44
45     def _real_extract(self, url):
46         m_url = re.match(self._VALID_URL, url)
47         video_id = m_url.group('id')
48         blog = m_url.group('blog_name')
49
50         url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
51         webpage = self._download_webpage(url, video_id)
52
53         vid_me_embed_url = self._search_regex(
54             r'src=[\'"](https?://vid\.me/[^\'"]+)[\'"]',
55             webpage, 'vid.me embed', default=None)
56         if vid_me_embed_url is not None:
57             return self.url_result(vid_me_embed_url, 'Vidme')
58
59         pornhub_url = PornHubIE._extract_url(webpage)
60         if pornhub_url:
61             return self.url_result(pornhub_url, 'PornHub')
62
63         iframe_url = self._search_regex(
64             r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
65             webpage, 'iframe url')
66         iframe = self._download_webpage(iframe_url, video_id)
67         video_url = self._search_regex(r'<source src="([^"]+)"',
68                                        iframe, 'video url')
69
70         # The only place where you can get a title, it's not complete,
71         # but searching in other places doesn't work for all videos
72         video_title = self._html_search_regex(
73             r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
74             webpage, 'title')
75
76         return {
77             'id': video_id,
78             'url': video_url,
79             'ext': 'mp4',
80             'title': video_title,
81             'description': self._og_search_description(webpage, default=None),
82             'thumbnail': self._og_search_thumbnail(webpage, default=None),
83         }