[ie/whowatch] Rework extractor (#17217)

Authored by: doe1080
This commit is contained in:
doe1080
2026-12-01 05:26:59 +00:00
committed by GitHub
parent 7cc9b9b542
commit 99629786d9
3 changed files with 114 additions and 74 deletions
+2 -1
View File
@@ -402,7 +402,8 @@ Tip: Use `CTRL`+`F` (or `Command`+`F`) to search by keywords
(default)
--live-from-start Download livestreams from the start.
Currently experimental and only supported
for YouTube, Twitch, TVer, and mellow-fan
for YouTube, Twitch, TVer, mellow-fan, and
WhoWatch
--no-live-from-start Download livestreams from the current time
(default)
--wait-for-video MIN[-MAX] Wait for scheduled streams to become
+109 -72
View File
@@ -1,96 +1,133 @@
from .common import InfoExtractor
from ..utils import (
ExtractorError,
UserNotLive,
clean_html,
int_or_none,
qualities,
try_call,
try_get,
join_nonempty,
parse_resolution,
url_or_none,
)
from ..utils.traversal import require, traverse_obj
class WhoWatchIE(InfoExtractor):
_WORKING = False
IE_NAME = 'whowatch'
_VALID_URL = r'https?://whowatch\.tv/viewer/(?P<id>\d+)'
IE_DESC = 'ふわっち'
_API_BASE = 'https://api.whowatch.tv/lives'
_VALID_URL = r'https?://whowatch\.tv/(?P<type>archives|viewer)/(?P<id>\d+)'
_TESTS = [{
'url': 'https://whowatch.tv/viewer/21450171',
'only_matching': True,
'url': 'https://whowatch.tv/viewer/74386035',
'info_dict': {
'id': '74386035',
'ext': 'mp4',
'title': str,
'categories': ['雑談'],
'comment_count': int,
'concurrent_view_count': int,
'description': '母業',
'like_count': int,
'live_status': 'is_live',
'thumbnail': r're:https?://.+',
'timestamp': 1784016434,
'upload_date': '20260714',
'uploader': 'よるの⭐️ひかり',
'uploader_id': 'ふ:pikari',
'uploader_url': 'https://whowatch.tv/profile/w:pikari',
'view_count': int,
},
'skip': 'Invalid URL',
}, {
'url': 'https://whowatch.tv/archives/73287032',
'info_dict': {
'id': '73287032',
'ext': 'mp4',
'title': '森黒之 最後の放送',
'categories': ['雑談'],
'comment_count': int,
'description': '最後の見送り',
'duration': 10802,
'live_status': 'was_live',
'thumbnail': r're:https?://.+',
'timestamp': 1780615257,
'upload_date': '20260604',
'uploader': 'Yossan',
'uploader_id': 'ふ:yossan',
'uploader_url': 'https://whowatch.tv/profile/w:yossan',
'view_count': int,
},
}]
def _call_api(self, video_id, endpoint=None, query=None):
return self._download_json(
join_nonempty(self._API_BASE, video_id, endpoint, delim='/'),
video_id, query=query)
def _real_extract(self, url):
video_id = self._match_id(url)
self._download_webpage(url, video_id)
metadata = self._download_json(f'https://api.whowatch.tv/lives/{video_id}', video_id)
live_data = self._download_json(f'https://api.whowatch.tv/lives/{video_id}/play', video_id)
video_type, video_id = self._match_valid_url(url).group('type', 'id')
live_info = self._call_api(video_id)
if err_code := traverse_obj(live_info, (
'error_code', {str}, filter,
)):
err_msg = traverse_obj(live_info, (
'error_message', {clean_html}, filter))
raise ExtractorError(join_nonempty(err_code, err_msg, delim=': '))
title = try_call(
lambda: live_data['share_info']['live_title'][1:-1],
lambda: metadata['live']['title'],
expected_type=str)
live_status_key = traverse_obj(live_info, ('live', 'live_status', {str}))
live_status = {
'DELETED': 'was_live',
'FINISHED': 'was_live',
'PUBLISHING': 'is_live',
}.get(live_status_key)
user_path = traverse_obj(live_info, ('live', 'user', 'user_path', {str}))
hls_url = live_data.get('hls_url')
if not hls_url:
raise ExtractorError(live_data.get('error_message') or 'The user is offline.', expected=True)
if video_type == 'archives':
if live_status_key == 'DELETED':
raise ExtractorError(
'This video is no longer available', expected=True)
QUALITIES = qualities(['low', 'medium', 'high', 'veryhigh'])
formats = []
stream_info = self._call_api(video_id, 'play_archive')
playback_data = traverse_obj(live_info, {
'duration': ('live', 'running_time', {int_or_none}),
'view_count': ('archive_play_count', {int_or_none}),
})
else:
if live_status != 'is_live':
raise UserNotLive(video_id=user_path)
for i, fmt in enumerate(live_data.get('streams') or []):
name = fmt.get('quality') or fmt.get('name') or str(i)
hls_url = fmt.get('hls_url')
rtmp_url = fmt.get('rtmp_url')
audio_only = fmt.get('audio_only')
quality = QUALITIES(fmt.get('quality'))
stream_info = self._call_api(video_id, 'watch_info', query={'hls': 'true'})
playback_data = traverse_obj(live_info, ('live', {
'concurrent_view_count': ('view_count', {int_or_none}),
'view_count': ('total_view_count', {int_or_none}),
}))
if hls_url:
hls_fmts = self._extract_m3u8_formats(
hls_url, video_id, ext='mp4', m3u8_id=f'hls-{name}', quality=quality)
formats.extend(hls_fmts)
else:
hls_fmts = []
# RTMP url for audio_only is same as high format, so skip it
if rtmp_url and not audio_only:
formats.append({
'url': rtmp_url,
'format_id': f'rtmp-{name}',
'ext': 'mp4',
'protocol': 'rtmp_ffmpeg', # ffmpeg can, while rtmpdump can't
'vcodec': 'h264',
'acodec': 'aac',
'quality': quality,
'format_note': fmt.get('label'),
# note: HLS and RTMP have same resolution for now, so it's acceptable
'width': try_get(hls_fmts, lambda x: x[0]['width'], int),
'height': try_get(hls_fmts, lambda x: x[0]['height'], int),
})
# This contains the same formats as the above manifests and is used only as a fallback
formats.extend(self._extract_m3u8_formats(
hls_url, video_id, ext='mp4', m3u8_id='hls'))
self._remove_duplicate_formats(formats)
uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], str)
if uploader_url:
uploader_url = f'https://whowatch.tv/profile/{uploader_url}'
uploader_id = str(try_get(metadata, lambda x: x['live']['user']['id'], int))
uploader = try_get(metadata, lambda x: x['live']['user']['name'], str)
thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], str)
timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000)
view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int)
comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int)
m3u8_url = traverse_obj(stream_info, (
'hls_url', {url_or_none}, {require('m3u8 URL')}))
formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
for fmt in formats:
fmt.update(parse_resolution(fmt['url'], parse_fps=True))
if self.get_param('live_from_start'):
fmt.setdefault('downloader_options', {}).update({'ffmpeg_args': ['-live_start_index', '0']})
fmt['is_from_start'] = True
return {
'id': video_id,
'title': title,
'uploader_id': uploader_id,
'uploader_url': uploader_url,
'uploader': uploader,
'comment_count': traverse_obj(live_info, ('comment_count', {int_or_none})),
'formats': formats,
'thumbnail': thumbnail,
'timestamp': timestamp,
'view_count': view_count,
'comment_count': comment_count,
'is_live': True,
'live_status': live_status,
'uploader_url': f'https://whowatch.tv/profile/{user_path}',
**playback_data,
**traverse_obj(live_info, ('live', {
'title': ('title', {clean_html}, filter),
'categories': ('category', 'name', {clean_html}, filter, all, filter),
'description': ('telop', {clean_html}, filter),
'like_count': ('nice_info', 'total_count', {int_or_none}),
'thumbnail': ('latest_thumbnail_url', {url_or_none}),
'timestamp': ('started_at', {int_or_none(scale=1000)}),
})),
**traverse_obj(live_info, ('live', 'user', {
'uploader': ('name', {clean_html}, filter),
'uploader_id': ('account_name', {str}, filter),
})),
}
+3 -1
View File
@@ -511,7 +511,9 @@ def create_parser():
general.add_option(
'--live-from-start',
action='store_true', dest='live_from_start',
help='Download livestreams from the start. Currently experimental and only supported for YouTube, Twitch, TVer, and mellow-fan')
help=(
'Download livestreams from the start. Currently experimental and '
'only supported for YouTube, Twitch, TVer, mellow-fan, and WhoWatch'))
general.add_option(
'--no-live-from-start',
action='store_false', dest='live_from_start',