threeqsdn.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. determine_ext,
  6. float_or_none,
  7. int_or_none,
  8. join_nonempty,
  9. parse_iso8601,
  10. )
  11. class ThreeQSDNIE(InfoExtractor):
  12. IE_NAME = '3qsdn'
  13. IE_DESC = '3Q SDN'
  14. _VALID_URL = r'https?://playout\.3qsdn\.com/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
  15. _EMBED_REGEX = [rf'<iframe[^>]+\b(?:data-)?src=(["\'])(?P<url>{_VALID_URL}.*?)\1']
  16. _TESTS = [{
  17. # https://player.3qsdn.com/demo.html
  18. 'url': 'https://playout.3qsdn.com/7201c779-6b3c-11e7-a40e-002590c750be',
  19. 'md5': '64a57396b16fa011b15e0ea60edce918',
  20. 'info_dict': {
  21. 'id': '7201c779-6b3c-11e7-a40e-002590c750be',
  22. 'ext': 'mp4',
  23. 'title': 'Video Ads',
  24. 'is_live': False,
  25. 'description': 'Video Ads Demo',
  26. 'timestamp': 1500334803,
  27. 'upload_date': '20170717',
  28. 'duration': 888.032,
  29. 'subtitles': {
  30. 'eng': 'count:1',
  31. },
  32. },
  33. 'expected_warnings': ['Unknown MIME type application/mp4 in DASH manifest'],
  34. }, {
  35. # live video stream
  36. 'url': 'https://playout.3qsdn.com/66e68995-11ca-11e8-9273-002590c750be',
  37. 'info_dict': {
  38. 'id': '66e68995-11ca-11e8-9273-002590c750be',
  39. 'ext': 'mp4',
  40. 'title': 're:^66e68995-11ca-11e8-9273-002590c750be [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  41. 'is_live': True,
  42. },
  43. 'params': {
  44. 'skip_download': True, # m3u8 downloads
  45. },
  46. }, {
  47. # live audio stream
  48. 'url': 'http://playout.3qsdn.com/9edf36e0-6bf2-11e2-a16a-9acf09e2db48',
  49. 'only_matching': True,
  50. }, {
  51. # live audio stream with some 404 URLs
  52. 'url': 'http://playout.3qsdn.com/ac5c3186-777a-11e2-9c30-9acf09e2db48',
  53. 'only_matching': True,
  54. }, {
  55. # geo restricted with 'This content is not available in your country'
  56. 'url': 'http://playout.3qsdn.com/d63a3ffe-75e8-11e2-9c30-9acf09e2db48',
  57. 'only_matching': True,
  58. }, {
  59. # geo restricted with 'playout.3qsdn.com/forbidden'
  60. 'url': 'http://playout.3qsdn.com/8e330f26-6ae2-11e2-a16a-9acf09e2db48',
  61. 'only_matching': True,
  62. }, {
  63. # live video with rtmp link
  64. 'url': 'https://playout.3qsdn.com/6092bb9e-8f72-11e4-a173-002590c750be',
  65. 'only_matching': True,
  66. }, {
  67. # ondemand from http://www.philharmonie.tv/veranstaltung/26/
  68. 'url': 'http://playout.3qsdn.com/0280d6b9-1215-11e6-b427-0cc47a188158?protocol=http',
  69. 'only_matching': True,
  70. }, {
  71. # live video stream
  72. 'url': 'https://playout.3qsdn.com/d755d94b-4ab9-11e3-9162-0025907ad44f?js=true',
  73. 'only_matching': True,
  74. }]
  75. def _extract_from_webpage(self, url, webpage):
  76. for res in super()._extract_from_webpage(url, webpage):
  77. yield {
  78. **res,
  79. '_type': 'url_transparent',
  80. 'uploader': self._search_regex(r'^(?:https?://)?([^/]*)/.*', url, 'video uploader'),
  81. }
  82. def _real_extract(self, url):
  83. video_id = self._match_id(url)
  84. try:
  85. config = self._download_json(
  86. url.replace('://playout.3qsdn.com/', '://playout.3qsdn.com/config/'), video_id)
  87. except ExtractorError as e:
  88. if isinstance(e.cause, HTTPError) and e.cause.status == 401:
  89. self.raise_geo_restricted()
  90. raise
  91. live = config.get('streamContent') == 'live'
  92. aspect = float_or_none(config.get('aspect'))
  93. formats = []
  94. subtitles = {}
  95. for source_type, source in (config.get('sources') or {}).items():
  96. if not source:
  97. continue
  98. if source_type == 'dash':
  99. fmts, subs = self._extract_mpd_formats_and_subtitles(
  100. source, video_id, mpd_id='mpd', fatal=False)
  101. formats.extend(fmts)
  102. subtitles = self._merge_subtitles(subtitles, subs)
  103. elif source_type == 'hls':
  104. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  105. source, video_id, 'mp4', live=live, m3u8_id='hls', fatal=False)
  106. formats.extend(fmts)
  107. subtitles = self._merge_subtitles(subtitles, subs)
  108. elif source_type == 'progressive':
  109. for s in source:
  110. src = s.get('src')
  111. if not (src and self._is_valid_url(src, video_id)):
  112. continue
  113. ext = determine_ext(src)
  114. height = int_or_none(s.get('height'))
  115. formats.append({
  116. 'ext': ext,
  117. 'format_id': join_nonempty('http', ext, height and f'{height}p'),
  118. 'height': height,
  119. 'source_preference': 0,
  120. 'url': src,
  121. 'vcodec': 'none' if height == 0 else None,
  122. 'width': int(height * aspect) if height and aspect else None,
  123. })
  124. for subtitle in (config.get('subtitles') or []):
  125. src = subtitle.get('src')
  126. if not src:
  127. continue
  128. subtitles.setdefault(subtitle.get('label') or 'eng', []).append({
  129. 'url': src,
  130. })
  131. title = config.get('title') or video_id
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'thumbnail': config.get('poster') or None,
  136. 'description': config.get('description') or None,
  137. 'timestamp': parse_iso8601(config.get('upload_date')),
  138. 'duration': float_or_none(config.get('vlength')) or None,
  139. 'is_live': live,
  140. 'formats': formats,
  141. 'subtitles': subtitles,
  142. # It seems like this would be correctly handled by default
  143. # However, unless someone can confirm this, the old
  144. # behaviour is being kept as-is
  145. '_format_sort_fields': ('res', 'source_preference'),
  146. }