[tvigle] Capture error message
[youtube-dl] / youtube_dl / extractor / tvigle.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     float_or_none,
10     int_or_none,
11     parse_age_limit,
12 )
13
14
15 class TvigleIE(InfoExtractor):
16     IE_NAME = 'tvigle'
17     IE_DESC = 'Интернет-телевидение Tvigle.ru'
18     _VALID_URL = r'https?://(?:www\.)?(?:tvigle\.ru/(?:[^/]+/)+(?P<display_id>[^/]+)/$|cloud\.tvigle\.ru/video/(?P<id>\d+))'
19
20     _TESTS = [
21         {
22             'url': 'http://www.tvigle.ru/video/sokrat/',
23             'md5': '36514aed3657d4f70b4b2cef8eb520cd',
24             'info_dict': {
25                 'id': '1848932',
26                 'display_id': 'sokrat',
27                 'ext': 'flv',
28                 'title': 'Сократ',
29                 'description': 'md5:d6b92ffb7217b4b8ebad2e7665253c17',
30                 'duration': 6586,
31                 'age_limit': 12,
32             },
33         },
34         {
35             'url': 'http://www.tvigle.ru/video/vladimir-vysotskii/vedushchii-teleprogrammy-60-minut-ssha-o-vladimire-vysotskom/',
36             'md5': 'e7efe5350dd5011d0de6550b53c3ba7b',
37             'info_dict': {
38                 'id': '5142516',
39                 'ext': 'flv',
40                 'title': 'Ведущий телепрограммы «60 минут» (США) о Владимире Высоцком',
41                 'description': 'md5:027f7dc872948f14c96d19b4178428a4',
42                 'duration': 186.080,
43                 'age_limit': 0,
44             },
45         }, {
46             'url': 'https://cloud.tvigle.ru/video/5267604/',
47             'only_matching': True,
48         }
49     ]
50
51     def _real_extract(self, url):
52         mobj = re.match(self._VALID_URL, url)
53         video_id = mobj.group('id')
54         display_id = mobj.group('display_id')
55
56         if not video_id:
57             webpage = self._download_webpage(url, display_id)
58             video_id = self._html_search_regex(
59                 r'class="video-preview current_playing" id="(\d+)">',
60                 webpage, 'video id')
61
62         video_data = self._download_json(
63             'http://cloud.tvigle.ru/api/play/video/%s/' % video_id, display_id)
64
65         item = video_data['playlist']['items'][0]
66
67         videos = item.get('videos')
68
69         error_message = item.get('errorMessage')
70         if not videos and error_message:
71             raise ExtractorError(
72                 '%s returned error: %s' % (self.IE_NAME, error_message), expected=True)
73
74         title = item['title']
75         description = item.get('description')
76         thumbnail = item.get('thumbnail')
77         duration = float_or_none(item.get('durationMilliseconds'), 1000)
78         age_limit = parse_age_limit(item.get('ageRestrictions'))
79
80         formats = []
81         for vcodec, fmts in item['videos'].items():
82             for format_id, video_url in fmts.items():
83                 if format_id == 'm3u8':
84                     formats.extend(self._extract_m3u8_formats(
85                         video_url, video_id, 'mp4', m3u8_id=vcodec))
86                     continue
87                 height = self._search_regex(
88                     r'^(\d+)[pP]$', format_id, 'height', default=None)
89                 formats.append({
90                     'url': video_url,
91                     'format_id': '%s-%s' % (vcodec, format_id),
92                     'vcodec': vcodec,
93                     'height': int_or_none(height),
94                     'filesize': int_or_none(item.get('video_files_size', {}).get(vcodec, {}).get(format_id)),
95                 })
96         self._sort_formats(formats)
97
98         return {
99             'id': video_id,
100             'display_id': display_id,
101             'title': title,
102             'description': description,
103             'thumbnail': thumbnail,
104             'duration': duration,
105             'age_limit': age_limit,
106             'formats': formats,
107         }