Add support for https for all extractors as preventive and future-proof measure
[youtube-dl] / youtube_dl / extractor / ntvru.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     clean_html,
7     xpath_text,
8     int_or_none,
9 )
10
11
12 class NTVRuIE(InfoExtractor):
13     IE_NAME = 'ntv.ru'
14     _VALID_URL = r'https?://(?:www\.)?ntv\.ru/(?P<id>.+)'
15
16     _TESTS = [
17         {
18             'url': 'http://www.ntv.ru/novosti/863142/',
19             'md5': 'ba7ea172a91cb83eb734cad18c10e723',
20             'info_dict': {
21                 'id': '746000',
22                 'ext': 'mp4',
23                 'title': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
24                 'description': 'Командующий Черноморским флотом провел переговоры в штабе ВМС Украины',
25                 'thumbnail': 're:^http://.*\.jpg',
26                 'duration': 136,
27             },
28         },
29         {
30             'url': 'http://www.ntv.ru/video/novosti/750370/',
31             'md5': 'adecff79691b4d71e25220a191477124',
32             'info_dict': {
33                 'id': '750370',
34                 'ext': 'mp4',
35                 'title': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
36                 'description': 'Родные пассажиров пропавшего Boeing не верят в трагический исход',
37                 'thumbnail': 're:^http://.*\.jpg',
38                 'duration': 172,
39             },
40         },
41         {
42             'url': 'http://www.ntv.ru/peredacha/segodnya/m23700/o232416',
43             'md5': '82dbd49b38e3af1d00df16acbeab260c',
44             'info_dict': {
45                 'id': '747480',
46                 'ext': 'mp4',
47                 'title': '«Сегодня». 21 марта 2014 года. 16:00',
48                 'description': '«Сегодня». 21 марта 2014 года. 16:00',
49                 'thumbnail': 're:^http://.*\.jpg',
50                 'duration': 1496,
51             },
52         },
53         {
54             'url': 'http://www.ntv.ru/kino/Koma_film',
55             'md5': 'f825770930937aa7e5aca0dc0d29319a',
56             'info_dict': {
57                 'id': '1007609',
58                 'ext': 'mp4',
59                 'title': 'Остросюжетный фильм «Кома»',
60                 'description': 'Остросюжетный фильм «Кома»',
61                 'thumbnail': 're:^http://.*\.jpg',
62                 'duration': 5592,
63             },
64         },
65         {
66             'url': 'http://www.ntv.ru/serial/Delo_vrachey/m31760/o233916/',
67             'md5': '9320cd0e23f3ea59c330dc744e06ff3b',
68             'info_dict': {
69                 'id': '751482',
70                 'ext': 'mp4',
71                 'title': '«Дело врачей»: «Деревце жизни»',
72                 'description': '«Дело врачей»: «Деревце жизни»',
73                 'thumbnail': 're:^http://.*\.jpg',
74                 'duration': 2590,
75             },
76         },
77     ]
78
79     _VIDEO_ID_REGEXES = [
80         r'<meta property="og:url" content="http://www\.ntv\.ru/video/(\d+)',
81         r'<video embed=[^>]+><id>(\d+)</id>',
82         r'<video restriction[^>]+><key>(\d+)</key>',
83     ]
84
85     def _real_extract(self, url):
86         video_id = self._match_id(url)
87
88         webpage = self._download_webpage(url, video_id)
89
90         video_id = self._html_search_regex(self._VIDEO_ID_REGEXES, webpage, 'video id')
91
92         player = self._download_xml(
93             'http://www.ntv.ru/vi%s/' % video_id,
94             video_id, 'Downloading video XML')
95         title = clean_html(xpath_text(player, './data/title', 'title', fatal=True))
96         description = clean_html(xpath_text(player, './data/description', 'description'))
97
98         video = player.find('./data/video')
99         video_id = xpath_text(video, './id', 'video id')
100         thumbnail = xpath_text(video, './splash', 'thumbnail')
101         duration = int_or_none(xpath_text(video, './totaltime', 'duration'))
102         view_count = int_or_none(xpath_text(video, './views', 'view count'))
103
104         token = self._download_webpage(
105             'http://stat.ntv.ru/services/access/token',
106             video_id, 'Downloading access token')
107
108         formats = []
109         for format_id in ['', 'hi', 'webm']:
110             file_ = video.find('./%sfile' % format_id)
111             if file_ is None:
112                 continue
113             size = video.find('./%ssize' % format_id)
114             formats.append({
115                 'url': 'http://media2.ntv.ru/vod/%s&tok=%s' % (file_.text, token),
116                 'filesize': int_or_none(size.text if size is not None else None),
117             })
118         self._sort_formats(formats)
119
120         return {
121             'id': video_id,
122             'title': title,
123             'description': description,
124             'thumbnail': thumbnail,
125             'duration': duration,
126             'view_count': view_count,
127             'formats': formats,
128         }