[nowness] add api abstration function adn extend _VALID_URL regex

pull/6768/head
remitamine 9 years ago
parent 75b399f455
commit f33f32f159

@ -30,10 +30,20 @@ class NownessBaseIE(InfoExtractor):
elif source == 'cinematique': elif source == 'cinematique':
return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique') return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique')
def api_request(self, url, request_url):
id = self._match_id(url)
lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
request = compat_urllib_request.Request(request_url % id, headers={
'X-Nowness-Language': lang,
})
json_data = self._download_json(request, id)
return id, json_data
class NownessIE(NownessBaseIE): class NownessIE(NownessBaseIE):
IE_NAME = 'nowness' IE_NAME = 'nowness'
_VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(story|series/[^/]+)/(?P<id>[0-9a-z-]+)' _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P<id>[^/]+?)(?:$|[?#])'
_TESTS = [ _TESTS = [
{ {
'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation', 'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation',
@ -62,19 +72,13 @@ class NownessIE(NownessBaseIE):
] ]
def _real_extract(self, url): def _real_extract(self, url):
display_id = self._match_id(url) display_id, post = self.api_request(url, 'http://api.nowness.com/api/post/getBySlug/%s')
lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
request = compat_urllib_request.Request('http://api.nowness.com/api/post/getBySlug/%s' % display_id, headers={
'X-Nowness-Language': lang,
})
post = self._download_json(request, display_id)
return self.extract_url_result(post) return self.extract_url_result(post)
class NownessPlaylistIE(NownessBaseIE): class NownessPlaylistIE(NownessBaseIE):
IE_NAME = 'nowness:playlist' IE_NAME = 'nowness:playlist'
_VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)/[0-9a-z-]+' _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P<id>\d+)'
_TEST = { _TEST = {
'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues', 'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues',
'info_dict': 'info_dict':
@ -85,20 +89,14 @@ class NownessPlaylistIE(NownessBaseIE):
} }
def _real_extract(self, url): def _real_extract(self, url):
playlist_id = self._match_id(url) playlist_id, playlist = self.api_request(url, 'http://api.nowness.com/api/post?PlaylistId=%s')
lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
request = compat_urllib_request.Request('http://api.nowness.com/api/post?PlaylistId=%s' % playlist_id, headers={
'X-Nowness-Language': lang,
})
playlist = self._download_json(request, playlist_id)
entries = [self.extract_url_result(item) for item in playlist['items']] entries = [self.extract_url_result(item) for item in playlist['items']]
return self.playlist_result(entries, playlist_id) return self.playlist_result(entries, playlist_id)
class NownessSerieIE(NownessBaseIE): class NownessSerieIE(NownessBaseIE):
IE_NAME = 'nowness:serie' IE_NAME = 'nowness:serie'
_VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[0-9a-z-]+)' _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P<id>[^/]+?)(?:$|[?#])'
_TEST = { _TEST = {
'url': 'https://www.nowness.com/series/60-seconds', 'url': 'https://www.nowness.com/series/60-seconds',
'info_dict': 'info_dict':
@ -109,13 +107,7 @@ class NownessSerieIE(NownessBaseIE):
} }
def _real_extract(self, url): def _real_extract(self, url):
display_id = self._match_id(url) display_id, serie = self.api_request(url, 'http://api.nowness.com/api/series/getBySlug/%s')
lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us'
request = compat_urllib_request.Request('http://api.nowness.com/api/series/getBySlug/%s' % display_id, headers={
'X-Nowness-Language': lang,
})
serie = self._download_json(request, display_id)
serie_id = str(serie['id']) serie_id = str(serie['id'])
entries = [self.extract_url_result(post) for post in serie['posts']] entries = [self.extract_url_result(post) for post in serie['posts']]
return self.playlist_result(entries, serie_id) return self.playlist_result(entries, serie_id)

Loading…
Cancel
Save