dacast.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import hashlib
  2. import re
  3. import time
  4. from .common import InfoExtractor
  5. from ..networking.exceptions import HTTPError
  6. from ..utils import (
  7. ExtractorError,
  8. classproperty,
  9. float_or_none,
  10. traverse_obj,
  11. url_or_none,
  12. )
  13. class DacastBaseIE(InfoExtractor):
  14. _URL_TYPE = None
  15. @classproperty
  16. def _VALID_URL(cls):
  17. return fr'https?://iframe\.dacast\.com/{cls._URL_TYPE}/(?P<user_id>[\w-]+)/(?P<id>[\w-]+)'
  18. @classproperty
  19. def _EMBED_REGEX(cls):
  20. return [rf'<iframe[^>]+\bsrc=["\'](?P<url>{cls._VALID_URL})']
  21. _API_INFO_URL = 'https://playback.dacast.com/content/info'
  22. @classmethod
  23. def _get_url_from_id(cls, content_id):
  24. user_id, media_id = content_id.split(f'-{cls._URL_TYPE}-')
  25. return f'https://iframe.dacast.com/{cls._URL_TYPE}/{user_id}/{media_id}'
  26. @classmethod
  27. def _extract_embed_urls(cls, url, webpage):
  28. yield from super()._extract_embed_urls(url, webpage)
  29. for content_id in re.findall(
  30. rf'<script[^>]+\bsrc=["\']https://player\.dacast\.com/js/player\.js\?contentId=([\w-]+-{cls._URL_TYPE}-[\w-]+)["\']', webpage):
  31. yield cls._get_url_from_id(content_id)
  32. class DacastVODIE(DacastBaseIE):
  33. _URL_TYPE = 'vod'
  34. _TESTS = [{
  35. 'url': 'https://iframe.dacast.com/vod/acae82153ef4d7a7344ae4eaa86af534/1c6143e3-5a06-371d-8695-19b96ea49090',
  36. 'info_dict': {
  37. 'id': '1c6143e3-5a06-371d-8695-19b96ea49090',
  38. 'ext': 'mp4',
  39. 'uploader_id': 'acae82153ef4d7a7344ae4eaa86af534',
  40. 'title': '2_4||Adnexal mass characterisation: O-RADS US and MRI||N. Bharwani, London/UK',
  41. 'thumbnail': 'https://universe-files.dacast.com/26137208-5858-65c1-5e9a-9d6b6bd2b6c2',
  42. },
  43. 'params': {'skip_download': 'm3u8'},
  44. }]
  45. _WEBPAGE_TESTS = [{
  46. 'url': 'https://www.dacast.com/support/knowledgebase/how-can-i-embed-a-video-on-my-website/',
  47. 'info_dict': {
  48. 'id': 'b6674869-f08a-23c5-1d7b-81f5309e1a90',
  49. 'ext': 'mp4',
  50. 'title': '4-HowToEmbedVideo.mp4',
  51. 'uploader_id': '3b67c4a9-3886-4eb1-d0eb-39b23b14bef3',
  52. 'thumbnail': 'https://universe-files.dacast.com/d26ab48f-a52a-8783-c42e-a90290ba06b6.png',
  53. },
  54. 'params': {'skip_download': 'm3u8'},
  55. }, {
  56. 'url': 'https://gist.githubusercontent.com/bashonly/4ad249ef2910346fbdf3809b220f11ee/raw/87349778d4af1a80b1fcc3beb9c88108de5858f5/dacast_embeds.html',
  57. 'info_dict': {
  58. 'id': 'e7df418e-a83b-7a7f-7b5e-1a667981e8fa',
  59. 'ext': 'mp4',
  60. 'title': 'Evening Service 2-5-23',
  61. 'uploader_id': '943bb1ab3c03695ba85330d92d6d226e',
  62. 'thumbnail': 'https://universe-files.dacast.com/337472b3-e92c-2ea4-7eb7-5700da477f67',
  63. },
  64. 'params': {'skip_download': 'm3u8'},
  65. }]
  66. def _real_extract(self, url):
  67. user_id, video_id = self._match_valid_url(url).group('user_id', 'id')
  68. query = {'contentId': f'{user_id}-vod-{video_id}', 'provider': 'universe'}
  69. info = self._download_json(self._API_INFO_URL, video_id, query=query, fatal=False)
  70. access = self._download_json(
  71. 'https://playback.dacast.com/content/access', video_id,
  72. note='Downloading access JSON', query=query, expected_status=403)
  73. error = access.get('error')
  74. if error in ('Broadcaster has been blocked', 'Content is offline'):
  75. raise ExtractorError(error, expected=True)
  76. elif error:
  77. raise ExtractorError(f'Dacast API says "{error}"')
  78. hls_url = access['hls']
  79. hls_aes = {}
  80. if 'DRM_EXT' in hls_url:
  81. self.report_drm(video_id)
  82. elif '/uspaes/' in hls_url:
  83. # From https://player.dacast.com/js/player.js
  84. ts = int(time.time())
  85. signature = hashlib.sha1(
  86. f'{10413792000 - ts}{ts}YfaKtquEEpDeusCKbvYszIEZnWmBcSvw').digest().hex()
  87. hls_aes['uri'] = f'https://keys.dacast.com/uspaes/{video_id}.key?s={signature}&ts={ts}'
  88. for retry in self.RetryManager():
  89. try:
  90. formats = self._extract_m3u8_formats(hls_url, video_id, 'mp4', m3u8_id='hls')
  91. except ExtractorError as e:
  92. # CDN will randomly respond with 403
  93. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  94. retry.error = e
  95. continue
  96. raise
  97. return {
  98. 'id': video_id,
  99. 'uploader_id': user_id,
  100. 'formats': formats,
  101. 'hls_aes': hls_aes or None,
  102. **traverse_obj(info, ('contentInfo', {
  103. 'title': 'title',
  104. 'duration': ('duration', {float_or_none}),
  105. 'thumbnail': ('thumbnailUrl', {url_or_none}),
  106. })),
  107. }
  108. class DacastPlaylistIE(DacastBaseIE):
  109. _URL_TYPE = 'playlist'
  110. _TESTS = [{
  111. 'url': 'https://iframe.dacast.com/playlist/943bb1ab3c03695ba85330d92d6d226e/b632eb053cac17a9c9a02bcfc827f2d8',
  112. 'playlist_mincount': 28,
  113. 'info_dict': {
  114. 'id': 'b632eb053cac17a9c9a02bcfc827f2d8',
  115. 'title': 'Archive Sermons',
  116. },
  117. }]
  118. _WEBPAGE_TESTS = [{
  119. 'url': 'https://gist.githubusercontent.com/bashonly/7efb606f49f3c6e07ea0327de5a661d1/raw/05a16eac830245ea301fb0a585023bec71e6093c/dacast_playlist_embed.html',
  120. 'playlist_mincount': 28,
  121. 'info_dict': {
  122. 'id': 'b632eb053cac17a9c9a02bcfc827f2d8',
  123. 'title': 'Archive Sermons',
  124. },
  125. }]
  126. def _real_extract(self, url):
  127. user_id, playlist_id = self._match_valid_url(url).group('user_id', 'id')
  128. info = self._download_json(
  129. self._API_INFO_URL, playlist_id, note='Downloading playlist JSON', query={
  130. 'contentId': f'{user_id}-playlist-{playlist_id}',
  131. 'provider': 'universe',
  132. })['contentInfo']
  133. def entries(info):
  134. for video in traverse_obj(info, ('features', 'playlist', 'contents', lambda _, v: v['id'])):
  135. yield self.url_result(
  136. DacastVODIE._get_url_from_id(video['id']), DacastVODIE, video['id'], video.get('title'))
  137. return self.playlist_result(entries(info), playlist_id, info.get('title'))