[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / urplay.py
blob79bb8a8055627b1d7479909b99ef7ff4c5dc285f
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 ISO639Utils,
5 dict_get,
6 int_or_none,
7 parse_age_limit,
8 try_get,
9 unified_timestamp,
13 class URPlayIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?ur(?:play|skola)\.se/(?:program|Produkter)/(?P<id>[0-9]+)'
15 _TESTS = [{
16 'url': 'https://urplay.se/program/203704-ur-samtiden-livet-universum-och-rymdens-markliga-musik-om-vetenskap-kritiskt-tankande-och-motstand',
17 'md5': '5ba36643c77cc3d34ffeadad89937d1e',
18 'info_dict': {
19 'id': '203704',
20 'ext': 'mp4',
21 'title': 'UR Samtiden - Livet, universum och rymdens märkliga musik : Om vetenskap, kritiskt tänkande och motstånd',
22 'description': 'md5:5344508a52aa78c1ced6c1b8b9e44e9a',
23 'thumbnail': r're:^https?://.+\.jpg',
24 'timestamp': 1513292400,
25 'upload_date': '20171214',
26 'series': 'UR Samtiden - Livet, universum och rymdens märkliga musik',
27 'duration': 2269,
28 'categories': ['Vetenskap & teknik'],
29 'tags': ['Kritiskt tänkande', 'Vetenskap', 'Vetenskaplig verksamhet'],
30 'episode': 'Om vetenskap, kritiskt tänkande och motstånd',
31 'age_limit': 15,
33 }, {
34 'url': 'https://urplay.se/program/222967-en-foralders-dagbok-mitt-barn-skadar-sig-sjalv',
35 'info_dict': {
36 'id': '222967',
37 'ext': 'mp4',
38 'title': 'En förälders dagbok : Mitt barn skadar sig själv',
39 'description': 'md5:9f771eef03a732a213b367b52fe826ca',
40 'thumbnail': r're:^https?://.+\.jpg',
41 'timestamp': 1629676800,
42 'upload_date': '20210823',
43 'series': 'En förälders dagbok',
44 'duration': 1740,
45 'age_limit': 15,
46 'episode_number': 3,
47 'categories': 'count:2',
48 'tags': 'count:7',
49 'episode': 'Mitt barn skadar sig själv',
51 }, {
52 'url': 'https://urskola.se/Produkter/190031-Tripp-Trapp-Trad-Sovkudde',
53 'info_dict': {
54 'id': '190031',
55 'ext': 'mp4',
56 'title': 'Tripp, Trapp, Träd : Sovkudde',
57 'description': 'md5:b86bffdae04a7e9379d1d7e5947df1d1',
58 'thumbnail': r're:^https?://.+\.jpg',
59 'timestamp': 1440086400,
60 'upload_date': '20150820',
61 'series': 'Tripp, Trapp, Träd',
62 'duration': 865,
63 'age_limit': 1,
64 'episode_number': 1,
65 'categories': [],
66 'tags': ['Sova'],
67 'episode': 'Sovkudde',
68 'season': 'Säsong 1',
70 }, {
71 'url': 'http://urskola.se/Produkter/155794-Smasagor-meankieli-Grodan-i-vida-varlden',
72 'only_matching': True,
75 def _real_extract(self, url):
76 video_id = self._match_id(url)
77 url = url.replace('skola.se/Produkter', 'play.se/program')
78 webpage = self._download_webpage(url, video_id)
79 urplayer_data = self._search_nextjs_data(webpage, video_id, fatal=False) or {}
80 if urplayer_data:
81 urplayer_data = try_get(urplayer_data, lambda x: x['props']['pageProps']['program'], dict)
82 if not urplayer_data:
83 raise ExtractorError('Unable to parse __NEXT_DATA__')
84 else:
85 accessible_episodes = self._parse_json(self._html_search_regex(
86 r'data-react-class="routes/Product/components/ProgramContainer/ProgramContainer"[^>]+data-react-props="({.+?})"',
87 webpage, 'urplayer data'), video_id)['accessibleEpisodes']
88 urplayer_data = next(e for e in accessible_episodes if e.get('id') == int_or_none(video_id))
89 episode = urplayer_data['title']
91 host = self._download_json('http://streaming-loadbalancer.ur.se/loadbalancer.json', video_id)['redirect']
92 formats = []
93 urplayer_streams = urplayer_data.get('streamingInfo', {})
95 for k, v in urplayer_streams.get('raw', {}).items():
96 if not (k in ('sd', 'hd', 'mp3', 'm4a') and isinstance(v, dict)):
97 continue
98 file_http = v.get('location')
99 if file_http:
100 formats.extend(self._extract_wowza_formats(
101 f'http://{host}/{file_http}playlist.m3u8',
102 video_id, skip_protocols=['f4m', 'rtmp', 'rtsp']))
104 subtitles = {}
106 def parse_lang_code(code):
107 "3-character language code or None (utils candidate)"
108 if code is None:
109 return
110 lang = code.lower()
111 if not ISO639Utils.long2short(lang):
112 lang = ISO639Utils.short2long(lang)
113 return lang or None
115 for stream in urplayer_data['streamingInfo'].values():
116 for k, v in stream.items():
117 if (k in ('sd', 'hd') or not isinstance(v, dict)):
118 continue
119 lang, sttl_url = (v.get(kk) for kk in ('language', 'location'))
120 if not sttl_url:
121 continue
122 lang = parse_lang_code(lang)
123 if not lang:
124 continue
125 sttl = subtitles.get(lang) or []
126 sttl.append({'ext': k, 'url': sttl_url})
127 subtitles[lang] = sttl
129 image = urplayer_data.get('image') or {}
130 thumbnails = []
131 for k, v in image.items():
132 t = {
133 'id': k,
134 'url': v,
136 wh = k.split('x')
137 if len(wh) == 2:
138 t.update({
139 'width': int_or_none(wh[0]),
140 'height': int_or_none(wh[1]),
142 thumbnails.append(t)
144 series = urplayer_data.get('series') or {}
145 series_title = dict_get(series, ('seriesTitle', 'title')) or dict_get(urplayer_data, ('seriesTitle', 'mainTitle'))
147 return {
148 'id': video_id,
149 'title': f'{series_title} : {episode}' if series_title else episode,
150 'description': urplayer_data.get('description'),
151 'thumbnails': thumbnails,
152 'timestamp': unified_timestamp(urplayer_data.get('publishedAt')),
153 'series': series_title,
154 'formats': formats,
155 'duration': int_or_none(urplayer_data.get('duration')),
156 'categories': urplayer_data.get('categories'),
157 'tags': urplayer_data.get('keywords'),
158 'season': series.get('label'),
159 'episode': episode,
160 'episode_number': int_or_none(urplayer_data.get('episodeNumber')),
161 'age_limit': parse_age_limit(min(try_get(a, lambda x: x['from'], int) or 0
162 for a in urplayer_data.get('ageRanges', []))),
163 'subtitles': subtitles,