You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.0 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. js_to_json,
  7. mimetype2ext,
  8. )
  9. class ThreeQSDNIE(InfoExtractor):
  10. IE_NAME = '3qsdn'
  11. IE_DESC = '3Q SDN'
  12. _VALID_URL = r'https?://playout\.3qsdn\.com/(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
  13. _TESTS = [{
  14. # ondemand from http://www.philharmonie.tv/veranstaltung/26/
  15. 'url': 'http://playout.3qsdn.com/0280d6b9-1215-11e6-b427-0cc47a188158?protocol=http',
  16. 'md5': 'ab040e37bcfa2e0c079f92cb1dd7f6cd',
  17. 'info_dict': {
  18. 'id': '0280d6b9-1215-11e6-b427-0cc47a188158',
  19. 'ext': 'mp4',
  20. 'title': '0280d6b9-1215-11e6-b427-0cc47a188158',
  21. 'is_live': False,
  22. },
  23. 'expected_warnings': ['Failed to download MPD manifest'],
  24. }, {
  25. # live video stream
  26. 'url': 'https://playout.3qsdn.com/d755d94b-4ab9-11e3-9162-0025907ad44f?js=true',
  27. 'info_dict': {
  28. 'id': 'd755d94b-4ab9-11e3-9162-0025907ad44f',
  29. 'ext': 'mp4',
  30. 'title': 'd755d94b-4ab9-11e3-9162-0025907ad44f',
  31. 'is_live': False,
  32. },
  33. }, {
  34. # live audio stream
  35. 'url': 'http://playout.3qsdn.com/9edf36e0-6bf2-11e2-a16a-9acf09e2db48',
  36. 'only_matching': True,
  37. }, {
  38. # live audio stream with some 404 URLs
  39. 'url': 'http://playout.3qsdn.com/ac5c3186-777a-11e2-9c30-9acf09e2db48',
  40. 'only_matching': True,
  41. }, {
  42. # geo restricted with 'This content is not available in your country'
  43. 'url': 'http://playout.3qsdn.com/d63a3ffe-75e8-11e2-9c30-9acf09e2db48',
  44. 'only_matching': True,
  45. }, {
  46. # geo restricted with 'playout.3qsdn.com/forbidden'
  47. 'url': 'http://playout.3qsdn.com/8e330f26-6ae2-11e2-a16a-9acf09e2db48',
  48. 'only_matching': True,
  49. }, {
  50. # live video with rtmp link
  51. 'url': 'https://playout.3qsdn.com/6092bb9e-8f72-11e4-a173-002590c750be',
  52. 'only_matching': True,
  53. }]
  54. @staticmethod
  55. def _extract_url(webpage):
  56. mobj = re.search(
  57. r'<iframe[^>]+\b(?:data-)?src=(["\'])(?P<url>%s.*?)\1' % ThreeQSDNIE._VALID_URL, webpage)
  58. if mobj:
  59. return mobj.group('url')
  60. def _real_extract(self, url):
  61. video_id = self._match_id(url)
  62. js = self._download_webpage(
  63. 'http://playout.3qsdn.com/%s' % video_id, video_id,
  64. query={'js': 'true'})
  65. if any(p in js for p in (
  66. '>This content is not available in your country',
  67. 'playout.3qsdn.com/forbidden')):
  68. self.raise_geo_restricted()
  69. stream_content = self._search_regex(
  70. r'streamContent\s*:\s*(["\'])(?P<content>.+?)\1', js,
  71. 'stream content', default='demand', group='content')
  72. live = stream_content == 'live'
  73. stream_type = self._search_regex(
  74. r'streamType\s*:\s*(["\'])(?P<type>audio|video)\1', js,
  75. 'stream type', default='video', group='type')
  76. formats = []
  77. urls = set()
  78. def extract_formats(item_url, item={}):
  79. if not item_url or item_url in urls:
  80. return
  81. urls.add(item_url)
  82. ext = mimetype2ext(item.get('type')) or determine_ext(item_url, default_ext=None)
  83. if ext == 'mpd':
  84. formats.extend(self._extract_mpd_formats(
  85. item_url, video_id, mpd_id='mpd', fatal=False))
  86. elif ext == 'm3u8':
  87. formats.extend(self._extract_m3u8_formats(
  88. item_url, video_id, 'mp4',
  89. entry_protocol='m3u8' if live else 'm3u8_native',
  90. m3u8_id='hls', fatal=False))
  91. elif ext == 'f4m':
  92. formats.extend(self._extract_f4m_formats(
  93. item_url, video_id, f4m_id='hds', fatal=False))
  94. else:
  95. if not self._is_valid_url(item_url, video_id):
  96. return
  97. formats.append({
  98. 'url': item_url,
  99. 'format_id': item.get('quality'),
  100. 'ext': 'mp4' if item_url.startswith('rtsp') else ext,
  101. 'vcodec': 'none' if stream_type == 'audio' else None,
  102. })
  103. for item_js in re.findall(r'({.*?\b(?:src|source)\s*:\s*["\'].+?})', js):
  104. f = self._parse_json(
  105. item_js, video_id, transform_source=js_to_json, fatal=False)
  106. if not f:
  107. continue
  108. extract_formats(f.get('src'), f)
  109. # More relaxed version to collect additional URLs and acting
  110. # as a future-proof fallback
  111. for _, src in re.findall(r'\b(?:src|source)\s*:\s*(["\'])((?:https?|rtsp)://.+?)\1', js):
  112. extract_formats(src)
  113. self._sort_formats(formats)
  114. title = self._live_title(video_id) if live else video_id
  115. return {
  116. 'id': video_id,
  117. 'title': title,
  118. 'is_live': live,
  119. 'formats': formats,
  120. }