[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / tv2dk.py
blob9cd7606b0a1e8fc87db95a6a93027300d998e93b
1 import json
2 import re
4 from .common import InfoExtractor
5 from ..utils import (
6 determine_ext,
7 extract_attributes,
8 js_to_json,
9 url_or_none,
13 class TV2DKIE(InfoExtractor):
14 _VALID_URL = r'''(?x)
15 https?://
16 (?:www\.)?
17 (?:
18 tvsyd|
19 tv2ostjylland|
20 tvmidtvest|
21 tv2fyn|
22 tv2east|
23 tv2lorry|
24 tv2nord
25 )\.dk/
26 (:[^/]+/)*
27 (?P<id>[^/?\#&]+)
28 '''
29 _TESTS = [{
30 'url': 'https://www.tvsyd.dk/nyheder/28-10-2019/1930/1930-28-okt-2019?autoplay=1#player',
31 'info_dict': {
32 'id': '0_52jmwa0p',
33 'ext': 'mp4',
34 'title': '19:30 - 28. okt. 2019',
35 'timestamp': 1572290248,
36 'upload_date': '20191028',
37 'uploader_id': 'tvsyd',
38 'duration': 1347,
39 'view_count': int,
41 'add_ie': ['Kaltura'],
42 }, {
43 'url': 'https://www.tv2lorry.dk/gadekamp/gadekamp-6-hoejhuse-i-koebenhavn',
44 'info_dict': {
45 'id': '1_7iwll9n0',
46 'ext': 'mp4',
47 'upload_date': '20211027',
48 'title': 'Gadekamp #6 - Højhuse i København',
49 'uploader_id': 'tv2lorry',
50 'timestamp': 1635345229,
52 'add_ie': ['Kaltura'],
53 }, {
54 'url': 'https://www.tv2ostjylland.dk/artikel/minister-gaar-ind-i-sag-om-diabetes-teknologi',
55 'only_matching': True,
56 }, {
57 'url': 'https://www.tv2ostjylland.dk/nyheder/28-10-2019/22/2200-nyhederne-mandag-d-28-oktober-2019?autoplay=1#player',
58 'only_matching': True,
59 }, {
60 'url': 'https://www.tvmidtvest.dk/nyheder/27-10-2019/1930/1930-27-okt-2019',
61 'only_matching': True,
62 }, {
63 'url': 'https://www.tv2fyn.dk/artikel/fyn-kan-faa-landets-foerste-fabrik-til-groent-jetbraendstof',
64 'only_matching': True,
65 }, {
66 'url': 'https://www.tv2east.dk/artikel/gods-faar-indleveret-tonsvis-af-aebler-100-kilo-aebler-gaar-til-en-aeblebrandy',
67 'only_matching': True,
68 }, {
69 'url': 'https://www.tv2lorry.dk/koebenhavn/rasmus-paludan-evakueret-til-egen-demonstration#player',
70 'only_matching': True,
71 }, {
72 'url': 'https://www.tv2nord.dk/artikel/dybt-uacceptabelt',
73 'only_matching': True,
76 def _real_extract(self, url):
77 video_id = self._match_id(url)
79 webpage = self._download_webpage(url, video_id)
81 entries = []
83 def add_entry(partner_id, kaltura_id):
84 entries.append(self.url_result(
85 f'kaltura:{partner_id}:{kaltura_id}', 'Kaltura',
86 video_id=kaltura_id))
88 for video_el in re.findall(r'(?s)<[^>]+\bdata-entryid\s*=[^>]*>', webpage):
89 video = extract_attributes(video_el)
90 kaltura_id = video.get('data-entryid')
91 if not kaltura_id:
92 continue
93 partner_id = video.get('data-partnerid')
94 if not partner_id:
95 continue
96 add_entry(partner_id, kaltura_id)
97 if not entries:
98 kaltura_id = self._search_regex(
99 (r'entry_id\s*:\s*["\']([0-9a-z_]+)',
100 r'\\u002FentryId\\u002F(\w+)\\u002F'), webpage, 'kaltura id')
101 partner_id = self._search_regex(
102 (r'\\u002Fp\\u002F(\d+)\\u002F', r'/p/(\d+)/'), webpage,
103 'partner id')
104 add_entry(partner_id, kaltura_id)
105 if len(entries) == 1:
106 return entries[0]
107 return self.playlist_result(entries)
110 class TV2DKBornholmPlayIE(InfoExtractor):
111 _VALID_URL = r'https?://play\.tv2bornholm\.dk/\?.*?\bid=(?P<id>\d+)'
112 _TEST = {
113 'url': 'http://play.tv2bornholm.dk/?area=specifikTV&id=781021',
114 'info_dict': {
115 'id': '781021',
116 'ext': 'mp4',
117 'title': '12Nyheder-27.11.19',
119 'params': {
120 'skip_download': True,
124 def _real_extract(self, url):
125 video_id = self._match_id(url)
127 video = self._download_json(
128 'https://play.tv2bornholm.dk/controls/AJAX.aspx/specifikVideo', video_id,
129 data=json.dumps({
130 'playlist_id': video_id,
131 'serienavn': '',
132 }).encode(), headers={
133 'X-Requested-With': 'XMLHttpRequest',
134 'Content-Type': 'application/json; charset=UTF-8',
135 })['d']
137 # TODO: generalize flowplayer
138 title = self._search_regex(
139 r'title\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1', video, 'title',
140 group='value')
141 sources = self._parse_json(self._search_regex(
142 r'(?s)sources:\s*(\[.+?\]),', video, 'sources'),
143 video_id, js_to_json)
145 formats = []
146 srcs = set()
147 for source in sources:
148 src = url_or_none(source.get('src'))
149 if not src:
150 continue
151 if src in srcs:
152 continue
153 srcs.add(src)
154 ext = determine_ext(src)
155 src_type = source.get('type')
156 if src_type == 'application/x-mpegurl' or ext == 'm3u8':
157 formats.extend(self._extract_m3u8_formats(
158 src, video_id, ext='mp4', entry_protocol='m3u8_native',
159 m3u8_id='hls', fatal=False))
160 elif src_type == 'application/dash+xml' or ext == 'mpd':
161 formats.extend(self._extract_mpd_formats(
162 src, video_id, mpd_id='dash', fatal=False))
163 else:
164 formats.append({
165 'url': src,
168 return {
169 'id': video_id,
170 'title': title,
171 'formats': formats,