From c23c3d7d7d3ca9368b6fca70b696fe64ca55ba07 Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 16:38:05 +0100 Subject: [PATCH 1/6] [nowness] fix video extraction and add support serie and playlist extraction (fixes #6720) --- youtube_dl/extractor/__init__.py | 6 +- youtube_dl/extractor/nowness.py | 109 +++++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 27 deletions(-) diff --git a/youtube_dl/extractor/__init__.py b/youtube_dl/extractor/__init__.py index 5d2ea39d0..7408f68d6 100644 --- a/youtube_dl/extractor/__init__.py +++ b/youtube_dl/extractor/__init__.py @@ -403,7 +403,11 @@ from .normalboots import NormalbootsIE from .nosvideo import NosVideoIE from .nova import NovaIE from .novamov import NovaMovIE -from .nowness import NownessIE +from .nowness import ( + NownessIE, + NownessPlaylistIE, + NownessSerieIE, +) from .nowtv import NowTVIE from .nowvideo import NowVideoIE from .npo import ( diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index 6b2f3f55a..1acb9b92a 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -1,19 +1,42 @@ # encoding: utf-8 from __future__ import unicode_literals -import re - from .brightcove import BrightcoveIE from .common import InfoExtractor from ..utils import ExtractorError +from ..compat import compat_urllib_request -class NownessIE(InfoExtractor): - _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/[^?#]*?/(?P[0-9]+)/(?P[^/]+?)(?:$|[?#])' +class NownessBaseIE(InfoExtractor): + def extract_url_result(self, post): + if post['type'] == 'video': + for media in post['media']: + if media['type'] == 'video': + video_id = media['content'] + source = media['source'] + if source == 'brightcove': + player_code = self._download_webpage( + 'http://www.nowness.com/iframe?id=%s' % video_id, video_id, + note='Downloading player JavaScript', + errnote='Player download failed') + bc_url = BrightcoveIE._extract_brightcove_url(player_code) + if bc_url is None: + raise ExtractorError('Could not find player definition') + return self.url_result(bc_url, 'Brightcove') + elif source == 'vimeo': + return self.url_result('http://vimeo.com/%s' % video_id, 'Vimeo') + elif source == 'youtube': + return self.url_result(video_id, 'Youtube') + elif source == 'cinematique': + return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique') + +class NownessIE(NownessBaseIE): + IE_NAME = 'nowness' + _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/(story|series/[^/])/(?P[0-9a-z-]+)' _TESTS = [ { - 'url': 'http://www.nowness.com/day/2013/6/27/3131/candor--the-art-of-gesticulation', + 'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation', 'md5': '068bc0202558c2e391924cb8cc470676', 'info_dict': { 'id': '2520295746001', @@ -25,7 +48,7 @@ class NownessIE(InfoExtractor): } }, { - 'url': 'http://cn.nowness.com/day/2014/8/7/4069/kasper-bj-rke-ft-jaakko-eino-kalevi--tnr', + 'url': 'https://cn.nowness.com/story/kasper-bjorke-ft-jaakko-eino-kalevi-tnr', 'md5': 'e79cf125e387216f86b2e0a5b5c63aa3', 'info_dict': { 'id': '3716354522001', @@ -39,26 +62,60 @@ class NownessIE(InfoExtractor): ] def _real_extract(self, url): - mobj = re.match(self._VALID_URL, url) - video_id = mobj.group('slug') + display_id = self._match_id(url) - webpage = self._download_webpage(url, video_id) - player_url = self._search_regex( - r'"([^"]+/content/issue-[0-9.]+.js)"', webpage, 'player URL') - real_id = self._search_regex( - r'\sdata-videoId="([0-9]+)"', webpage, 'internal video ID') + 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) - player_code = self._download_webpage( - player_url, video_id, - note='Downloading player JavaScript', - errnote='Player download failed') - player_code = player_code.replace("'+d+'", real_id) - bc_url = BrightcoveIE._extract_brightcove_url(player_code) - if bc_url is None: - raise ExtractorError('Could not find player definition') - return { - '_type': 'url', - 'url': bc_url, - 'ie_key': 'Brightcove', - } +class NownessPlaylistIE(NownessBaseIE): + IE_NAME = 'nowness:playlist' + _VALID_URL = r'https?://(?:(?:www|cn)\.)?nowness\.com/playlist/(?P\d+)/[0-9a-z-]+' + _TEST = { + 'url': 'https://www.nowness.com/playlist/3286/i-guess-thats-why-they-call-it-the-blues', + 'info_dict': + { + 'id': '3286', + }, + 'playlist_mincount': 8, + } + + 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) + 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-]+)' + _TEST = { + 'url': 'https://www.nowness.com/series/60-seconds', + 'info_dict': + { + 'id': '60', + }, + 'playlist_mincount': 4, + } + + 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('https://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']) + entries = [self.extract_url_result(post) for post in serie['posts']] + return self.playlist_result(entries, serie_id) From 75b399f45557468e602c3b0126a9b5ce4736218f Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 16:53:17 +0100 Subject: [PATCH 2/6] [nowness] fix _VALID_URL regex --- youtube_dl/extractor/nowness.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index 1acb9b92a..401d3b646 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -33,7 +33,7 @@ class NownessBaseIE(InfoExtractor): 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/[^/]+)/(?P[0-9a-z-]+)' _TESTS = [ { 'url': 'https://www.nowness.com/story/candor-the-art-of-gesticulation', @@ -112,7 +112,7 @@ class NownessSerieIE(NownessBaseIE): display_id = self._match_id(url) lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us' - request = compat_urllib_request.Request('https://api.nowness.com/api/series/getBySlug/%s' % display_id, headers={ + 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) From f33f32f1591087743454cbaa6a6689299f1da1d4 Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 20:32:31 +0100 Subject: [PATCH 3/6] [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) From f95c5e1218aacc5cc4c01b19f5ee9f7baf9269d6 Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 20:51:27 +0100 Subject: [PATCH 4/6] [nowness] change id variable name --- youtube_dl/extractor/nowness.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index 04d614c9c..a8ee4fe68 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -31,14 +31,14 @@ class NownessBaseIE(InfoExtractor): return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique') def api_request(self, url, request_url): - id = self._match_id(url) + display_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={ + request = compat_urllib_request.Request(request_url % display_id, headers={ 'X-Nowness-Language': lang, }) - json_data = self._download_json(request, id) - return id, json_data + json_data = self._download_json(request, display_id) + return display_id, json_data class NownessIE(NownessBaseIE): From 673bf566fc1aa1351a40c7378893aff6cb0fc91b Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 21:03:13 +0100 Subject: [PATCH 5/6] [nowness] more api abstraction --- youtube_dl/extractor/nowness.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index a8ee4fe68..3371145c1 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -30,11 +30,11 @@ 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): + def api_request(self, url, request_path): display_id = self._match_id(url) lang = 'zh-cn' if 'cn.nowness.com' in url else 'en-us' - request = compat_urllib_request.Request(request_url % display_id, headers={ + request = compat_urllib_request.Request('http://api.nowness.com/api/' + request_path % display_id, headers={ 'X-Nowness-Language': lang, }) json_data = self._download_json(request, display_id) @@ -72,7 +72,7 @@ class NownessIE(NownessBaseIE): ] def _real_extract(self, url): - display_id, post = self.api_request(url, 'http://api.nowness.com/api/post/getBySlug/%s') + display_id, post = self.api_request(url, 'post/getBySlug/%s') return self.extract_url_result(post) @@ -89,7 +89,7 @@ class NownessPlaylistIE(NownessBaseIE): } def _real_extract(self, url): - playlist_id, playlist = self.api_request(url, 'http://api.nowness.com/api/post?PlaylistId=%s') + playlist_id, playlist = self.api_request(url, 'post?PlaylistId=%s') entries = [self.extract_url_result(item) for item in playlist['items']] return self.playlist_result(entries, playlist_id) @@ -107,7 +107,7 @@ class NownessSerieIE(NownessBaseIE): } def _real_extract(self, url): - display_id, serie = self.api_request(url, 'http://api.nowness.com/api/series/getBySlug/%s') + display_id, serie = self.api_request(url, '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) From f43c1631582267fba591d150a18bee764bb37708 Mon Sep 17 00:00:00 2001 From: remitamine Date: Sat, 5 Sep 2015 21:18:21 +0100 Subject: [PATCH 6/6] [nowness] pass cinematique --- youtube_dl/extractor/nowness.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/youtube_dl/extractor/nowness.py b/youtube_dl/extractor/nowness.py index 3371145c1..9a0e46917 100644 --- a/youtube_dl/extractor/nowness.py +++ b/youtube_dl/extractor/nowness.py @@ -28,7 +28,9 @@ class NownessBaseIE(InfoExtractor): elif source == 'youtube': return self.url_result(video_id, 'Youtube') elif source == 'cinematique': - return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique') + # youtube-dl currently doesn't support cinematique + # return self.url_result('http://cinematique.com/embed/%s' % video_id, 'Cinematique') + pass def api_request(self, url, request_path): display_id = self._match_id(url)