[vesti] Fix width and height
[youtube-dl] / youtube_dl / extractor / vesti.py
1 # encoding: 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 )
11
12
13 class VestiIE(InfoExtractor):
14     IE_NAME = 'vesti'
15     IE_DESC = 'Вести.Ru'
16     _VALID_URL = r'http://(?:.+?\.)?vesti\.ru/(?P<id>.+)'
17
18     _TESTS = [
19         {
20             'url': 'http://www.vesti.ru/videos?vid=575582&cid=1',
21             'info_dict': {
22                 'id': '765035',
23                 'ext': 'mp4',
24                 'title': 'Вести.net: биткоины в России не являются законными',
25                 'description': 'md5:d4bb3859dc1177b28a94c5014c35a36b',
26                 'duration': 302,
27             },
28             'params': {
29                 # m3u8 download
30                 'skip_download': True,
31             },
32         },
33         {
34             'url': 'http://www.vesti.ru/only_video.html?vid=576180',
35             'info_dict': {
36                 'id': '766048',
37                 'ext': 'mp4',
38                 'title': 'США заморозило, Британию затопило',
39                 'description': 'md5:f0ed0695ec05aed27c56a70a58dc4cc1',
40                 'duration': 87,
41             },
42             'params': {
43                 # m3u8 download
44                 'skip_download': True,
45             },
46         },
47         {
48             'url': 'http://sochi2014.vesti.ru/video/index/video_id/766403',
49             'info_dict': {
50                 'id': '766403',
51                 'ext': 'mp4',
52                 'title': 'XXII зимние Олимпийские игры. Российские хоккеисты стартовали на Олимпиаде с победы',
53                 'description': 'md5:55805dfd35763a890ff50fa9e35e31b3',
54                 'duration': 271,
55             },
56             'params': {
57                 # m3u8 download
58                 'skip_download': True,
59             },
60             'skip': 'Blocked outside Russia'
61         },
62         {
63             'url': 'http://sochi2014.vesti.ru/live/play/live_id/301',
64             'info_dict': {
65                 'id': '51499',
66                 'ext': 'flv',
67                 'title': 'Сочи-2014. Биатлон. Индивидуальная гонка. Мужчины ',
68                 'description': 'md5:9e0ed5c9d2fa1efbfdfed90c9a6d179c',
69             },
70             'params': {
71                 # rtmp download
72                 'skip_download': True,
73             },
74             'skip': 'Translation has finished'
75         }
76     ]
77
78     def _real_extract(self, url):
79         mobj = re.match(self._VALID_URL, url)
80         video_id = mobj.group('id')
81
82         page = self._download_webpage(url, video_id, 'Downloading page')
83
84         mobj = re.search(r'<meta property="og:video" content=".+?\.swf\?v?id=(?P<id>\d+).*?" />', page)
85         if mobj:
86             video_type = 'video'
87             video_id = mobj.group('id')
88         else:
89             mobj = re.search(
90                 r'<iframe.+?src="http://player\.rutv\.ru/iframe/(?P<type>[^/]+)/id/(?P<id>\d+)[^"]*".*?></iframe>', page)
91
92             if not mobj:
93                 raise ExtractorError('No media found')
94
95             video_type = mobj.group('type')
96             video_id = mobj.group('id')
97
98         json_data = self._download_json(
99             'http://player.rutv.ru/iframe/%splay/id/%s' % ('live-' if video_type == 'live' else '', video_id),
100             video_id, 'Downloading JSON')
101
102         if json_data['errors']:
103             raise ExtractorError('vesti returned error: %s' % json_data['errors'], expected=True)
104
105         playlist = json_data['data']['playlist']
106         medialist = playlist['medialist']
107         media = medialist[0]
108
109         if media['errors']:
110             raise ExtractorError('vesti returned error: %s' % media['errors'], expected=True)
111
112         view_count = playlist.get('count_views')
113         priority_transport = playlist['priority_transport']
114
115         thumbnail = media['picture']
116         width = int_or_none(media['width'])
117         height = int_or_none(media['height'])
118         description = media['anons']
119         title = media['title']
120         duration = int_or_none(media.get('duration'))
121
122         formats = []
123
124         for transport, links in media['sources'].items():
125             for quality, url in links.items():
126                 if transport == 'rtmp':
127                     mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>.+)$', url)
128                     if not mobj:
129                         continue
130                     fmt = {
131                         'url': mobj.group('url'),
132                         'play_path': mobj.group('playpath'),
133                         'app': mobj.group('app'),
134                         'page_url': 'http://player.rutv.ru',
135                         'player_url': 'http://player.rutv.ru/flash2v/osmf.swf?i=22',
136                         'rtmp_live': True,
137                         'ext': 'flv',
138                         'vbr': int(quality),
139                     }
140                 elif transport == 'm3u8':
141                     fmt = {
142                         'url': url,
143                         'ext': 'mp4',
144                     }
145                 else:
146                     fmt = {
147                         'url': url
148                     }
149                 fmt.update({
150                     'width': width,
151                     'height': height,
152                     'format_id': '%s-%s' % (transport, quality),
153                     'preference': -1 if priority_transport == transport else -2,
154                 })
155                 formats.append(fmt)
156
157         if not formats:
158             raise ExtractorError('No media links available for %s' % video_id)
159
160         self._sort_formats(formats)
161
162         return {
163             'id': video_id,
164             'title': title,
165             'description': description,
166             'thumbnail': thumbnail,
167             'view_count': view_count,
168             'duration': duration,
169             'formats': formats,
170         }