Merge remote-tracking branch 'hojel/slutload'
[youtube-dl] / youtube_dl / extractor / ntv.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     unescapeHTML
10 )
11
12
13 class NTVIE(InfoExtractor):
14     _VALID_URL = r'http://(?:www\.)?ntv\.ru/(?P<id>.+)'
15
16     _TESTS = [
17         {
18             'url': 'http://www.ntv.ru/novosti/863142/',
19             'info_dict': {
20                 'id': '746000',
21                 'ext': 'flv',
22                 'title': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
23                 'description': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
24                 'duration': 136,
25             },
26             'params': {
27                 # rtmp download
28                 'skip_download': True,
29             },
30         },
31         {
32             'url': 'http://www.ntv.ru/video/novosti/750370/',
33             'info_dict': {
34                 'id': '750370',
35                 'ext': 'flv',
36                 'title': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
37                 'description': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
38                 'duration': 172,
39             },
40             'params': {
41                 # rtmp download
42                 'skip_download': True,
43             },
44         },
45         {
46             'url': 'http://www.ntv.ru/peredacha/segodnya/m23700/o232416',
47             'info_dict': {
48                 'id': '747480',
49                 'ext': 'flv',
50                 'title': '«Сегодня». 21 марта 2014 года. 16:00 ',
51                 'description': '«Сегодня». 21 марта 2014 года. 16:00 ',
52                 'duration': 1496,
53             },
54             'params': {
55                 # rtmp download
56                 'skip_download': True,
57             },
58         },
59         {
60             'url': 'http://www.ntv.ru/kino/Koma_film',
61             'info_dict': {
62                 'id': '758100',
63                 'ext': 'flv',
64                 'title': 'Остросюжетный фильм «Кома»',
65                 'description': 'Остросюжетный фильм «Кома»',
66                 'duration': 5592,
67             },
68             'params': {
69                 # rtmp download
70                 'skip_download': True,
71             },
72         },
73         {
74             'url': 'http://www.ntv.ru/serial/Delo_vrachey/m31760/o233916/',
75             'info_dict': {
76                 'id': '751482',
77                 'ext': 'flv',
78                 'title': '«Дело врачей»: «Деревце жизни»',
79                 'description': '«Дело врачей»: «Деревце жизни»',
80                 'duration': 2590,
81             },
82             'params': {
83                 # rtmp download
84                 'skip_download': True,
85             },
86         },
87     ]
88
89     _VIDEO_ID_REGEXES = [
90         r'<meta property="og:url" content="http://www\.ntv\.ru/video/(\d+)',
91         r'<video embed=[^>]+><id>(\d+)</id>',
92         r'<video restriction[^>]+><key>(\d+)</key>',
93     ]
94
95     def _real_extract(self, url):
96         mobj = re.match(self._VALID_URL, url)
97         video_id = mobj.group('id')
98
99         page = self._download_webpage(url, video_id)
100
101         video_id = self._html_search_regex(self._VIDEO_ID_REGEXES, page, 'video id')
102
103         player = self._download_xml('http://www.ntv.ru/vi%s/' % video_id, video_id, 'Downloading video XML')
104         title = unescapeHTML(player.find('./data/title').text)
105         description = unescapeHTML(player.find('./data/description').text)
106
107         video = player.find('./data/video')
108         video_id = video.find('./id').text
109         thumbnail = video.find('./splash').text
110         duration = int(video.find('./totaltime').text)
111         view_count = int(video.find('./views').text)
112         puid22 = video.find('./puid22').text
113
114         apps = {
115             '4': 'video1',
116             '7': 'video2',
117         }
118
119         app = apps.get(puid22, apps['4'])
120
121         formats = []
122         for format_id in ['', 'hi', 'webm']:
123             file = video.find('./%sfile' % format_id)
124             if file is None:
125                 continue
126             size = video.find('./%ssize' % format_id)
127             formats.append({
128                 'url': 'rtmp://media.ntv.ru/%s' % app,
129                 'app': app,
130                 'play_path': file.text,
131                 'rtmp_conn': 'B:1',
132                 'player_url': 'http://www.ntv.ru/swf/vps1.swf?update=20131128',
133                 'page_url': 'http://www.ntv.ru',
134                 'flash_ver': 'LNX 11,2,202,341',
135                 'rtmp_live': True,
136                 'ext': 'flv',
137                 'filesize': int(size.text),
138             })
139         self._sort_formats(formats)
140
141         return {
142             'id': video_id,
143             'title': title,
144             'description': description,
145             'thumbnail': thumbnail,
146             'duration': duration,
147             'view_count': view_count,
148             'formats': formats,
149         }