logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

redge.py (5199B)


  1. from .common import InfoExtractor
  2. from ..networking import HEADRequest
  3. from ..utils import (
  4. float_or_none,
  5. int_or_none,
  6. join_nonempty,
  7. parse_qs,
  8. update_url_query,
  9. )
  10. from ..utils.traversal import traverse_obj
  11. class RedCDNLivxIE(InfoExtractor):
  12. _VALID_URL = r'https?://[^.]+\.(?:dcs\.redcdn|atmcdn)\.pl/(?:live(?:dash|hls|ss)|nvr)/o2/(?P<tenant>[^/?#]+)/(?P<id>[^?#]+)\.livx'
  13. IE_NAME = 'redcdnlivx'
  14. _TESTS = [{
  15. 'url': 'https://r.dcs.redcdn.pl/livedash/o2/senat/ENC02/channel.livx?indexMode=true&startTime=638272860000&stopTime=638292544000',
  16. 'info_dict': {
  17. 'id': 'ENC02-638272860000-638292544000',
  18. 'ext': 'mp4',
  19. 'title': 'ENC02',
  20. 'duration': 19683.982,
  21. 'live_status': 'was_live',
  22. },
  23. }, {
  24. 'url': 'https://r.dcs.redcdn.pl/livedash/o2/sejm/ENC18/live.livx?indexMode=true&startTime=722333096000&stopTime=722335562000',
  25. 'info_dict': {
  26. 'id': 'ENC18-722333096000-722335562000',
  27. 'ext': 'mp4',
  28. 'title': 'ENC18',
  29. 'duration': 2463.995,
  30. 'live_status': 'was_live',
  31. },
  32. }, {
  33. 'url': 'https://r.dcs.redcdn.pl/livehls/o2/sportevolution/live/triathlon2018/warsaw.livx/playlist.m3u8?startTime=550305000000&stopTime=550327620000',
  34. 'info_dict': {
  35. 'id': 'triathlon2018-warsaw-550305000000-550327620000',
  36. 'ext': 'mp4',
  37. 'title': 'triathlon2018/warsaw',
  38. 'duration': 22619.98,
  39. 'live_status': 'was_live',
  40. },
  41. }, {
  42. 'url': 'https://n-25-12.dcs.redcdn.pl/nvr/o2/sejm/Migacz-ENC01/1.livx?startTime=722347200000&stopTime=722367345000',
  43. 'only_matching': True,
  44. }, {
  45. 'url': 'https://redir.atmcdn.pl/nvr/o2/sejm/ENC08/1.livx?startTime=503831270000&stopTime=503840040000',
  46. 'only_matching': True,
  47. }]
  48. '''
  49. Known methods (first in url path):
  50. - `livedash` - DASH MPD
  51. - `livehls` - HTTP Live Streaming
  52. - `livess` - IIS Smooth Streaming
  53. - `nvr` - CCTV mode, directly returns a file, typically flv, avc1, aac
  54. - `sc` - shoutcast/icecast (audio streams, like radio)
  55. '''
  56. def _real_extract(self, url):
  57. tenant, path = self._match_valid_url(url).group('tenant', 'id')
  58. qs = parse_qs(url)
  59. start_time = traverse_obj(qs, ('startTime', 0, {int_or_none}))
  60. stop_time = traverse_obj(qs, ('stopTime', 0, {int_or_none}))
  61. def livx_mode(mode):
  62. suffix = ''
  63. if mode == 'livess':
  64. suffix = '/manifest'
  65. elif mode == 'livehls':
  66. suffix = '/playlist.m3u8'
  67. file_qs = {}
  68. if start_time:
  69. file_qs['startTime'] = start_time
  70. if stop_time:
  71. file_qs['stopTime'] = stop_time
  72. if mode == 'nvr':
  73. file_qs['nolimit'] = 1
  74. elif mode != 'sc':
  75. file_qs['indexMode'] = 'true'
  76. return update_url_query(f'https://r.dcs.redcdn.pl/{mode}/o2/{tenant}/{path}.livx{suffix}', file_qs)
  77. # no id or title for a transmission. making ones up.
  78. title = path \
  79. .replace('/live', '').replace('live/', '') \
  80. .replace('/channel', '').replace('channel/', '') \
  81. .strip('/')
  82. video_id = join_nonempty(title.replace('/', '-'), start_time, stop_time)
  83. formats = []
  84. # downloading the manifest separately here instead of _extract_ism_formats to also get some stream metadata
  85. ism_res = self._download_xml_handle(
  86. livx_mode('livess'), video_id,
  87. note='Downloading ISM manifest',
  88. errnote='Failed to download ISM manifest',
  89. fatal=False)
  90. ism_doc = None
  91. if ism_res is not False:
  92. ism_doc, ism_urlh = ism_res
  93. formats, _ = self._parse_ism_formats_and_subtitles(ism_doc, ism_urlh.url, 'ss')
  94. nvr_urlh = self._request_webpage(
  95. HEADRequest(livx_mode('nvr')), video_id, 'Follow flv file redirect', fatal=False,
  96. expected_status=lambda _: True)
  97. if nvr_urlh and nvr_urlh.status == 200:
  98. formats.append({
  99. 'url': nvr_urlh.url,
  100. 'ext': 'flv',
  101. 'format_id': 'direct-0',
  102. 'preference': -1, # might be slow
  103. })
  104. formats.extend(self._extract_mpd_formats(livx_mode('livedash'), video_id, mpd_id='dash', fatal=False))
  105. formats.extend(self._extract_m3u8_formats(
  106. livx_mode('livehls'), video_id, m3u8_id='hls', ext='mp4', fatal=False))
  107. time_scale = traverse_obj(ism_doc, ('@TimeScale', {int_or_none})) or 10000000
  108. duration = traverse_obj(
  109. ism_doc, ('@Duration', {float_or_none(scale=time_scale)})) or None
  110. live_status = None
  111. if traverse_obj(ism_doc, '@IsLive') == 'TRUE':
  112. live_status = 'is_live'
  113. elif duration:
  114. live_status = 'was_live'
  115. return {
  116. 'id': video_id,
  117. 'title': title,
  118. 'formats': formats,
  119. 'duration': duration,
  120. 'live_status': live_status,
  121. }