Merge branch 'daum' of https://github.com/remitamine/youtube-dl into remitamine-daum
[youtube-dl] / youtube_dl / extractor / nowness.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .brightcove import (
5     BrightcoveLegacyIE,
6     BrightcoveNewIE,
7 )
8 from .common import InfoExtractor
9 from ..compat import compat_str
10 from ..utils import (
11     ExtractorError,
12     sanitized_Request,
13 )
14
15
16 class NownessBaseIE(InfoExtractor):
17     def _extract_url_result(self, post):
18         if post['type'] == 'video':
19             for media in post['media']:
20                 if media['type'] == 'video':
21                     video_id = media['content']
22                     source = media['source']
23                     if source == 'brightcove':
24                         player_code = self._download_webpage(
25                             'http://www.nowness.com/iframe?id=%s' % video_id, video_id,
26                             note='Downloading player JavaScript',
27                             errnote='Unable to download player JavaScript')
28                         bc_url = BrightcoveLegacyIE._extract_brightcove_url(player_code)
29                         if bc_url:
30                             return self.url_result(bc_url, BrightcoveLegacyIE.ie_key())
31                         bc_url = BrightcoveNewIE._extract_url(player_code)
32                         if bc_url:
33                             return self.url_result(bc_url, BrightcoveNewIE.ie_key())
34                         raise ExtractorError('Could not find player definition')
35                     elif source == 'vimeo':
36                         return self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo')
37                     elif source == 'youtube':
38                         return self.url_result(video_id, 'Youtube')
39                     elif source == 'cinematique':
40                         # youtube-dl currently doesn't support cinematique
41                         # return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')
42                         pass
43
44     def _api_request(self, url, request_path):
45         display_id = self._match_id(url)
46         request = sanitized_Request(
47             'http://api.nowness.com/api/' + request_path % display_id,
48             headers={
49                 'X-Nowness-Language': 'zh-cn' if 'cn.nowness.com' in url else 'en-us',
50             })
51         return display_id, self._download_json(request, display_id)
52
53
54 class NownessIE(NownessBaseIE):
55     IE_NAME = 'nowness'
56     _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P<id>[^/]+?)(?:$|[?#])'
57     _TESTS = [{
58         'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation',
59         'md5': '068bc0202558c2e391924cb8cc470676',
60         'info_dict': {
61             'id': '2520295746001',
62             'ext': 'mp4',
63             'title': 'Candor: The Art of Gesticulation',
64             'description': 'Candor: The Art of Gesticulation',
65             'thumbnail': 're:^https?://.*\.jpg',
66             'uploader': 'Nowness',
67         },
68     }, {
69         'url': 'https://cn.nowness.com/story/kasper-bjorke-ft-jaakko-eino-kalevi-tnr',
70         'md5': 'e79cf125e387216f86b2e0a5b5c63aa3',
71         'info_dict': {
72             'id': '3716354522001',
73             'ext': 'mp4',
74             'title': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
75             'description': 'Kasper Bjørke ft. Jaakko Eino Kalevi: TNR',
76             'thumbnail': 're:^https?://.*\.jpg',
77             'uploader': 'Nowness',
78         },
79     }, {
80         # vimeo
81         'url': 'https://www.nowness.com/series/nowness-picks/jean-luc-godard-supercut',
82         'md5': '9a5a6a8edf806407e411296ab6bc2a49',
83         'info_dict': {
84             'id': '130020913',
85             'ext': 'mp4',
86             'title': 'Bleu, Blanc, Rouge - A Godard Supercut',
87             'description': 'md5:f0ea5f1857dffca02dbd37875d742cec',
88             'thumbnail': 're:^https?://.*\.jpg',
89             'upload_date': '20150607',
90             'uploader': 'Cinema Sem Lei',
91             'uploader_id': 'cinemasemlei',
92         },
93     }]
94
95     def _real_extract(self, url):
96         _, post = self._api_request(url, 'post/getBySlug/%s')
97         return self._extract_url_result(post)
98
99
100 class NownessPlaylistIE(NownessBaseIE):
101     IE_NAME = 'nowness:playlist'
102     _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)'
103     _TEST = {
104         'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
105         'info_dict': {
106             'id': '3286',
107         },
108         'playlist_mincount': 8,
109     }
110
111     def _real_extract(self, url):
112         playlist_id, playlist = self._api_request(url, 'post?PlaylistId=%s')
113         entries = [self._extract_url_result(item) for item in playlist['items']]
114         return self.playlist_result(entries, playlist_id)
115
116
117 class NownessSeriesIE(NownessBaseIE):
118     IE_NAME = 'nowness:series'
119     _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[^/]+?)(?:$|[?#])'
120     _TEST = {
121         'url': 'https://www.nowness.com/series/60-seconds',
122         'info_dict': {
123             'id': '60',
124             'title': '60 Seconds',
125             'description': 'One-minute wisdom in a new NOWNESS series',
126         },
127         'playlist_mincount': 4,
128     }
129
130     def _real_extract(self, url):
131         display_id, series = self._api_request(url, 'series/getBySlug/%s')
132         entries = [self._extract_url_result(post) for post in series['posts']]
133         series_title = None
134         series_description = None
135         translations = series.get('translations', [])
136         if translations:
137             series_title = translations[0].get('title') or translations[0]['seoTitle']
138             series_description = translations[0].get('seoDescription')
139         return self.playlist_result(
140             entries, compat_str(series['id']), series_title, series_description)