[tvc] Separate embed extractor
[youtube-dl] / youtube_dl / extractor / tvc.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     clean_html,
7     int_or_none,
8 )
9
10
11 class TVCEmbedIE(InfoExtractor):
12     _VALID_URL = r'http://(?:www\.)?tvc\.ru/video/iframe/id/(?P<id>\d+)'
13     _TEST = {
14         'url': 'http://www.tvc.ru/video/iframe/id/74622/isPlay/false/id_stat/channel/?acc_video_id=/channel/brand/id/17/show/episodes/episode_id/39702',
15         'md5': 'bbc5ff531d1e90e856f60fc4b3afd708',
16         'info_dict': {
17             'id': '74622',
18             'ext': 'mp4',
19             'title': 'События. "События". Эфир от 22.05.2015 14:30',
20             'thumbnail': 're:^https?://.*\.jpg$',
21             'duration': 1122,
22         },
23     }
24
25     def _real_extract(self, url):
26         video_id = self._match_id(url)
27
28         video = self._download_json(
29             'http://www.tvc.ru/video/json/id/%s' % video_id, video_id)
30
31         formats = []
32         for info in video.get('path', {}).get('quality', []):
33             video_url = info.get('url')
34             if not video_url:
35                 continue
36             format_id = self._search_regex(
37                 r'cdnvideo/([^/]+?)(?:-[^/]+?)?/', video_url,
38                 'format id', default=None)
39             formats.append({
40                 'url': video_url,
41                 'format_id': format_id,
42                 'width': int_or_none(info.get('width')),
43                 'height': int_or_none(info.get('height')),
44                 'tbr': int_or_none(info.get('bitrate')),
45             })
46         self._sort_formats(formats)
47
48         return {
49             'id': video_id,
50             'title': video['title'],
51             'thumbnail': video.get('picture'),
52             'duration': int_or_none(video.get('duration')),
53             'formats': formats,
54         }
55
56
57 class TVCIE(InfoExtractor):
58     _VALID_URL = r'http://(?:www\.)?tvc\.ru/(?!video/iframe/id/)(?P<id>[^?#]+)'
59     _TESTS = [{
60         'url': 'http://www.tvc.ru/channel/brand/id/29/show/episodes/episode_id/39702/',
61         'info_dict': {
62             'id': '74622',
63             'ext': 'mp4',
64             'title': 'События. "События". Эфир от 22.05.2015 14:30',
65             'description': 'md5:ad7aa7db22903f983e687b8a3e98c6dd',
66             'thumbnail': 're:^https?://.*\.jpg$',
67             'duration': 1122,
68         },
69     }, {
70         'url': 'http://www.tvc.ru/news/show/id/69944',
71         'info_dict': {
72             'id': '75399',
73             'ext': 'mp4',
74             'title': 'Эксперты: в столице встал вопрос о максимально безопасных остановках',
75             'description': 'md5:f2098f71e21f309e89f69b525fd9846e',
76             'thumbnail': 're:^https?://.*\.jpg$',
77             'duration': 278,
78         },
79     }, {
80         'url': 'http://www.tvc.ru/channel/brand/id/47/show/episodes#',
81         'info_dict': {
82             'id': '2185',
83             'ext': 'mp4',
84             'title': 'Ещё не поздно. Эфир от 03.08.2013',
85             'description': 'md5:51fae9f3f8cfe67abce014e428e5b027',
86             'thumbnail': 're:^https?://.*\.jpg$',
87             'duration': 3316,
88         },
89     }]
90
91     def _real_extract(self, url):
92         webpage = self._download_webpage(url, self._match_id(url))
93         return {
94             '_type': 'url_transparent',
95             'ie_key': 'TVCEmbed',
96             'url': self._og_search_video_url(webpage),
97             'title': clean_html(self._og_search_title(webpage)),
98             'description': clean_html(self._og_search_description(webpage)),
99             'thumbnail': self._og_search_thumbnail(webpage),
100         }