From 5997ce694a52cb590b23223090d432ce2ef83641 Mon Sep 17 00:00:00 2001 From: dirkf Date: Fri, 6 Aug 2021 16:50:33 +0100 Subject: [PATCH 1/3] Add playlist extraction for Comedy Central --- youtube_dl/extractor/comedycentral.py | 117 +++++++++++++++++++++++++- youtube_dl/extractor/extractors.py | 2 + 2 files changed, 117 insertions(+), 2 deletions(-) diff --git a/youtube_dl/extractor/comedycentral.py b/youtube_dl/extractor/comedycentral.py index 1bfa912be..bad0f737c 100644 --- a/youtube_dl/extractor/comedycentral.py +++ b/youtube_dl/extractor/comedycentral.py @@ -1,10 +1,17 @@ from __future__ import unicode_literals +import re +from ..compat import ( + compat_str, + compat_urlparse, +) +from ..utils import urljoin + from .mtv import MTVServicesInfoExtractor class ComedyCentralIE(MTVServicesInfoExtractor): - _VALID_URL = r'https?://(?:www\.)?cc\.com/(?:episodes|video(?:-clips)?)/(?P[0-9a-z]{6})' + _VALID_URL = r'https?://(?:www\.)?cc\.com/(?:episodes|video(?:-clips)?|collection-playlist/[0-9a-z]+/[^/]+)/(?P[0-9a-z]{6})' _FEED_URL = 'http://comedycentral.com/feeds/mrss/' _TESTS = [{ @@ -24,7 +31,17 @@ class ComedyCentralIE(MTVServicesInfoExtractor): }, { 'url': 'https://www.cc.com/video/k3sdvm/the-daily-show-with-jon-stewart-exclusive-the-fourth-estate', 'only_matching': True, - }] + }, { + 'url': 'https://www.cc.com/collection-playlist/8b7hw5/the-daily-shows-summer-exclusives/o2qny3', + 'info_dict': { + 'id': '2f56e756-91ec-4d68-8799-e3b710e360e4', + 'ext': 'mp4', + 'title': 'The Daily Show with Trevor Noah|August 4, 2021|26|NO-EPISODE#|Hottest Take - The Olympics', + 'description': 'md5:f877effd54484878f0aaddbe418314f4', + 'timestamp': 1628125200, + 'upload_date': '20210805', + }, + }, ] class ComedyCentralTVIE(MTVServicesInfoExtractor): @@ -49,3 +66,99 @@ class ComedyCentralTVIE(MTVServicesInfoExtractor): 'imageEp': 'web.cc.tv', 'mgid': uri, } + + +class ComedyCentralCollectionIE(ComedyCentralIE): + _VALID_URL = r'https?://(?:www\.)?cc\.com/collections/(?P[0-9a-z]{6})(?:/[^/]*)?' + _TESTS = [{ + 'url': 'https://www.cc.com/collections/8b7hw5/the-daily-show-s-summer-exclusives', + 'info_dict': { + 'id': '8b7hw5', + 'title': "The Daily Show's Summer Exclusives", + 'description': 'md5:ae65028fcf8438e65f1c98099119fe6d', + }, + 'playlist_mincount': 11, + }, ] + _CLIP_IE = 'ComedyCentral' + + # we need to know the redirected URL, so stash it in the extractor + # strange that this hasn't been a general requirement + def _request_webpage(self, url_or_request, video_id, + note=None, errnote=None, fatal=True, data=None, + headers={}, query={}, expected_status=None): + urlh = super(ComedyCentralCollectionIE, self)._request_webpage( + url_or_request, video_id, note, errnote, fatal, + data, headers, query, expected_status) + if urlh is not False: + self._url = urlh.geturl() + elif isinstance(url_or_request, compat_str): + self._url = url_or_request + else: + self._url = url_or_request.get_full_url() + return urlh + + def _get_clip_id(self, url): + return None + + def _real_extract(self, url): + playlist_id = self._match_id(url) + webpage = self._download_webpage(url, playlist_id) + url = self._url + clip_id = self._get_clip_id(url) + if clip_id: + url = url.rstrip(clip_id) + else: + url = url.replace('/collections/', '/collection-playlist/') + path = compat_urlparse.urlparse(url).path + if path.endswith('/'): + path = path[:-1] + clip_urls = re.finditer( + r']*?href\s*=\s*"(?P%s/(?P[0-9a-z]{6}))"[^<]*>' % path, + webpage) + playlist_title = (self._html_search_meta('twitter:title', webpage, 'title') + or self._og_search_title(webpage, display_name='title') + or self._html_search_regex(r']*>([^<]+)[0-9a-z]{6})(?:/(?:[^/]+(?:/(?P[0-9a-z]{6})?)?)?)?' + _TESTS = [{ + 'url': 'https://www.cc.com/playlists/mym6e5/corporate-working-nine-to-five', + 'info_dict': { + 'id': 'mym6e5', + 'title': 'Coffee Break - Corporate | Comedy Central US', + 'description': 'md5:89bc6e8e983ad34ded85af84108f6ea8', + }, + 'playlist_mincount': 5, + }, { + 'url': 'https://www.cc.com/playlists/mym6e5/corporate-working-nine-to-five/dm8ekc', + 'only_matching': True, + }, ] + + def _get_clip_id(self, url): + mobj = re.match(self._VALID_URL, url) + return mobj.group('clip_id') + + def _real_initialize(self): + self._CLIP_IE = self.IE_NAME + + def _real_extract(self, url): + if self._get_clip_id(url): + return ComedyCentralIE._real_extract(self, url) + playlist_id = self._match_id(url) + if self._downloader.params.get('noplaylist', False): + _ = self._download_webpage(url, playlist_id) + if self._get_clip_id(self._url): + self.to_screen( + 'Downloading just the "now playing" clip because of --no-playlist') + return self.url_result(self._url, self._CLIP_IE, playlist_id) + return super(ComedyCentralPlaylistIE, self)._real_extract(url) diff --git a/youtube_dl/extractor/extractors.py b/youtube_dl/extractor/extractors.py index 6e8fc3961..248f5f779 100644 --- a/youtube_dl/extractor/extractors.py +++ b/youtube_dl/extractor/extractors.py @@ -244,6 +244,8 @@ from .coub import CoubIE from .comedycentral import ( ComedyCentralIE, ComedyCentralTVIE, + ComedyCentralCollectionIE, + ComedyCentralPlaylistIE, ) from .commonmistakes import CommonMistakesIE, UnicodeBOMIE from .commonprotocols import ( From a15f994677c191338b151732bb2e267faeb26ff0 Mon Sep 17 00:00:00 2001 From: dirkf Date: Fri, 6 Aug 2021 17:28:59 +0100 Subject: [PATCH 2/3] Extraction tweaks for Comedy Central playlists Return a single item as a video result; retain the 6-char ID as display_id --- youtube_dl/extractor/mtv.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/youtube_dl/extractor/mtv.py b/youtube_dl/extractor/mtv.py index 5a5205c0e..965145b38 100644 --- a/youtube_dl/extractor/mtv.py +++ b/youtube_dl/extractor/mtv.py @@ -229,6 +229,8 @@ class MTVServicesInfoExtractor(InfoExtractor): if info: entries.append(info) + if len(entries) == 1: + return entries[0] return self.playlist_result( entries, playlist_title=title, playlist_description=description) @@ -299,6 +301,8 @@ class MTVServicesInfoExtractor(InfoExtractor): webpage = self._download_webpage(url, title) mgid = self._extract_mgid(webpage) videos_info = self._get_videos_info(mgid) + if videos_info and videos_info.get('_type') != 'playlist': + videos_info['display_id'] = title return videos_info From 08202a62a7251f2a471660bc83a1c2c8340ce52b Mon Sep 17 00:00:00 2001 From: df Date: Mon, 9 Aug 2021 16:24:12 +0100 Subject: [PATCH 3/3] Fix tests --- youtube_dl/extractor/comedycentral.py | 10 ++++++++-- youtube_dl/extractor/generic.py | 3 +++ youtube_dl/extractor/mtv.py | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/youtube_dl/extractor/comedycentral.py b/youtube_dl/extractor/comedycentral.py index bad0f737c..b2d26f523 100644 --- a/youtube_dl/extractor/comedycentral.py +++ b/youtube_dl/extractor/comedycentral.py @@ -16,7 +16,7 @@ class ComedyCentralIE(MTVServicesInfoExtractor): _TESTS = [{ 'url': 'http://www.cc.com/video-clips/5ke9v2/the-daily-show-with-trevor-noah-doc-rivers-and-steve-ballmer---the-nba-player-strike', - 'md5': 'b8acb347177c680ff18a292aa2166f80', + 'md5': '58e7caf5c7c0c865d9d79f7d151e5090', 'info_dict': { 'id': '89ccc86e-1b02-4f83-b0c9-1d9592ecd025', 'ext': 'mp4', @@ -25,6 +25,9 @@ class ComedyCentralIE(MTVServicesInfoExtractor): 'timestamp': 1598670000, 'upload_date': '20200829', }, + 'params': { + 'hls_prefer_native': False, + }, }, { 'url': 'http://www.cc.com/episodes/pnzzci/drawn-together--american-idol--parody-clip-show-season-3-ep-314', 'only_matching': True, @@ -37,10 +40,13 @@ class ComedyCentralIE(MTVServicesInfoExtractor): 'id': '2f56e756-91ec-4d68-8799-e3b710e360e4', 'ext': 'mp4', 'title': 'The Daily Show with Trevor Noah|August 4, 2021|26|NO-EPISODE#|Hottest Take - The Olympics', - 'description': 'md5:f877effd54484878f0aaddbe418314f4', + 'description': 'md5:104484314a4cba36d8c62b094523efc8', 'timestamp': 1628125200, 'upload_date': '20210805', }, + 'params': { + 'hls_prefer_native': False, + }, }, ] diff --git a/youtube_dl/extractor/generic.py b/youtube_dl/extractor/generic.py index a9c064105..d4b4883e6 100644 --- a/youtube_dl/extractor/generic.py +++ b/youtube_dl/extractor/generic.py @@ -901,6 +901,9 @@ class GenericIE(InfoExtractor): 'timestamp': 1349922600, 'upload_date': '20121011', }, + 'params': { + 'hls-prefer-native': False, + }, }, # YouTube embed via { diff --git a/youtube_dl/extractor/mtv.py b/youtube_dl/extractor/mtv.py index 965145b38..5008d33ae 100644 --- a/youtube_dl/extractor/mtv.py +++ b/youtube_dl/extractor/mtv.py @@ -292,7 +292,7 @@ class MTVServicesInfoExtractor(InfoExtractor): main_container = self._extract_child_with_type(data, 'MainContainer') ab_testing = self._extract_child_with_type(main_container, 'ABTesting') video_player = self._extract_child_with_type(ab_testing or main_container, 'VideoPlayer') - mgid = video_player['props']['media']['video']['config']['uri'] + mgid = try_get(video_player, lambda x: x['props']['media']['video']['config']['uri']) return mgid @@ -300,6 +300,8 @@ class MTVServicesInfoExtractor(InfoExtractor): title = url_basename(url) webpage = self._download_webpage(url, title) mgid = self._extract_mgid(webpage) + if not mgid: + raise ExtractorError('Unable to determine MTVServices ID (mgid)', expected=True) videos_info = self._get_videos_info(mgid) if videos_info and videos_info.get('_type') != 'playlist': videos_info['display_id'] = title