Merge pull request #9288 from reyyed/issue#9063fix
[youtube-dl] / youtube_dl / extractor / cda.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     decode_packed_codes,
9     ExtractorError,
10     parse_duration
11 )
12
13
14 class CDAIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)'
16     _TESTS = [{
17         'url': 'http://www.cda.pl/video/5749950c',
18         'md5': '6f844bf51b15f31fae165365707ae970',
19         'info_dict': {
20             'id': '5749950c',
21             'ext': 'mp4',
22             'height': 720,
23             'title': 'Oto dlaczego przed zakrętem należy zwolnić.',
24             'duration': 39
25         }
26     }, {
27         'url': 'http://www.cda.pl/video/57413289',
28         'md5': 'a88828770a8310fc00be6c95faf7f4d5',
29         'info_dict': {
30             'id': '57413289',
31             'ext': 'mp4',
32             'title': 'Lądowanie na lotnisku na Maderze',
33             'duration': 137
34         }
35     }, {
36         'url': 'http://ebd.cda.pl/0x0/5749950c',
37         'only_matching': True,
38     }]
39
40     def _real_extract(self, url):
41         video_id = self._match_id(url)
42         webpage = self._download_webpage('http://ebd.cda.pl/0x0/' + video_id, video_id)
43
44         if 'Ten film jest dostępny dla użytkowników premium' in webpage:
45             raise ExtractorError('This video is only available for premium users.', expected=True)
46
47         title = self._html_search_regex(r'<title>(.+?)</title>', webpage, 'title')
48
49         formats = []
50
51         info_dict = {
52             'id': video_id,
53             'title': title,
54             'formats': formats,
55             'duration': None,
56         }
57
58         def extract_format(page, version):
59             unpacked = decode_packed_codes(page)
60             format_url = self._search_regex(
61                 r"(?:file|url)\s*:\s*(\\?[\"'])(?P<url>http.+?)\1", unpacked,
62                 '%s url' % version, fatal=False, group='url')
63             if not format_url:
64                 return
65             f = {
66                 'url': format_url,
67             }
68             m = re.search(
69                 r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p',
70                 page)
71             if m:
72                 f.update({
73                     'format_id': m.group('format_id'),
74                     'height': int(m.group('height')),
75                 })
76             info_dict['formats'].append(f)
77             if not info_dict['duration']:
78                 info_dict['duration'] = parse_duration(self._search_regex(
79                     r"duration\s*:\s*(\\?[\"'])(?P<duration>.+?)\1",
80                     unpacked, 'duration', fatal=False, group='duration'))
81
82         extract_format(webpage, 'default')
83
84         for href, resolution in re.findall(
85                 r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)',
86                 webpage):
87             webpage = self._download_webpage(
88                 href, video_id, 'Downloading %s version information' % resolution, fatal=False)
89             if not webpage:
90                 # Manually report warning because empty page is returned when
91                 # invalid version is requested.
92                 self.report_warning('Unable to download %s version information' % resolution)
93                 continue
94             extract_format(webpage, resolution)
95
96         self._sort_formats(formats)
97
98         return info_dict