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