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