[youtube] Fix extraction.
[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 from .videomore import VideomoreIE
13
14
15 class CarambaTVIE(InfoExtractor):
16     _VALID_URL = r'(?:carambatv:|https?://video1\.carambatv\.ru/v/)(?P<id>\d+)'
17     _TESTS = [{
18         'url': 'http://video1.carambatv.ru/v/191910501',
19         'md5': '2f4a81b7cfd5ab866ee2d7270cb34a2a',
20         'info_dict': {
21             'id': '191910501',
22             'ext': 'mp4',
23             'title': '[BadComedian] - Разборка в Маниле (Абсолютный обзор)',
24             'thumbnail': r're:^https?://.*\.jpg',
25             'duration': 2678.31,
26         },
27     }, {
28         'url': 'carambatv:191910501',
29         'only_matching': True,
30     }]
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         video = self._download_json(
36             'http://video1.carambatv.ru/v/%s/videoinfo.js' % video_id,
37             video_id)
38
39         title = video['title']
40
41         base_url = video.get('video') or 'http://video1.carambatv.ru/v/%s/' % video_id
42
43         formats = [{
44             'url': base_url + f['fn'],
45             'height': int_or_none(f.get('height')),
46             'format_id': '%sp' % f['height'] if f.get('height') else None,
47         } for f in video['qualities'] if f.get('fn')]
48         self._sort_formats(formats)
49
50         thumbnail = video.get('splash')
51         duration = float_or_none(try_get(
52             video, lambda x: x['annotations'][0]['end_time'], compat_str))
53
54         return {
55             'id': video_id,
56             'title': title,
57             'thumbnail': thumbnail,
58             'duration': duration,
59             'formats': formats,
60         }
61
62
63 class CarambaTVPageIE(InfoExtractor):
64     _VALID_URL = r'https?://carambatv\.ru/(?:[^/]+/)+(?P<id>[^/?#&]+)'
65     _TEST = {
66         'url': 'http://carambatv.ru/movie/bad-comedian/razborka-v-manile/',
67         'md5': 'a49fb0ec2ad66503eeb46aac237d3c86',
68         'info_dict': {
69             'id': '475222',
70             'ext': 'flv',
71             'title': '[BadComedian] - Разборка в Маниле (Абсолютный обзор)',
72             'thumbnail': r're:^https?://.*\.jpg',
73             # duration reported by videomore is incorrect
74             'duration': int,
75         },
76         'add_ie': [VideomoreIE.ie_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         videomore_url = VideomoreIE._extract_url(webpage)
85         if not videomore_url:
86             videomore_id = self._search_regex(
87                 r'getVMCode\s*\(\s*["\']?(\d+)', webpage, 'videomore id',
88                 default=None)
89             if videomore_id:
90                 videomore_url = 'videomore:%s' % videomore_id
91         if videomore_url:
92             title = self._og_search_title(webpage)
93             return {
94                 '_type': 'url_transparent',
95                 'url': videomore_url,
96                 'ie_key': VideomoreIE.ie_key(),
97                 'title': title,
98             }
99
100         video_url = self._og_search_property('video:iframe', webpage, default=None)
101
102         if not video_url:
103             video_id = self._search_regex(
104                 r'(?:video_id|crmb_vuid)\s*[:=]\s*["\']?(\d+)',
105                 webpage, 'video id')
106             video_url = 'carambatv:%s' % video_id
107
108         return self.url_result(video_url, CarambaTVIE.ie_key())