[ntvde] Add new extractor (Fixes #4850)
[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     IE_NAME = 'ntv.ru'
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         video_id = self._match_id(url)
97         page = self._download_webpage(url, video_id)
98
99         video_id = self._html_search_regex(self._VIDEO_ID_REGEXES, page, 'video id')
100
101         player = self._download_xml('http://www.ntv.ru/vi%s/' % video_id, video_id, 'Downloading video XML')
102         title = unescapeHTML(player.find('./data/title').text)
103         description = unescapeHTML(player.find('./data/description').text)
104
105         video = player.find('./data/video')
106         video_id = video.find('./id').text
107         thumbnail = video.find('./splash').text
108         duration = int(video.find('./totaltime').text)
109         view_count = int(video.find('./views').text)
110         puid22 = video.find('./puid22').text
111
112         apps = {
113             '4': 'video1',
114             '7': 'video2',
115         }
116
117         app = apps.get(puid22, apps['4'])
118
119         formats = []
120         for format_id in ['', 'hi', 'webm']:
121             file = video.find('./%sfile' % format_id)
122             if file is None:
123                 continue
124             size = video.find('./%ssize' % format_id)
125             formats.append({
126                 'url': 'rtmp://media.ntv.ru/%s' % app,
127                 'app': app,
128                 'play_path': file.text,
129                 'rtmp_conn': 'B:1',
130                 'player_url': 'http://www.ntv.ru/swf/vps1.swf?update=20131128',
131                 'page_url': 'http://www.ntv.ru',
132                 'flash_version': 'LNX 11,2,202,341',
133                 'rtmp_live': True,
134                 'ext': 'flv',
135                 'filesize': int(size.text),
136             })
137         self._sort_formats(formats)
138
139         return {
140             'id': video_id,
141             'title': title,
142             'description': description,
143             'thumbnail': thumbnail,
144             'duration': duration,
145             'view_count': view_count,
146             'formats': formats,
147         }