Merge commit '98703c7fbfcf06348220aa63f9422cdd792cfe1a'
[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     str_to_int,
10 )
11
12
13 class TvigleIE(InfoExtractor):
14     IE_NAME = 'tvigle'
15     IE_DESC = 'Интернет-телевидение Tvigle.ru'
16     _VALID_URL = r'http://(?:www\.)?tvigle\.ru/(?:[^/]+/)+(?P<display_id>[^/]+)/$'
17
18     _TESTS = [
19         {
20             'url': 'http://www.tvigle.ru/video/brat-2/',
21             'md5': '72cb7eab33e54314e1790da402d3c9c3',
22             'info_dict': {
23                 'id': '5119390',
24                 'display_id': 'brat-2',
25                 'ext': 'mp4',
26                 'title': 'Брат 2 ',
27                 'description': 'md5:5751f4fe345a58e1692585c361294bd8',
28                 'duration': 7356.369,
29                 'age_limit': 0,
30             },
31         },
32         {
33             'url': 'http://www.tvigle.ru/video/vladimir-vysotskii/vedushchii-teleprogrammy-60-minut-ssha-o-vladimire-vysotskom/',
34             'md5': 'd9012d7c7c598fe7a11d7fb46dc1f574',
35             'info_dict': {
36                 'id': '5142516',
37                 'ext': 'mp4',
38                 'title': 'Ведущий телепрограммы «60 минут» (США) о Владимире Высоцком',
39                 'description': 'md5:027f7dc872948f14c96d19b4178428a4',
40                 'duration': 186.080,
41                 'age_limit': 0,
42             },
43         },
44     ]
45
46     def _real_extract(self, url):
47         mobj = re.match(self._VALID_URL, url)
48         display_id = mobj.group('display_id')
49
50         webpage = self._download_webpage(url, display_id)
51
52         video_id = self._html_search_regex(
53             r'<li class="video-preview current_playing" id="(\d+)">', webpage, 'video id')
54
55         video_data = self._download_json(
56             'http://cloud.tvigle.ru/api/play/video/%s/' % video_id, display_id)
57
58         item = video_data['playlist']['items'][0]
59
60         title = item['title']
61         description = item['description']
62         thumbnail = item['thumbnail']
63         duration = float_or_none(item['durationMilliseconds'], 1000)
64         age_limit = str_to_int(item['ageRestrictions'])
65
66         formats = []
67         for vcodec, fmts in item['videos'].items():
68             for quality, video_url in fmts.items():
69                 formats.append({
70                     'url': video_url,
71                     'format_id': '%s-%s' % (vcodec, quality),
72                     'vcodec': vcodec,
73                     'height': int(quality[:-1]),
74                 })
75         self._sort_formats(formats)
76
77         return {
78             'id': video_id,
79             'display_id': display_id,
80             'title': title,
81             'description': description,
82             'thumbnail': thumbnail,
83             'duration': duration,
84             'age_limit': age_limit,
85             'formats': formats,
86         }