whowatch.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. int_or_none,
  5. qualities,
  6. try_call,
  7. try_get,
  8. )
  9. class WhoWatchIE(InfoExtractor):
  10. IE_NAME = 'whowatch'
  11. _VALID_URL = r'https?://whowatch\.tv/viewer/(?P<id>\d+)'
  12. _TESTS = [{
  13. 'url': 'https://whowatch.tv/viewer/21450171',
  14. 'only_matching': True,
  15. }]
  16. def _real_extract(self, url):
  17. video_id = self._match_id(url)
  18. self._download_webpage(url, video_id)
  19. metadata = self._download_json(f'https://api.whowatch.tv/lives/{video_id}', video_id)
  20. live_data = self._download_json(f'https://api.whowatch.tv/lives/{video_id}/play', video_id)
  21. title = try_call(
  22. lambda: live_data['share_info']['live_title'][1:-1],
  23. lambda: metadata['live']['title'],
  24. expected_type=str)
  25. hls_url = live_data.get('hls_url')
  26. if not hls_url:
  27. raise ExtractorError(live_data.get('error_message') or 'The user is offline.', expected=True)
  28. QUALITIES = qualities(['low', 'medium', 'high', 'veryhigh'])
  29. formats = []
  30. for i, fmt in enumerate(live_data.get('streams') or []):
  31. name = fmt.get('quality') or fmt.get('name') or str(i)
  32. hls_url = fmt.get('hls_url')
  33. rtmp_url = fmt.get('rtmp_url')
  34. audio_only = fmt.get('audio_only')
  35. quality = QUALITIES(fmt.get('quality'))
  36. if hls_url:
  37. hls_fmts = self._extract_m3u8_formats(
  38. hls_url, video_id, ext='mp4', m3u8_id=f'hls-{name}', quality=quality)
  39. formats.extend(hls_fmts)
  40. else:
  41. hls_fmts = []
  42. # RTMP url for audio_only is same as high format, so skip it
  43. if rtmp_url and not audio_only:
  44. formats.append({
  45. 'url': rtmp_url,
  46. 'format_id': f'rtmp-{name}',
  47. 'ext': 'mp4',
  48. 'protocol': 'rtmp_ffmpeg', # ffmpeg can, while rtmpdump can't
  49. 'vcodec': 'h264',
  50. 'acodec': 'aac',
  51. 'quality': quality,
  52. 'format_note': fmt.get('label'),
  53. # note: HLS and RTMP have same resolution for now, so it's acceptable
  54. 'width': try_get(hls_fmts, lambda x: x[0]['width'], int),
  55. 'height': try_get(hls_fmts, lambda x: x[0]['height'], int),
  56. })
  57. # This contains the same formats as the above manifests and is used only as a fallback
  58. formats.extend(self._extract_m3u8_formats(
  59. hls_url, video_id, ext='mp4', m3u8_id='hls'))
  60. self._remove_duplicate_formats(formats)
  61. uploader_url = try_get(metadata, lambda x: x['live']['user']['user_path'], str)
  62. if uploader_url:
  63. uploader_url = f'https://whowatch.tv/profile/{uploader_url}'
  64. uploader_id = str(try_get(metadata, lambda x: x['live']['user']['id'], int))
  65. uploader = try_get(metadata, lambda x: x['live']['user']['name'], str)
  66. thumbnail = try_get(metadata, lambda x: x['live']['latest_thumbnail_url'], str)
  67. timestamp = int_or_none(try_get(metadata, lambda x: x['live']['started_at'], int), scale=1000)
  68. view_count = try_get(metadata, lambda x: x['live']['total_view_count'], int)
  69. comment_count = try_get(metadata, lambda x: x['live']['comment_count'], int)
  70. return {
  71. 'id': video_id,
  72. 'title': title,
  73. 'uploader_id': uploader_id,
  74. 'uploader_url': uploader_url,
  75. 'uploader': uploader,
  76. 'formats': formats,
  77. 'thumbnail': thumbnail,
  78. 'timestamp': timestamp,
  79. 'view_count': view_count,
  80. 'comment_count': comment_count,
  81. 'is_live': True,
  82. }