]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nowness.py
Rename compat_urllib_request_Request to sanitized_Request and move to utils
[yt-dlp.git] / youtube_dl / extractor / nowness.py
CommitLineData
a91cf277 1# encoding: utf-8
066f6a06
PH
2from __future__ import unicode_literals
3
4fcaa4f4 4from .brightcove import BrightcoveLegacyIE
066f6a06 5from .common import InfoExtractor
e3a6576f 6from ..utils import ExtractorError
82c18e2a
S
7from ..compat import (
8 compat_str,
9 compat_urllib_request,
10)
c23c3d7d 11
066f6a06 12
c23c3d7d 13class NownessBaseIE(InfoExtractor):
82c18e2a 14 def _extract_url_result(self, post):
c23c3d7d 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',
82c18e2a 24 errnote='Unable to download player JavaScript')
4fcaa4f4 25 bc_url = BrightcoveLegacyIE._extract_brightcove_url(player_code)
c23c3d7d 26 if bc_url is None:
27 raise ExtractorError('Could not find player definition')
3b7d9aa4 28 return self.url_result(bc_url, 'BrightcoveLegacy')
c23c3d7d 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':
f43c1631 34 # youtube-dl currently doesn't support cinematique
35 # return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')
36 pass
066f6a06 37
82c18e2a 38 def _api_request(self, url, request_path):
f95c5e12 39 display_id = self._match_id(url)
82c18e2a
S
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)
f33f32f1 46
a91cf277 47
c23c3d7d 48class NownessIE(NownessBaseIE):
49 IE_NAME = 'nowness'
f33f32f1 50 _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P<id>[^/]+?)(?:$|[?#])'
82c18e2a
S
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',
c2467735 61 },
82c18e2a
S
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',
c2467735
S
72 },
73 }, {
74 # vimeo
75 'url': 'https://www.nowness.com/series/nowness-picks/jean-luc-godard-supercut',
76 'md5': '9a5a6a8edf806407e411296ab6bc2a49',
77 'info_dict': {
78 'id': '130020913',
79 'ext': 'mp4',
80 'title': 'Bleu, Blanc, Rouge - A Godard Supercut',
81 'description': 'md5:f0ea5f1857dffca02dbd37875d742cec',
82 'thumbnail': 're:^https?://.*\.jpg',
83 'upload_date': '20150607',
84 'uploader': 'Cinema Sem Lei',
85 'uploader_id': 'cinemasemlei',
86 },
82c18e2a 87 }]
066f6a06
PH
88
89 def _real_extract(self, url):
82c18e2a
S
90 _, post = self._api_request(url, 'post/getBySlug/%s')
91 return self._extract_url_result(post)
c23c3d7d 92
93
94class NownessPlaylistIE(NownessBaseIE):
95 IE_NAME = 'nowness:playlist'
f33f32f1 96 _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)'
c23c3d7d 97 _TEST = {
98 'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
82c18e2a 99 'info_dict': {
c23c3d7d 100 'id': '3286',
101 },
102 'playlist_mincount': 8,
103 }
104
105 def _real_extract(self, url):
82c18e2a
S
106 playlist_id, playlist = self._api_request(url, 'post?PlaylistId=%s')
107 entries = [self._extract_url_result(item) for item in playlist['items']]
c23c3d7d 108 return self.playlist_result(entries, playlist_id)
066f6a06 109
066f6a06 110
82c18e2a
S
111class NownessSeriesIE(NownessBaseIE):
112 IE_NAME = 'nowness:series'
f33f32f1 113 _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[^/]+?)(?:$|[?#])'
c23c3d7d 114 _TEST = {
115 'url': 'https://www.nowness.com/series/60-seconds',
82c18e2a 116 'info_dict': {
c23c3d7d 117 'id': '60',
82c18e2a
S
118 'title': '60 Seconds',
119 'description': 'One-minute wisdom in a new NOWNESS series',
c23c3d7d 120 },
121 'playlist_mincount': 4,
122 }
123
124 def _real_extract(self, url):
82c18e2a
S
125 display_id, series = self._api_request(url, 'series/getBySlug/%s')
126 entries = [self._extract_url_result(post) for post in series['posts']]
127 series_title = None
128 series_description = None
129 translations = series.get('translations', [])
130 if translations:
131 series_title = translations[0].get('title') or translations[0]['seoTitle']
132 series_description = translations[0].get('seoDescription')
133 return self.playlist_result(
134 entries, compat_str(series['id']), series_title, series_description)