From acaf806c15f0a802ba286c23af02a10cf4bd4731 Mon Sep 17 00:00:00 2001 From: DmitryScaletta Date: Mon, 5 Feb 2024 05:17:39 +0300 Subject: [PATCH] [ie/nuum] Add extractors (#8868) Authored by: DmitryScaletta, seproDev Co-authored-by: sepro <4618135+seproDev@users.noreply.github.com> --- yt_dlp/extractor/_extractors.py | 10 +- yt_dlp/extractor/nuum.py | 199 ++++++++++++++++++++++++++++++++ yt_dlp/extractor/wasdtv.py | 159 ------------------------- 3 files changed, 204 insertions(+), 164 deletions(-) create mode 100644 yt_dlp/extractor/nuum.py delete mode 100644 yt_dlp/extractor/wasdtv.py diff --git a/yt_dlp/extractor/_extractors.py b/yt_dlp/extractor/_extractors.py index 36335286c3a..e7dd34c77bd 100644 --- a/yt_dlp/extractor/_extractors.py +++ b/yt_dlp/extractor/_extractors.py @@ -1354,6 +1354,11 @@ NYTimesCookingIE, NYTimesCookingRecipeIE, ) +from .nuum import ( + NuumLiveIE, + NuumTabIE, + NuumMediaIE, +) from .nuvid import NuvidIE from .nzherald import NZHeraldIE from .nzonscreen import NZOnScreenIE @@ -2315,11 +2320,6 @@ WashingtonPostIE, WashingtonPostArticleIE, ) -from .wasdtv import ( - WASDTVStreamIE, - WASDTVRecordIE, - WASDTVClipIE, -) from .wat import WatIE from .wdr import ( WDRIE, diff --git a/yt_dlp/extractor/nuum.py b/yt_dlp/extractor/nuum.py new file mode 100644 index 00000000000..3db663ded0d --- /dev/null +++ b/yt_dlp/extractor/nuum.py @@ -0,0 +1,199 @@ +import functools + +from .common import InfoExtractor +from ..utils import ( + ExtractorError, + OnDemandPagedList, + UserNotLive, + filter_dict, + int_or_none, + parse_iso8601, + str_or_none, + url_or_none, +) +from ..utils.traversal import traverse_obj + + +class NuumBaseIE(InfoExtractor): + def _call_api(self, path, video_id, description, query={}): + response = self._download_json( + f'https://nuum.ru/api/v2/{path}', video_id, query=query, + note=f'Downloading {description} metadata', + errnote=f'Unable to download {description} metadata') + if error := response.get('error'): + raise ExtractorError(f'API returned error: {error!r}') + return response['result'] + + def _get_channel_info(self, channel_name): + return self._call_api( + 'broadcasts/public', video_id=channel_name, description='channel', + query={ + 'with_extra': 'true', + 'channel_name': channel_name, + 'with_deleted': 'true', + }) + + def _parse_video_data(self, container, extract_formats=True): + stream = traverse_obj(container, ('media_container_streams', 0, {dict})) or {} + media = traverse_obj(stream, ('stream_media', 0, {dict})) or {} + media_url = traverse_obj(media, ( + 'media_meta', ('media_archive_url', 'media_url'), {url_or_none}), get_all=False) + + video_id = str(container['media_container_id']) + is_live = media.get('media_status') == 'RUNNING' + + formats, subtitles = None, None + if extract_formats: + formats, subtitles = self._extract_m3u8_formats_and_subtitles( + media_url, video_id, 'mp4', live=is_live) + + return filter_dict({ + 'id': video_id, + 'is_live': is_live, + 'formats': formats, + 'subtitles': subtitles, + **traverse_obj(container, { + 'title': ('media_container_name', {str}), + 'description': ('media_container_description', {str}), + 'timestamp': ('created_at', {parse_iso8601}), + 'channel': ('media_container_channel', 'channel_name', {str}), + 'channel_id': ('media_container_channel', 'channel_id', {str_or_none}), + }), + **traverse_obj(stream, { + 'view_count': ('stream_total_viewers', {int_or_none}), + 'concurrent_view_count': ('stream_current_viewers', {int_or_none}), + }), + **traverse_obj(media, { + 'duration': ('media_duration', {int_or_none}), + 'thumbnail': ('media_meta', ('media_preview_archive_url', 'media_preview_url'), {url_or_none}), + }, get_all=False), + }) + + +class NuumMediaIE(NuumBaseIE): + IE_NAME = 'nuum:media' + _VALID_URL = r'https?://nuum\.ru/(?:streams|videos|clips)/(?P[\d]+)' + _TESTS = [{ + 'url': 'https://nuum.ru/streams/1592713-7-days-to-die', + 'only_matching': True, + }, { + 'url': 'https://nuum.ru/videos/1567547-toxi-hurtz', + 'md5': 'f1d9118a30403e32b702a204eb03aca3', + 'info_dict': { + 'id': '1567547', + 'ext': 'mp4', + 'title': 'Toxi$ - Hurtz', + 'description': '', + 'timestamp': 1702631651, + 'upload_date': '20231215', + 'thumbnail': r're:^https?://.+\.jpg', + 'view_count': int, + 'concurrent_view_count': int, + 'channel_id': '6911', + 'channel': 'toxis', + 'duration': 116, + }, + }, { + 'url': 'https://nuum.ru/clips/1552564-pro-misu', + 'md5': 'b248ae1565b1e55433188f11beeb0ca1', + 'info_dict': { + 'id': '1552564', + 'ext': 'mp4', + 'title': 'Про Мису 🙃', + 'timestamp': 1701971828, + 'upload_date': '20231207', + 'thumbnail': r're:^https?://.+\.jpg', + 'view_count': int, + 'concurrent_view_count': int, + 'channel_id': '3320', + 'channel': 'Misalelik', + 'duration': 41, + }, + }] + + def _real_extract(self, url): + video_id = self._match_id(url) + video_data = self._call_api(f'media-containers/{video_id}', video_id, 'media') + + return self._parse_video_data(video_data) + + +class NuumLiveIE(NuumBaseIE): + IE_NAME = 'nuum:live' + _VALID_URL = r'https?://nuum\.ru/channel/(?P[^/#?]+)/?(?:$|[#?])' + _TESTS = [{ + 'url': 'https://nuum.ru/channel/mts_live', + 'only_matching': True, + }] + + def _real_extract(self, url): + channel = self._match_id(url) + channel_info = self._get_channel_info(channel) + if traverse_obj(channel_info, ('channel', 'channel_is_live')) is False: + raise UserNotLive(video_id=channel) + + info = self._parse_video_data(channel_info['media_container']) + return { + 'webpage_url': f'https://nuum.ru/streams/{info["id"]}', + 'extractor_key': NuumMediaIE.ie_key(), + 'extractor': NuumMediaIE.IE_NAME, + **info, + } + + +class NuumTabIE(NuumBaseIE): + IE_NAME = 'nuum:tab' + _VALID_URL = r'https?://nuum\.ru/channel/(?P[^/#?]+)/(?Pstreams|videos|clips)' + _TESTS = [{ + 'url': 'https://nuum.ru/channel/dankon_/clips', + 'info_dict': { + 'id': 'dankon__clips', + 'title': 'Dankon_', + }, + 'playlist_mincount': 29, + }, { + 'url': 'https://nuum.ru/channel/dankon_/videos', + 'info_dict': { + 'id': 'dankon__videos', + 'title': 'Dankon_', + }, + 'playlist_mincount': 2, + }, { + 'url': 'https://nuum.ru/channel/dankon_/streams', + 'info_dict': { + 'id': 'dankon__streams', + 'title': 'Dankon_', + }, + 'playlist_mincount': 1, + }] + + _PAGE_SIZE = 50 + + def _fetch_page(self, channel_id, tab_type, tab_id, page): + CONTAINER_TYPES = { + 'clips': ['SHORT_VIDEO', 'REVIEW_VIDEO'], + 'videos': ['LONG_VIDEO'], + 'streams': ['SINGLE'], + } + + media_containers = self._call_api( + 'media-containers', video_id=tab_id, description=f'{tab_type} tab page {page + 1}', + query={ + 'limit': self._PAGE_SIZE, + 'offset': page * self._PAGE_SIZE, + 'channel_id': channel_id, + 'media_container_status': 'STOPPED', + 'media_container_type': CONTAINER_TYPES[tab_type], + }) + for container in traverse_obj(media_containers, (..., {dict})): + metadata = self._parse_video_data(container, extract_formats=False) + yield self.url_result(f'https://nuum.ru/videos/{metadata["id"]}', NuumMediaIE, **metadata) + + def _real_extract(self, url): + channel_name, tab_type = self._match_valid_url(url).group('id', 'type') + tab_id = f'{channel_name}_{tab_type}' + channel_data = self._get_channel_info(channel_name)['channel'] + + return self.playlist_result(OnDemandPagedList(functools.partial( + self._fetch_page, channel_data['channel_id'], tab_type, tab_id), self._PAGE_SIZE), + playlist_id=tab_id, playlist_title=channel_data.get('channel_name')) diff --git a/yt_dlp/extractor/wasdtv.py b/yt_dlp/extractor/wasdtv.py deleted file mode 100644 index f57c619b5f8..00000000000 --- a/yt_dlp/extractor/wasdtv.py +++ /dev/null @@ -1,159 +0,0 @@ -from .common import InfoExtractor -from ..utils import ( - ExtractorError, - int_or_none, - parse_iso8601, - traverse_obj, - try_get, -) - - -class WASDTVBaseIE(InfoExtractor): - - def _fetch(self, path, video_id, description, query={}): - response = self._download_json( - f'https://wasd.tv/api/{path}', video_id, query=query, - note=f'Downloading {description} metadata', - errnote=f'Unable to download {description} metadata') - error = response.get('error') - if error: - raise ExtractorError(f'{self.IE_NAME} returned error: {error}', expected=True) - return response.get('result') - - def _extract_thumbnails(self, thumbnails_dict): - return [{ - 'url': url, - 'preference': index, - } for index, url in enumerate( - traverse_obj(thumbnails_dict, (('small', 'medium', 'large'),))) if url] - - def _real_extract(self, url): - container = self._get_container(url) - stream = traverse_obj(container, ('media_container_streams', 0)) - media = try_get(stream, lambda x: x['stream_media'][0]) - if not media: - raise ExtractorError('Can not extract media data.', expected=True) - media_meta = media.get('media_meta') - media_url, is_live = self._get_media_url(media_meta) - video_id = media.get('media_id') or container.get('media_container_id') - formats, subtitles = self._extract_m3u8_formats_and_subtitles(media_url, video_id, 'mp4') - return { - 'id': str(video_id), - 'title': container.get('media_container_name') or self._og_search_title(self._download_webpage(url, video_id)), - 'description': container.get('media_container_description'), - 'thumbnails': self._extract_thumbnails(media_meta.get('media_preview_images')), - 'timestamp': parse_iso8601(container.get('created_at')), - 'view_count': int_or_none(stream.get('stream_current_viewers' if is_live else 'stream_total_viewers')), - 'is_live': is_live, - 'formats': formats, - 'subtitles': subtitles, - } - - def _get_container(self, url): - raise NotImplementedError('Subclass for get media container') - - def _get_media_url(self, media_meta): - raise NotImplementedError('Subclass for get media url') - - -class WASDTVStreamIE(WASDTVBaseIE): - IE_NAME = 'wasdtv:stream' - _VALID_URL = r'https?://wasd\.tv/(?P[^/#?]+)$' - _TESTS = [{ - 'url': 'https://wasd.tv/24_7', - 'info_dict': { - 'id': '559738', - 'ext': 'mp4', - 'title': 'Live 24/7 Music', - 'description': '24/7 Music', - 'timestamp': int, - 'upload_date': r're:^\d{8}$', - 'is_live': True, - 'view_count': int, - }, - }] - - def _get_container(self, url): - nickname = self._match_id(url) - channel = self._fetch(f'channels/nicknames/{nickname}', video_id=nickname, description='channel') - channel_id = channel.get('channel_id') - containers = self._fetch( - 'v2/media-containers', channel_id, 'running media containers', - query={ - 'channel_id': channel_id, - 'media_container_type': 'SINGLE', - 'media_container_status': 'RUNNING', - }) - if not containers: - raise ExtractorError(f'{nickname} is offline', expected=True) - return containers[0] - - def _get_media_url(self, media_meta): - return media_meta['media_url'], True - - -class WASDTVRecordIE(WASDTVBaseIE): - IE_NAME = 'wasdtv:record' - _VALID_URL = r'https?://wasd\.tv/[^/#?]+(?:/videos)?\?record=(?P\d+)$' - _TESTS = [{ - 'url': 'https://wasd.tv/spacemita/videos?record=907755', - 'md5': 'c9899dd85be4cc997816ff9f9ca516ce', - 'info_dict': { - 'id': '906825', - 'ext': 'mp4', - 'title': 'Музыкальный', - 'description': 'md5:f510388d929ff60ae61d4c3cab3137cc', - 'timestamp': 1645812079, - 'upload_date': '20220225', - 'thumbnail': r're:^https?://.+\.jpg', - 'is_live': False, - 'view_count': int, - }, - }, { - 'url': 'https://wasd.tv/spacemita?record=907755', - 'only_matching': True, - }] - - def _get_container(self, url): - container_id = self._match_id(url) - return self._fetch( - f'v2/media-containers/{container_id}', container_id, 'media container') - - def _get_media_url(self, media_meta): - media_archive_url = media_meta.get('media_archive_url') - if media_archive_url: - return media_archive_url, False - return media_meta['media_url'], True - - -class WASDTVClipIE(WASDTVBaseIE): - IE_NAME = 'wasdtv:clip' - _VALID_URL = r'https?://wasd\.tv/[^/#?]+/clips\?clip=(?P\d+)$' - _TESTS = [{ - 'url': 'https://wasd.tv/spacemita/clips?clip=26804', - 'md5': '818885e720143d7a4e776ff66fcff148', - 'info_dict': { - 'id': '26804', - 'ext': 'mp4', - 'title': 'Пуш флексит на голове стримера', - 'timestamp': 1646682908, - 'upload_date': '20220307', - 'thumbnail': r're:^https?://.+\.jpg', - 'view_count': int, - }, - }] - - def _real_extract(self, url): - clip_id = self._match_id(url) - clip = self._fetch(f'v2/clips/{clip_id}', video_id=clip_id, description='clip') - clip_data = clip.get('clip_data') - formats, subtitles = self._extract_m3u8_formats_and_subtitles(clip_data.get('url'), video_id=clip_id, ext='mp4') - return { - 'id': clip_id, - 'title': clip.get('clip_title') or self._og_search_title(self._download_webpage(url, clip_id, fatal=False)), - 'thumbnails': self._extract_thumbnails(clip_data.get('preview')), - 'timestamp': parse_iso8601(clip.get('created_at')), - 'view_count': int_or_none(clip.get('clip_views_count')), - 'formats': formats, - 'subtitles': subtitles, - }