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