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