Compare commits

..

2 Commits

Author SHA1 Message Date
Dmitry Meyer
9c2c5ae80d Fix tests after refactoring 2024-02-08 09:49:43 +00:00
Dmitry Meyer
daf8035778
Refactor extractor
applying suggestions from code review

Co-authored-by: sepro <4618135+seproDev@users.noreply.github.com>
2024-02-08 12:24:44 +03:00

View File

@ -19,8 +19,11 @@ class BoostyIE(InfoExtractor):
'id': 'd7473824-352e-48e2-ae53-d4aa39459968', 'id': 'd7473824-352e-48e2-ae53-d4aa39459968',
'title': 'phasma_3', 'title': 'phasma_3',
'channel': 'Kuplinov', 'channel': 'Kuplinov',
'timestamp': 1655049000, 'channel_id': '7958701',
'timestamp': 1655031975,
'upload_date': '20220612', 'upload_date': '20220612',
'release_timestamp': 1655049000,
'release_date': '20220612',
'modified_timestamp': 1668680993, 'modified_timestamp': 1668680993,
'modified_date': '20221117', 'modified_date': '20221117',
'tags': ['куплинов', 'phasmophobia'], 'tags': ['куплинов', 'phasmophobia'],
@ -37,11 +40,13 @@ class BoostyIE(InfoExtractor):
'id': '0c652798-3b35-471f-8b48-a76a0b28736f', 'id': '0c652798-3b35-471f-8b48-a76a0b28736f',
'title': 'то что не пропустил юта6', 'title': 'то что не пропустил юта6',
'channel': 'Илья Давыдов', 'channel': 'Илья Давыдов',
'channel_id': '6808257',
'timestamp': 1694017040, 'timestamp': 1694017040,
'upload_date': '20230906', 'upload_date': '20230906',
'release_timestamp': 1694017040,
'release_date': '20230906',
'modified_timestamp': 1694071178, 'modified_timestamp': 1694071178,
'modified_date': '20230907', 'modified_date': '20230907',
'tags': [],
'like_count': int, 'like_count': int,
}, },
'playlist_count': 3, 'playlist_count': 3,
@ -49,6 +54,15 @@ class BoostyIE(InfoExtractor):
'info_dict': { 'info_dict': {
'id': 'cc325a9f-a563-41c6-bf47-516c1b506c9a', 'id': 'cc325a9f-a563-41c6-bf47-516c1b506c9a',
'title': 'то что не пропустил юта6', 'title': 'то что не пропустил юта6',
'channel': 'Илья Давыдов',
'channel_id': '6808257',
'timestamp': 1694017040,
'upload_date': '20230906',
'release_timestamp': 1694017040,
'release_date': '20230906',
'modified_timestamp': 1694071178,
'modified_date': '20230907',
'like_count': int,
'ext': 'mp4', 'ext': 'mp4',
'duration': 31204, 'duration': 31204,
'view_count': int, 'view_count': int,
@ -58,6 +72,15 @@ class BoostyIE(InfoExtractor):
'info_dict': { 'info_dict': {
'id': 'd07b0a72-9493-4512-b54e-55ce468fd4b7', 'id': 'd07b0a72-9493-4512-b54e-55ce468fd4b7',
'title': 'то что не пропустил юта6', 'title': 'то что не пропустил юта6',
'channel': 'Илья Давыдов',
'channel_id': '6808257',
'timestamp': 1694017040,
'upload_date': '20230906',
'release_timestamp': 1694017040,
'release_date': '20230906',
'modified_timestamp': 1694071178,
'modified_date': '20230907',
'like_count': int,
'ext': 'mp4', 'ext': 'mp4',
'duration': 25704, 'duration': 25704,
'view_count': int, 'view_count': int,
@ -67,6 +90,15 @@ class BoostyIE(InfoExtractor):
'info_dict': { 'info_dict': {
'id': '4a3bba32-78c8-422a-9432-2791aff60b42', 'id': '4a3bba32-78c8-422a-9432-2791aff60b42',
'title': 'то что не пропустил юта6', 'title': 'то что не пропустил юта6',
'channel': 'Илья Давыдов',
'channel_id': '6808257',
'timestamp': 1694017040,
'upload_date': '20230906',
'release_timestamp': 1694017040,
'release_date': '20230906',
'modified_timestamp': 1694071178,
'modified_date': '20230907',
'like_count': int,
'ext': 'mp4', 'ext': 'mp4',
'duration': 31867, 'duration': 31867,
'view_count': int, 'view_count': int,
@ -78,19 +110,15 @@ class BoostyIE(InfoExtractor):
'url': 'https://boosty.to/denischuzhoy/posts/6094a487-bcec-4cf8-a453-43313b463c38', 'url': 'https://boosty.to/denischuzhoy/posts/6094a487-bcec-4cf8-a453-43313b463c38',
'info_dict': { 'info_dict': {
'id': 'EXelTnve5lY', 'id': 'EXelTnve5lY',
'title': '4Класс', 'title': 'Послание Президента Федеральному Собранию | Класс народа',
'channel': 'Денис Чужой',
'timestamp': 1619380873,
'upload_date': '20210425', 'upload_date': '20210425',
'modified_timestamp': 1653321155, 'channel': 'Денис Чужой',
'modified_date': '20220523', 'tags': 'count:10',
'tags': [],
'like_count': int, 'like_count': int,
'ext': 'mp4', 'ext': 'mp4',
'duration': 816, 'duration': 816,
'view_count': int, 'view_count': int,
'thumbnail': r're:^https://i\.ytimg\.com/', 'thumbnail': r're:^https://i\.ytimg\.com/',
# youtube fields
'age_limit': 0, 'age_limit': 0,
'availability': 'public', 'availability': 'public',
'categories': list, 'categories': list,
@ -115,105 +143,67 @@ class BoostyIE(InfoExtractor):
f'https://api.boosty.to/v1/blog/{user}/post/{post_id}', post_id, f'https://api.boosty.to/v1/blog/{user}/post/{post_id}', post_id,
note='Downloading post data', errnote='Unable to download post data') note='Downloading post data', errnote='Unable to download post data')
post_title = self._extract_title(post, post_id, url) post_title = post.get('title')
entries = self._extract_entries(post, post_id, post_title) if not post_title:
if not entries: self.report_warning('Unable to extract post title. Falling back to parsing html page')
raise ExtractorError( webpage = self._download_webpage(url, video_id=post_id)
'no video found', video_id=post_id, expected=True) post_title = (self._og_search_title(webpage, fatal=False)
result = { or self._html_extract_title(webpage, fatal=True))
'id': post_id,
common_metadata = {
'title': post_title, 'title': post_title,
'channel': traverse_obj(post, ('user', 'name')), **traverse_obj(post, {
'timestamp': dict_get(post, ('publishTime', 'createdAt')), 'channel': ('user', 'name', {str}),
'modified_timestamp': post.get('updatedAt'), 'channel_id': ('user', 'id', {str_or_none}),
'tags': [tag['title'] for tag in post.get('tags', [])], 'timestamp': ('createdAt', {int_or_none}),
'like_count': traverse_obj(post, ('count', 'likes')), 'release_timestamp': ('publishTime', {int_or_none}),
'modified_timestamp': ('updatedAt', {int_or_none}),
'tags': ('tags', ..., 'title', {str}),
'like_count': ('count', 'likes', {int_or_none}),
}),
} }
if len(entries) == 1:
result.update(entries[0])
else:
result['_type'] = 'playlist'
result['entries'] = entries
return result
def _extract_title(self, post, post_id, url):
title = post.get('title')
if title:
return title
# falling back to parsing html page as a last resort
webpage = self._download_webpage(url, video_id=post_id)
return (
self._og_search_title(webpage, fatal=False)
or self._html_extract_title(webpage, fatal=True))
def _extract_entries(self, post, post_id, post_title):
entries = [] entries = []
for item in post['data']: for item in traverse_obj(post, ('data', ..., {dict})):
url = None
formats = None
item_id = item.get('id', post_id)
item_type = item.get('type') item_type = item.get('type')
if item_type == 'video': if item_type == 'video' and url_or_none(item.get('url')):
url = item.get('url') entries.append(self.url_result(item['url'], YoutubeIE))
elif item_type == 'ok_video': elif item_type == 'ok_video':
formats = self._extract_formats( video_id = item.get('id') or post_id
item.get('playerUrls', []), item_id) entries.append({
if not url and not formats: 'id': video_id,
continue 'formats': self._extract_formats(item.get('playerUrls'), video_id),
entry = { **common_metadata,
'id': item_id, **traverse_obj(item, {
'title': item.get('title', post_title), 'title': ('title', {str}),
'duration': item.get('duration'), 'duration': ('duration', {int_or_none}),
'view_count': item.get('viewsCounter'), 'view_count': ('viewsCounter', {int_or_none}),
'thumbnail': dict_get(item, ('preview', 'defaultPreview')), 'thumbnail': (('previewUrl', 'defaultPreview'), {url_or_none}),
} }, get_all=False)})
if url:
entry['_type'] = 'url_transparent'
entry['url'] = url
elif formats:
entry['formats'] = formats
entries.append(entry)
return entries
def _extract_formats(self, player_urls, post_id): if not entries:
raise ExtractorError('No videos found', expected=True)
if len(entries) == 1:
return entries[0]
return self.playlist_result(entries, post_id, post_title, **common_metadata)
def _extract_formats(self, player_urls, video_id):
formats = [] formats = []
for player_url in player_urls: mp4_types = ('tiny', 'lowest', 'low', 'medium', 'high', 'full_hd', 'quad_hd', 'ultra_hd')
url = player_url.get('url') quality = qualities(mp4_types)
if not url: for player_url in traverse_obj(player_urls, lambda _, v: url_or_none(v['url'])):
continue url = player_url['url']
_type = player_url.get('type') format_type = player_url.get('type')
if 'hls' in _type: if format_type in ('hls', 'hls_live', 'live_ondemand_hls', 'live_playback_hls'):
# 'hls', 'hls_live' formats.extend(self._extract_m3u8_formats(url, video_id, m3u8_id='hls', fatal=False))
formats.extend(self._extract_m3u8_formats( elif format_type in ('dash', 'dash_live', 'live_playback_dash'):
url, video_id=post_id, fatal=False)) formats.extend(self._extract_mpd_formats(url, video_id, mpd_id='dash', fatal=False))
elif 'dash' in _type: elif format_type in mp4_types:
# 'dash', 'dash_live'
formats.extend(self._extract_mpd_formats(
url, video_id=post_id, fatal=False))
else:
# one of: 'tiny', 'low', etc.; see _get_quality for full list
formats.append({ formats.append({
'url': url, 'url': url,
'ext': 'mp4', 'ext': 'mp4',
'format_id': _type, 'format_id': format_type,
'quality': quality(format_type),
}) })
for format in formats: else:
# .../type/<d>/... or ...&type=<d>&... self.report_warning(f'Unknown format type: {format_type!r}')
format_type = self._search_regex(
r'\btype[/=](\d)([^\d]|$)', format['url'],
'format type', default=None,
)
if format_type:
format['quality'] = self._get_quality(format_type)
return formats return formats
_get_quality = staticmethod(qualities((
'4', # tiny = 144p
'0', # lowest = 240p
'1', # low = 360p
'2', # medium = 480p
'3', # high = 720p
'5', # full_hd = 1080p
'6', # quad_hd = 1440p
'7', # ultra_hd = 2160p
)))