Merge branch 'akamai_pv' of https://github.com/remitamine/youtube-dl into remitamine...
[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"url:\\'(.+?)\\'", unpacked, '%s url' % version, fatal=False)
62             if not format_url:
63                 return
64             f = {
65                 'url': format_url,
66             }
67             m = re.search(
68                 r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p',
69                 page)
70             if m:
71                 f.update({
72                     'format_id': m.group('format_id'),
73                     'height': int(m.group('height')),
74                 })
75             info_dict['formats'].append(f)
76             if not info_dict['duration']:
77                 info_dict['duration'] = parse_duration(self._search_regex(
78                     r"duration:\\'(.+?)\\'", unpacked, 'duration', fatal=False))
79
80         extract_format(webpage, 'default')
81
82         for href, resolution in re.findall(
83                 r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)',
84                 webpage):
85             webpage = self._download_webpage(
86                 href, video_id, 'Downloading %s version information' % resolution, fatal=False)
87             if not webpage:
88                 # Manually report warning because empty page is returned when
89                 # invalid version is requested.
90                 self.report_warning('Unable to download %s version information' % resolution)
91                 continue
92             extract_format(webpage, resolution)
93
94         self._sort_formats(formats)
95
96         return info_dict