From f33f32f1591087743454cbaa6a6689299f1da1d4 Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 20:32:31 +0100 Subject: [PATCH] [nowness] add api abstration function adn extend _VALID_URL regex --- youtube_dl/extractor/nowness.py | 40 +++++++++++++-------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index 401d3b646..04d614c9c 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -30,10 +30,20 @@ class NownessBaseIE(InfoExtractor): elif source == '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): IE_NAME = 'nowness' - _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(story|series/[^/]+)/(?P[0-9a-z-]+)' + _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(?:story|(?:series|category)/[^/]+)/(?P[^/]+?)(?:$|[?#])' _TESTS = [ { 'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation', @@ -62,19 +72,13 @@ class NownessIE(NownessBaseIE): ] def _real_extract(self, url): - display_id = self._match_id(url) - - 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) + display_id, post = self.api_request(url, 'http://api.nowness.com/api/post/getBySlug/%s') return self.extract_url_result(post) class NownessPlaylistIE(NownessBaseIE): IE_NAME = 'nowness:playlist' - _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P\d+)/[0-9a-z-]+' + _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P\d+)' _TEST = { 'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues', 'info_dict': @@ -85,20 +89,14 @@ class NownessPlaylistIE(NownessBaseIE): } def _real_extract(self, url): - playlist_id = self._match_id(url) - - 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) + playlist_id, playlist = self.api_request(url, 'http://api.nowness.com/api/post?PlaylistId=%s') entries = [self.extract_url_result(item) for item in playlist['items']] return self.playlist_result(entries, playlist_id) class NownessSerieIE(NownessBaseIE): IE_NAME = 'nowness:serie' - _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P[0-9a-z-]+)' + _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/series/(?P[^/]+?)(?:$|[?#])' _TEST = { 'url': 'https://www.nowness.com/series/60-seconds', 'info_dict': @@ -109,13 +107,7 @@ class NownessSerieIE(NownessBaseIE): } def _real_extract(self, url): - display_id = self._match_id(url) - - 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) + display_id, serie = self.api_request(url, 'http://api.nowness.com/api/series/getBySlug/%s') serie_id = str(serie['id']) entries = [self.extract_url_result(post) for post in serie['posts']] return self.playlist_result(entries, serie_id)