Merge pull request #9288 from reyyed/issue#9063fix
[youtube-dl] / youtube_dl / extractor / carambatv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     float_or_none,
8     int_or_none,
9     try_get,
10 )
11
12
13 class CarambaTVIE(InfoExtractor):
14     _VALID_URL = r'(?:carambatv:|https?://video1\.carambatv\.ru/v/)(?P<id>\d+)'
15     _TESTS = [{
16         'url': 'http://video1.carambatv.ru/v/191910501',
17         'md5': '2f4a81b7cfd5ab866ee2d7270cb34a2a',
18         'info_dict': {
19             'id': '191910501',
20             'ext': 'mp4',
21             'title': '[BadComedian] - Разборка в Маниле (Абсолютный обзор)',
22             'thumbnail': 're:^https?://.*\.jpg',
23             'duration': 2678.31,
24         },
25     }, {
26         'url': 'carambatv:191910501',
27         'only_matching': True,
28     }]
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32
33         video = self._download_json(
34             'http://video1.carambatv.ru/v/%s/videoinfo.js' % video_id,
35             video_id)
36
37         title = video['title']
38
39         base_url = video.get('video') or 'http://video1.carambatv.ru/v/%s/' % video_id
40
41         formats = [{
42             'url': base_url + f['fn'],
43             'height': int_or_none(f.get('height')),
44             'format_id': '%sp' % f['height'] if f.get('height') else None,
45         } for f in video['qualities'] if f.get('fn')]
46         self._sort_formats(formats)
47
48         thumbnail = video.get('splash')
49         duration = float_or_none(try_get(
50             video, lambda x: x['annotations'][0]['end_time'], compat_str))
51
52         return {
53             'id': video_id,
54             'title': title,
55             'thumbnail': thumbnail,
56             'duration': duration,
57             'formats': formats,
58         }
59
60
61 class CarambaTVPageIE(InfoExtractor):
62     _VALID_URL = r'https?://carambatv\.ru/(?:[^/]+/)+(?P<id>[^/?#&]+)'
63     _TEST = {
64         'url': 'http://carambatv.ru/movie/bad-comedian/razborka-v-manile/',
65         'md5': '',
66         'info_dict': {
67             'id': '191910501',
68             'ext': 'mp4',
69             'title': '[BadComedian] - Разборка в Маниле (Абсолютный обзор)',
70             'thumbnail': 're:^https?://.*\.jpg$',
71             'duration': 2678.31,
72         },
73     }
74
75     def _real_extract(self, url):
76         video_id = self._match_id(url)
77
78         webpage = self._download_webpage(url, video_id)
79
80         video_url = self._og_search_property('video:iframe', webpage, default=None)
81
82         if not video_url:
83             video_id = self._search_regex(
84                 r'(?:video_id|crmb_vuid)\s*[:=]\s*["\']?(\d+)',
85                 webpage, 'video id')
86             video_url = 'carambatv:%s' % video_id
87
88         return self.url_result(video_url, CarambaTVIE.ie_key())