From 4b87b7f6bd3b2ef200c53ab5ce1390fec6792204 Mon Sep 17 00:00:00 2001 From: grqx_wsl <173253225+grqx@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:59:37 +1300 Subject: [PATCH] code review --- yt_dlp/extractor/nzonscreen.py | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/yt_dlp/extractor/nzonscreen.py b/yt_dlp/extractor/nzonscreen.py index 74edcd1b2e..e26a43e1c2 100644 --- a/yt_dlp/extractor/nzonscreen.py +++ b/yt_dlp/extractor/nzonscreen.py @@ -95,7 +95,6 @@ class NZOnScreenIE(InfoExtractor): 'description': 'Part one of four from this full length documentary.', 'display_id': 'reluctant-hero-2008', 'duration': 1108.0, - 'alt_title': 'Reluctant Hero', 'thumbnail': r're:https://www\.nzonscreen\.com/content/images/.+\.jpg', }, 'params': {'noplaylist': True}, @@ -122,36 +121,41 @@ class NZOnScreenIE(InfoExtractor): })) return formats - def _real_extract(self, url): - video_id = self._match_id(url) - webpage = self._download_webpage(url, video_id) - title = strip_or_none(( - self._html_extract_title(webpage, default=None) - or self._og_search_title(webpage)).rsplit('|', 2)[0]) - playlist = self._download_json( - f'https://www.nzonscreen.com/html5/video_data/{video_id}', video_id, - 'Downloading media data') - - if not self._yes_playlist(video_id, video_id): - del playlist[1:] - - return self.playlist_result([{ - 'alt_title': title if len(playlist) == 1 else None, + def _extract_from_api_resp(self, vid_info, is_single_vid, title, video_id): + return { + 'alt_title': title if is_single_vid else None, 'display_id': video_id, 'http_headers': { 'Referer': 'https://www.nzonscreen.com/', 'Origin': 'https://www.nzonscreen.com/', }, 'subtitles': {'en': [{ - 'url': traverse_obj(playinfo, ('h264', 'caption_url', {urljoin('https://www.nzonscreen.com')})), + 'url': traverse_obj(vid_info, ('h264', 'caption_url', {urljoin('https://www.nzonscreen.com')})), 'ext': 'vtt', }]}, - 'formats': self._extract_formats(playinfo), - **traverse_obj(playinfo, { + 'formats': self._extract_formats(vid_info), + **traverse_obj(vid_info, { 'id': 'uuid', 'title': ('label', {strip_or_none}), 'description': ('description', {strip_or_none}), 'thumbnail': ('thumbnail', 'path'), 'duration': ('duration', {float_or_none}), }), - } for playinfo in playlist], video_id, title) + } + + def _real_extract(self, url): + video_id = self._match_id(url) + webpage = self._download_webpage(url, video_id) + title = strip_or_none(( + self._html_extract_title(webpage, default=None) + or self._og_search_title(webpage)).rsplit('|', 2)[0]) + playlist = self._download_json( + f'https://www.nzonscreen.com/html5/video_data/{video_id}', video_id, + 'Downloading media data') + + if self._yes_playlist(video_id, traverse_obj(playlist, (0, 'id'))): + return self.playlist_result( + [self._extract_from_api_resp(vid_info, len(playlist) == 1, title, video_id) for vid_info in playlist], + playlist_id=video_id, playlist_title=title) + + return self._extract_from_api_resp(playlist[0], len(playlist) == 1, title, video_id)