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