museai.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. ExtractorError,
  5. determine_ext,
  6. float_or_none,
  7. int_or_none,
  8. js_to_json,
  9. traverse_obj,
  10. url_or_none,
  11. )
  12. class MuseAIIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?muse\.ai/(?:v|embed)/(?P<id>\w+)'
  14. _TESTS = [{
  15. 'url': 'https://muse.ai/embed/YdTWvUW',
  16. 'md5': 'f994f9a38be1c3aaf9e37cbd7d76fe7c',
  17. 'info_dict': {
  18. 'id': 'YdTWvUW',
  19. 'ext': 'mp4',
  20. 'title': '2023-05-28-Grabien-1941111 (1)',
  21. 'description': '',
  22. 'uploader': 'Today News Africa',
  23. 'uploader_id': 'TodayNewsAfrica',
  24. 'upload_date': '20230528',
  25. 'timestamp': 1685285044,
  26. 'duration': 1291.3,
  27. 'view_count': int,
  28. 'availability': 'public',
  29. },
  30. }, {
  31. 'url': 'https://muse.ai/v/gQ4gGAA-0756',
  32. 'md5': '52dbfc78e865e56dc19a1715badc35e8',
  33. 'info_dict': {
  34. 'id': 'gQ4gGAA',
  35. 'ext': 'mp4',
  36. 'title': '0756',
  37. 'description': 'md5:0ca1483f9aac423e9a96ad00bb3a0785',
  38. 'uploader': 'Aerial.ie',
  39. 'uploader_id': 'aerial',
  40. 'upload_date': '20210306',
  41. 'timestamp': 1615072842,
  42. 'duration': 21.4,
  43. 'view_count': int,
  44. 'availability': 'public',
  45. },
  46. }]
  47. _WEBPAGE_TESTS = [{
  48. 'url': 'https://muse.ai/docs',
  49. 'playlist_mincount': 4,
  50. 'info_dict': {
  51. 'id': 'docs',
  52. 'title': 'muse.ai | docs',
  53. 'description': 'md5:6c0293431481582739c82ee8902687fa',
  54. 'age_limit': 0,
  55. 'thumbnail': 'https://muse.ai/static/imgs/poster-img-docs.png',
  56. },
  57. 'params': {'allowed_extractors': ['all', '-html5']},
  58. }]
  59. _EMBED_REGEX = [r'<iframe[^>]*\bsrc=["\'](?P<url>https://muse\.ai/embed/\w+)']
  60. @classmethod
  61. def _extract_embed_urls(cls, url, webpage):
  62. yield from super()._extract_embed_urls(url, webpage)
  63. for embed_id in re.findall(r'<script>[^<]*\bMusePlayer\(\{[^}<]*\bvideo:\s*["\'](\w+)["\']', webpage):
  64. yield f'https://muse.ai/embed/{embed_id}'
  65. def _real_extract(self, url):
  66. video_id = self._match_id(url)
  67. webpage = self._download_webpage(f'https://muse.ai/embed/{video_id}', video_id)
  68. data = self._search_json(
  69. r'player\.setData\(', webpage, 'player data', video_id, transform_source=js_to_json)
  70. source_url = data['url']
  71. if not url_or_none(source_url):
  72. raise ExtractorError('Unable to extract video URL')
  73. formats = [{
  74. 'url': source_url,
  75. 'format_id': 'source',
  76. 'quality': 1,
  77. **traverse_obj(data, {
  78. 'ext': ('filename', {determine_ext}),
  79. 'width': ('width', {int_or_none}),
  80. 'height': ('height', {int_or_none}),
  81. 'filesize': ('size', {int_or_none}),
  82. }),
  83. }]
  84. if source_url.endswith('/data'):
  85. base_url = f'{source_url[:-5]}/videos'
  86. formats.extend(self._extract_m3u8_formats(
  87. f'{base_url}/hls.m3u8', video_id, m3u8_id='hls', fatal=False))
  88. formats.extend(self._extract_mpd_formats(
  89. f'{base_url}/dash.mpd', video_id, mpd_id='dash', fatal=False))
  90. return {
  91. 'id': video_id,
  92. 'formats': formats,
  93. **traverse_obj(data, {
  94. 'title': ('title', {str}),
  95. 'description': ('description', {str}),
  96. 'duration': ('duration', {float_or_none}),
  97. 'timestamp': ('tcreated', {int_or_none}),
  98. 'uploader': ('owner_name', {str}),
  99. 'uploader_id': ('owner_username', {str}),
  100. 'view_count': ('views', {int_or_none}),
  101. 'age_limit': ('mature', {lambda x: 18 if x else None}),
  102. 'availability': ('visibility', {lambda x: x if x in ('private', 'unlisted') else 'public'}),
  103. }),
  104. }