[pladform] Add extractor
[youtube-dl] / youtube_dl / extractor / pladform.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     int_or_none,
8     xpath_text,
9 )
10
11
12 class PladformIE(InfoExtractor):
13     _VALID_URL = r'''(?x)
14                     https?://
15                         (?:
16                             (?:
17                                 out\.pladform\.ru/player|
18                                 static\.pladform\.ru/player\.swf
19                             )
20                             \?.*\bvideoid=|
21                             video\.pladform\.ru/catalog/video/videoid/
22                         )
23                         (?P<id>\d+)
24                     '''
25     _TESTS = [{
26         # http://muz-tv.ru/kinozal/view/7400/
27         'url': 'http://out.pladform.ru/player?pl=24822&videoid=100183293',
28         'md5': '61f37b575dd27f1bb2e1854777fe31f4',
29         'info_dict': {
30             'id': '100183293',
31             'ext': 'mp4',
32             'title': 'Тайны перевала Дятлова • Тайна перевала Дятлова 1 серия 2 часть',
33             'description': 'Документальный сериал-расследование одной из самых жутких тайн ХХ века',
34             'thumbnail': 're:^https?://.*\.jpg$',
35             'duration': 694,
36             'age_limit': 0,
37         },
38     }, {
39         'url': 'http://static.pladform.ru/player.swf?pl=21469&videoid=100183293&vkcid=0',
40         'only_matching': True,
41     }, {
42         'url': 'http://video.pladform.ru/catalog/video/videoid/100183293/vkcid/0',
43         'only_matching': True,
44     }]
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48
49         video = self._download_xml(
50             'http://out.pladform.ru/getVideo?pl=1&videoid=%s' % video_id,
51             video_id)
52
53         if video.tag == 'error':
54             raise ExtractorError(
55                 '%s returned error: %s' % (self.IE_NAME, video.text),
56                 expected=True)
57
58         formats = [{
59             'url': src.text,
60             'format_id': src.get('quality'),
61         } for src in video.findall('./src')]
62         self._sort_formats(formats)
63
64         webpage = self._download_webpage(
65             'http://video.pladform.ru/catalog/video/videoid/%s' % video_id,
66             video_id)
67
68         title = self._og_search_title(webpage, fatal=False) or xpath_text(
69             video, './/title', 'title', fatal=True)
70         description = self._search_regex(
71             r'</h3>\s*<p>([^<]+)</p>', webpage, 'description', fatal=False)
72         thumbnail = self._og_search_thumbnail(webpage) or xpath_text(
73             video, './/cover', 'cover')
74
75         duration = int_or_none(xpath_text(video, './/time', 'duration'))
76         age_limit = int_or_none(xpath_text(video, './/age18', 'age limit'))
77
78         return {
79             'id': video_id,
80             'title': title,
81             'description': description,
82             'thumbnail': thumbnail,
83             'duration': duration,
84             'age_limit': age_limit,
85             'formats': formats,
86         }