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