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