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