[voxmedia] Add new extractor(closes #3182)
[youtube-dl] / youtube_dl / extractor / voxmedia.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urllib_parse_unquote
6
7
8 class VoxMediaIE(InfoExtractor):
9     _VALID_URL = r'https?://(?:www\.)?(?:theverge|vox|sbnation|eater|polygon|curbed|racked)\.com/(?:[^/]+/)*(?P<id>[^/?]+)'
10     _TESTS = [{
11         'url': 'http://www.theverge.com/2014/6/27/5849272/material-world-how-google-discovered-what-software-is-made-of',
12         'md5': '73856edf3e89a711e70d5cf7cb280b37',
13         'info_dict': {
14             'id': '11eXZobjrG8DCSTgrNjVinU-YmmdYjhe',
15             'ext': 'mp4',
16             'title': 'Google\'s new material design direction',
17             'description': 'md5:2f44f74c4d14a1f800ea73e1c6832ad2',
18         }
19     }, {
20         # data-ooyala-id
21         'url': 'http://www.theverge.com/2014/10/21/7025853/google-nexus-6-hands-on-photos-video-android-phablet',
22         'md5': 'd744484ff127884cd2ba09e3fa604e4b',
23         'info_dict': {
24             'id': 'RkZXU4cTphOCPDMZg5oEounJyoFI0g-B',
25             'ext': 'mp4',
26             'title': 'The Nexus 6: hands-on with Google\'s phablet',
27             'description': 'md5:87a51fe95ff8cea8b5bdb9ac7ae6a6af',
28         }
29     }, {
30         # volume embed
31         'url': 'http://www.vox.com/2016/3/31/11336640/mississippi-lgbt-religious-freedom-bill',
32         'md5': '375c483c5080ab8cd85c9c84cfc2d1e4',
33         'info_dict': {
34             'id': 'wydzk3dDpmRz7PQoXRsTIX6XTkPjYL0b',
35             'ext': 'mp4',
36             'title': 'The new frontier of LGBTQ civil rights, explained',
37             'description': 'md5:0dc58e94a465cbe91d02950f770eb93f',
38         }
39     }, {
40         # youtube embed
41         'url': 'http://www.vox.com/2016/3/24/11291692/robot-dance',
42         'md5': '83b3080489fb103941e549352d3e0977',
43         'info_dict': {
44             'id': 'FcNHTJU1ufM',
45             'ext': 'mp4',
46             'title': 'How "the robot" became the greatest novelty dance of all time',
47             'description': 'md5:b081c0d588b8b2085870cda55e6da176',
48             'upload_date': '20160324',
49             'uploader_id': 'voxdotcom',
50             'uploader': 'Vox',
51         }
52     }]
53
54     def _real_extract(self, url):
55         display_id = self._match_id(url)
56         webpage = compat_urllib_parse_unquote(self._download_webpage(url, display_id))
57
58         title = None
59         description = None
60         provider_video_id = None
61         provider_video_type = None
62
63         entry = self._search_regex([
64             r'Chorus\.VideoContext\.addVideo\(\[({.+})\]\);',
65             r'var\s+entry\s*=\s*({.+});'
66         ], webpage, 'video data', default=None)
67         if entry:
68             video_data = self._parse_json(entry, display_id)
69             provider_video_id = video_data.get('provider_video_id')
70             provider_video_type = video_data.get('provider_video_type')
71             if provider_video_id and provider_video_type:
72                 title = video_data.get('title')
73                 description = video_data.get('description')
74
75         if not provider_video_id or not provider_video_type:
76             provider_video_id = self._search_regex(
77                 r'data-ooyala-id="([^"]+)"', webpage, 'ooyala id', default=None)
78             if provider_video_id:
79                 provider_video_type = 'ooyala'
80             else:
81                 volume_uuid = self._search_regex(r'data-volume-uuid="([^"]+)"', webpage, 'volume uuid')
82                 volume_webpage = self._download_webpage(
83                     'http://volume.vox-cdn.com/embed/%s' % volume_uuid, volume_uuid)
84                 video_data = self._parse_json(self._search_regex(
85                     r'Volume\.createVideo\(({.+})\s*,\s*{.*}\);', volume_webpage, 'video data'), volume_uuid)
86                 title = video_data.get('title_short')
87                 description = video_data.get('description_long') or video_data.get('description_short')
88                 for pvtype in ('ooyala', 'youtube'):
89                     provider_video_id = video_data.get('%s_id' % pvtype)
90                     if provider_video_id:
91                         provider_video_type = pvtype
92                         break
93
94         return {
95             '_type': 'url_transparent',
96             'url': provider_video_id if provider_video_type == 'youtube' else '%s:%s' % (provider_video_type, provider_video_id),
97             'title': title or self._og_search_title(webpage),
98             'description': description or self._og_search_description(webpage),
99         }