[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / ruv.py
blob5e55a4059c82323885c1dc36229f8cd87576f1f7
1 from .common import InfoExtractor
2 from ..utils import (
3 determine_ext,
4 parse_duration,
5 traverse_obj,
6 unified_timestamp,
10 class RuvIE(InfoExtractor):
11 _VALID_URL = r'https?://(?:www\.)?ruv\.is/(?:sarpurinn/[^/]+|node)/(?P<id>[^/]+(?:/\d+)?)'
12 _TESTS = [{
13 # m3u8
14 'url': 'http://ruv.is/sarpurinn/ruv-aukaras/fh-valur/20170516',
15 'md5': '66347652f4e13e71936817102acc1724',
16 'info_dict': {
17 'id': '1144499',
18 'display_id': 'fh-valur/20170516',
19 'ext': 'mp4',
20 'title': 'FH - Valur',
21 'description': 'Bein útsending frá 3. leik FH og Vals í úrslitum Olísdeildar karla í handbolta.',
22 'timestamp': 1494963600,
23 'upload_date': '20170516',
25 }, {
26 # mp3
27 'url': 'http://ruv.is/sarpurinn/ras-2/morgunutvarpid/20170619',
28 'md5': '395ea250c8a13e5fdb39d4670ef85378',
29 'info_dict': {
30 'id': '1153630',
31 'display_id': 'morgunutvarpid/20170619',
32 'ext': 'mp3',
33 'title': 'Morgunútvarpið',
34 'description': 'md5:a4cf1202c0a1645ca096b06525915418',
35 'timestamp': 1497855000,
36 'upload_date': '20170619',
38 }, {
39 'url': 'http://ruv.is/sarpurinn/ruv/frettir/20170614',
40 'only_matching': True,
41 }, {
42 'url': 'http://www.ruv.is/node/1151854',
43 'only_matching': True,
44 }, {
45 'url': 'http://ruv.is/sarpurinn/klippa/secret-soltice-hefst-a-morgun',
46 'only_matching': True,
47 }, {
48 'url': 'http://ruv.is/sarpurinn/ras-1/morgunvaktin/20170619',
49 'only_matching': True,
52 def _real_extract(self, url):
53 display_id = self._match_id(url)
55 webpage = self._download_webpage(url, display_id)
57 title = self._og_search_title(webpage)
59 FIELD_RE = r'video\.%s\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1'
61 media_url = self._html_search_regex(
62 FIELD_RE % 'src', webpage, 'video URL', group='url')
64 video_id = self._search_regex(
65 r'<link\b[^>]+\bhref=["\']https?://www\.ruv\.is/node/(\d+)',
66 webpage, 'video id', default=display_id)
68 ext = determine_ext(media_url)
70 if ext == 'm3u8':
71 formats = self._extract_m3u8_formats(
72 media_url, video_id, 'mp4', entry_protocol='m3u8_native',
73 m3u8_id='hls')
74 elif ext == 'mp3':
75 formats = [{
76 'format_id': 'mp3',
77 'url': media_url,
78 'vcodec': 'none',
80 else:
81 formats = [{
82 'url': media_url,
85 description = self._og_search_description(webpage, default=None)
86 thumbnail = self._og_search_thumbnail(
87 webpage, default=None) or self._search_regex(
88 FIELD_RE % 'poster', webpage, 'thumbnail', fatal=False)
89 timestamp = unified_timestamp(self._html_search_meta(
90 'article:published_time', webpage, 'timestamp', fatal=False))
92 return {
93 'id': video_id,
94 'display_id': display_id,
95 'title': title,
96 'description': description,
97 'thumbnail': thumbnail,
98 'timestamp': timestamp,
99 'formats': formats,
103 class RuvSpilaIE(InfoExtractor):
104 IE_NAME = 'ruv.is:spila'
105 _VALID_URL = r'https?://(?:www\.)?ruv\.is/(?:(?:sjon|ut)varp|(?:krakka|ung)ruv)/spila/.+/(?P<series_id>[0-9]+)/(?P<id>[a-z0-9]+)'
106 _TESTS = [{
107 'url': 'https://www.ruv.is/sjonvarp/spila/ithrottir/30657/9jcnd4',
108 'info_dict': {
109 'id': '9jcnd4',
110 'ext': 'mp4',
111 'title': '01.02.2022',
112 'chapters': 'count:4',
113 'timestamp': 1643743500,
114 'upload_date': '20220201',
115 'thumbnail': 'https://d38kdhuogyllre.cloudfront.net/fit-in/1960x/filters:quality(65)/hd_posters/94boog-iti3jg.jpg',
116 'description': 'Íþróttafréttir.',
117 'age_limit': 0,
119 }, {
120 'url': 'https://www.ruv.is/utvarp/spila/i-ljosi-sogunnar/23795/7hqkre',
121 'info_dict': {
122 'id': '7hqkre',
123 'ext': 'mp3',
124 'thumbnail': 'https://d38kdhuogyllre.cloudfront.net/fit-in/1960x/filters:quality(65)/hd_posters/7hqkre-7uepao.jpg',
125 'description': 'md5:8d7046549daff35e9a3190dc9901a120',
126 'chapters': [],
127 'upload_date': '20220204',
128 'timestamp': 1643965500,
129 'title': 'Nellie Bly II',
130 'age_limit': 0,
132 }, {
133 'url': 'https://www.ruv.is/ungruv/spila/ungruv/28046/8beuph',
134 'only_matching': True,
135 }, {
136 'url': 'https://www.ruv.is/krakkaruv/spila/krakkafrettir/30712/9jbgb0',
137 'only_matching': True,
140 def _real_extract(self, url):
141 display_id, series_id = self._match_valid_url(url).group('id', 'series_id')
142 program = self._download_json(
143 'https://www.ruv.is/gql/', display_id, query={'query': '''{
144 Program(id: %s){
145 title image description short_description
146 episodes(id: {value: "%s"}) {
147 rating title duration file image firstrun description
148 clips {
149 time text
151 subtitles {
152 name value
156 }''' % (series_id, display_id)})['data']['Program'] # noqa: UP031
157 episode = program['episodes'][0]
159 subs = {}
160 for trk in episode.get('subtitles'):
161 if trk.get('name') and trk.get('value'):
162 subs.setdefault(trk['name'], []).append({'url': trk['value'], 'ext': 'vtt'})
164 media_url = episode['file']
165 if determine_ext(media_url) == 'm3u8':
166 formats = self._extract_m3u8_formats(media_url, display_id)
167 else:
168 formats = [{'url': media_url}]
170 clips = [
171 {'start_time': parse_duration(c.get('time')), 'title': c.get('text')}
172 for c in episode.get('clips') or []]
174 return {
175 'id': display_id,
176 'title': traverse_obj(program, ('episodes', 0, 'title'), 'title'),
177 'description': traverse_obj(
178 program, ('episodes', 0, 'description'), 'description', 'short_description',
179 expected_type=lambda x: x or None),
180 'subtitles': subs,
181 'thumbnail': episode.get('image', '').replace('$$IMAGESIZE$$', '1960') or None,
182 'timestamp': unified_timestamp(episode.get('firstrun')),
183 'formats': formats,
184 'age_limit': episode.get('rating'),
185 'chapters': clips,