]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/rutv.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / rutv.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import ExtractorError, int_or_none, str_to_int
5
6
7 class RUTVIE(InfoExtractor):
8 IE_DESC = 'RUTV.RU'
9 _VALID_URL = r'''(?x)
10 https?://
11 (?:test)?player\.(?:rutv\.ru|vgtrk\.com)/
12 (?P<path>
13 flash\d+v/container\.swf\?id=|
14 iframe/(?P<type>swf|video|live)/id/|
15 index/iframe/cast_id/
16 )
17 (?P<id>\d+)
18 '''
19 _EMBED_URLS = [
20 r'<iframe[^>]+?src=(["\'])(?P<url>https?://(?:test)?player\.(?:rutv\.ru|vgtrk\.com)/(?:iframe/(?:swf|video|live)/id|index/iframe/cast_id)/.+?)\1',
21 r'<meta[^>]+?property=(["\'])og:video\1[^>]+?content=(["\'])(?P<url>https?://(?:test)?player\.(?:rutv\.ru|vgtrk\.com)/flash\d+v/container\.swf\?id=.+?\2)',
22 ]
23
24 _TESTS = [
25 {
26 'url': 'http://player.rutv.ru/flash2v/container.swf?id=774471&sid=kultura&fbv=true&isPlay=true&ssl=false&i=560&acc_video_id=episode_id/972347/video_id/978186/brand_id/31724',
27 'info_dict': {
28 'id': '774471',
29 'ext': 'mp4',
30 'title': 'Монологи на все времена',
31 'description': 'md5:18d8b5e6a41fb1faa53819471852d5d5',
32 'duration': 2906,
33 },
34 'params': {
35 # m3u8 download
36 'skip_download': True,
37 },
38 },
39 {
40 'url': 'https://player.vgtrk.com/flash2v/container.swf?id=774016&sid=russiatv&fbv=true&isPlay=true&ssl=false&i=560&acc_video_id=episode_id/972098/video_id/977760/brand_id/57638',
41 'info_dict': {
42 'id': '774016',
43 'ext': 'mp4',
44 'title': 'Чужой в семье Сталина',
45 'description': '',
46 'duration': 2539,
47 },
48 'params': {
49 # m3u8 download
50 'skip_download': True,
51 },
52 },
53 {
54 'url': 'http://player.rutv.ru/iframe/swf/id/766888/sid/hitech/?acc_video_id=4000',
55 'info_dict': {
56 'id': '766888',
57 'ext': 'mp4',
58 'title': 'Вести.net: интернет-гиганты начали перетягивание программных "одеял"',
59 'description': 'md5:65ddd47f9830c4f42ed6475f8730c995',
60 'duration': 279,
61 },
62 'params': {
63 # m3u8 download
64 'skip_download': True,
65 },
66 },
67 {
68 'url': 'http://player.rutv.ru/iframe/video/id/771852/start_zoom/true/showZoomBtn/false/sid/russiatv/?acc_video_id=episode_id/970443/video_id/975648/brand_id/5169',
69 'info_dict': {
70 'id': '771852',
71 'ext': 'mp4',
72 'title': 'Прямой эфир. Жертвы загадочной болезни: смерть от старости в 17 лет',
73 'description': 'md5:b81c8c55247a4bd996b43ce17395b2d8',
74 'duration': 3096,
75 },
76 'params': {
77 # m3u8 download
78 'skip_download': True,
79 },
80 },
81 {
82 'url': 'http://player.rutv.ru/iframe/live/id/51499/showZoomBtn/false/isPlay/true/sid/sochi2014',
83 'info_dict': {
84 'id': '51499',
85 'ext': 'flv',
86 'title': 'Сочи-2014. Биатлон. Индивидуальная гонка. Мужчины ',
87 'description': 'md5:9e0ed5c9d2fa1efbfdfed90c9a6d179c',
88 },
89 'skip': 'Translation has finished',
90 },
91 {
92 'url': 'http://player.rutv.ru/iframe/live/id/21/showZoomBtn/false/isPlay/true/',
93 'info_dict': {
94 'id': '21',
95 'ext': 'mp4',
96 'title': 're:^Россия 24. Прямой эфир [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
97 'is_live': True,
98 },
99 'params': {
100 # m3u8 download
101 'skip_download': True,
102 },
103 },
104 {
105 'url': 'https://testplayer.vgtrk.com/iframe/live/id/19201/showZoomBtn/false/isPlay/true/',
106 'only_matching': True,
107 },
108 ]
109
110 def _real_extract(self, url):
111 mobj = self._match_valid_url(url)
112 video_id = mobj.group('id')
113 video_path = mobj.group('path')
114
115 if re.match(r'flash\d+v', video_path):
116 video_type = 'video'
117 elif video_path.startswith('iframe'):
118 video_type = mobj.group('type')
119 if video_type == 'swf':
120 video_type = 'video'
121 elif video_path.startswith('index/iframe/cast_id'):
122 video_type = 'live'
123
124 is_live = video_type == 'live'
125
126 json_data = self._download_json(
127 'http://player.vgtrk.com/iframe/data%s/id/%s' % ('live' if is_live else 'video', video_id),
128 video_id, 'Downloading JSON')
129
130 if json_data['errors']:
131 raise ExtractorError('%s said: %s' % (self.IE_NAME, json_data['errors']), expected=True)
132
133 playlist = json_data['data']['playlist']
134 medialist = playlist['medialist']
135 media = medialist[0]
136
137 if media['errors']:
138 raise ExtractorError('%s said: %s' % (self.IE_NAME, media['errors']), expected=True)
139
140 view_count = int_or_none(playlist.get('count_views'))
141 priority_transport = playlist['priority_transport']
142
143 thumbnail = media['picture']
144 width = int_or_none(media['width'])
145 height = int_or_none(media['height'])
146 description = media['anons']
147 title = media['title']
148 duration = int_or_none(media.get('duration'))
149
150 formats = []
151 subtitles = {}
152
153 for transport, links in media['sources'].items():
154 for quality, url in links.items():
155 preference = -1 if priority_transport == transport else -2
156 if transport == 'rtmp':
157 mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>.+)$', url)
158 if not mobj:
159 continue
160 fmt = {
161 'url': mobj.group('url'),
162 'play_path': mobj.group('playpath'),
163 'app': mobj.group('app'),
164 'page_url': 'http://player.rutv.ru',
165 'player_url': 'http://player.rutv.ru/flash3v/osmf.swf?i=22',
166 'rtmp_live': True,
167 'ext': 'flv',
168 'vbr': str_to_int(quality),
169 }
170 elif transport == 'm3u8':
171 fmt, subs = self._extract_m3u8_formats_and_subtitles(
172 url, video_id, 'mp4', quality=preference, m3u8_id='hls')
173 formats.extend(fmt)
174 self._merge_subtitles(subs, target=subtitles)
175 continue
176 else:
177 fmt = {
178 'url': url
179 }
180 fmt.update({
181 'width': int_or_none(quality, default=height, invscale=width, scale=height),
182 'height': int_or_none(quality, default=height),
183 'format_id': '%s-%s' % (transport, quality),
184 'source_preference': preference,
185 })
186 formats.append(fmt)
187
188 return {
189 'id': video_id,
190 'title': title,
191 'description': description,
192 'thumbnail': thumbnail,
193 'view_count': view_count,
194 'duration': duration,
195 'formats': formats,
196 'subtitles': subtitles,
197 'is_live': is_live,
198 '_format_sort_fields': ('source', ),
199 }