[vesti] Skip test 2 due to geo restrictions
[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'<div.+?id="current-video-holder".*?>\s*<iframe src="http://player\.rutv\.ru/iframe/(?P<type>[^/]+)/id/(?P<id>\d+)[^"]*"',
91                 page)
92
93             if not mobj:
94                 raise ExtractorError('No media found')
95
96             video_type = mobj.group('type')
97             video_id = mobj.group('id')
98
99         json_data = self._download_json(
100             'http://player.rutv.ru/iframe/%splay/id/%s' % ('live-' if video_type == 'live' else '', video_id),
101             video_id, 'Downloading JSON')
102
103         if json_data['errors']:
104             raise ExtractorError('vesti returned error: %s' % json_data['errors'], expected=True)
105
106         playlist = json_data['data']['playlist']
107         medialist = playlist['medialist']
108         media = medialist[0]
109
110         if media['errors']:
111             raise ExtractorError('vesti returned error: %s' % media['errors'], expected=True)
112
113         view_count = playlist.get('count_views')
114         priority_transport = playlist['priority_transport']
115
116         thumbnail = media['picture']
117         width = media['width']
118         height = media['height']
119         description = media['anons']
120         title = media['title']
121         duration = int_or_none(media.get('duration'))
122
123         formats = []
124
125         for transport, links in media['sources'].items():
126             for quality, url in links.items():
127                 if transport == 'rtmp':
128                     mobj = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>.+))/(?P<playpath>.+)$', url)
129                     if not mobj:
130                         continue
131                     fmt = {
132                         'url': mobj.group('url'),
133                         'play_path': mobj.group('playpath'),
134                         'app': mobj.group('app'),
135                         'page_url': 'http://player.rutv.ru',
136                         'player_url': 'http://player.rutv.ru/flash2v/osmf.swf?i=22',
137                         'rtmp_live': True,
138                         'ext': 'flv',
139                         'vbr': int(quality),
140                     }
141                 elif transport == 'm3u8':
142                     fmt = {
143                         'url': url,
144                         'ext': 'mp4',
145                     }
146                 else:
147                     fmt = {
148                         'url': url
149                     }
150                 fmt.update({
151                     'width': width,
152                     'height': height,
153                     'format_id': '%s-%s' % (transport, quality),
154                     'preference': -1 if priority_transport == transport else -2,
155                 })
156                 formats.append(fmt)
157
158         if not formats:
159             raise ExtractorError('No media links available for %s' % video_id)
160
161         self._sort_formats(formats)
162
163         return {
164             'id': video_id,
165             'title': title,
166             'description': description,
167             'thumbnail': thumbnail,
168             'view_count': view_count,
169             'duration': duration,
170             'formats': formats,
171         }