]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/cda.py
New upstream version 2017.03.26
[youtubedl] / youtube_dl / extractor / cda.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import codecs
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 float_or_none,
11 int_or_none,
12 parse_duration,
13 )
14
15
16 class CDAIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)'
18 _BASE_URL = 'http://www.cda.pl/'
19 _TESTS = [{
20 'url': 'http://www.cda.pl/video/5749950c',
21 'md5': '6f844bf51b15f31fae165365707ae970',
22 'info_dict': {
23 'id': '5749950c',
24 'ext': 'mp4',
25 'height': 720,
26 'title': 'Oto dlaczego przed zakrętem należy zwolnić.',
27 'description': 'md5:269ccd135d550da90d1662651fcb9772',
28 'thumbnail': r're:^https?://.*\.jpg$',
29 'average_rating': float,
30 'duration': 39
31 }
32 }, {
33 'url': 'http://www.cda.pl/video/57413289',
34 'md5': 'a88828770a8310fc00be6c95faf7f4d5',
35 'info_dict': {
36 'id': '57413289',
37 'ext': 'mp4',
38 'title': 'Lądowanie na lotnisku na Maderze',
39 'description': 'md5:60d76b71186dcce4e0ba6d4bbdb13e1a',
40 'thumbnail': r're:^https?://.*\.jpg$',
41 'uploader': 'crash404',
42 'view_count': int,
43 'average_rating': float,
44 'duration': 137
45 }
46 }, {
47 'url': 'http://ebd.cda.pl/0x0/5749950c',
48 'only_matching': True,
49 }]
50
51 def _real_extract(self, url):
52 video_id = self._match_id(url)
53 self._set_cookie('cda.pl', 'cda.player', 'html5')
54 webpage = self._download_webpage(
55 self._BASE_URL + '/video/' + video_id, video_id)
56
57 if 'Ten film jest dostępny dla użytkowników premium' in webpage:
58 raise ExtractorError('This video is only available for premium users.', expected=True)
59
60 formats = []
61
62 uploader = self._search_regex(r'''(?x)
63 <(span|meta)[^>]+itemprop=(["\'])author\2[^>]*>
64 (?:<\1[^>]*>[^<]*</\1>|(?!</\1>)(?:.|\n))*?
65 <(span|meta)[^>]+itemprop=(["\'])name\4[^>]*>(?P<uploader>[^<]+)</\3>
66 ''', webpage, 'uploader', default=None, group='uploader')
67 view_count = self._search_regex(
68 r'Odsłony:(?:\s|&nbsp;)*([0-9]+)', webpage,
69 'view_count', default=None)
70 average_rating = self._search_regex(
71 r'<(?:span|meta)[^>]+itemprop=(["\'])ratingValue\1[^>]*>(?P<rating_value>[0-9.]+)',
72 webpage, 'rating', fatal=False, group='rating_value')
73
74 info_dict = {
75 'id': video_id,
76 'title': self._og_search_title(webpage),
77 'description': self._og_search_description(webpage),
78 'uploader': uploader,
79 'view_count': int_or_none(view_count),
80 'average_rating': float_or_none(average_rating),
81 'thumbnail': self._og_search_thumbnail(webpage),
82 'formats': formats,
83 'duration': None,
84 }
85
86 def extract_format(page, version):
87 json_str = self._search_regex(
88 r'player_data=(\\?["\'])(?P<player_data>.+?)\1', page,
89 '%s player_json' % version, fatal=False, group='player_data')
90 if not json_str:
91 return
92 player_data = self._parse_json(
93 json_str, '%s player_data' % version, fatal=False)
94 if not player_data:
95 return
96 video = player_data.get('video')
97 if not video or 'file' not in video:
98 self.report_warning('Unable to extract %s version information' % version)
99 return
100 if video['file'].startswith('uggc'):
101 video['file'] = codecs.decode(video['file'], 'rot_13')
102 if video['file'].endswith('adc.mp4'):
103 video['file'] = video['file'].replace('adc.mp4', '.mp4')
104 f = {
105 'url': video['file'],
106 }
107 m = re.search(
108 r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p',
109 page)
110 if m:
111 f.update({
112 'format_id': m.group('format_id'),
113 'height': int(m.group('height')),
114 })
115 info_dict['formats'].append(f)
116 if not info_dict['duration']:
117 info_dict['duration'] = parse_duration(video.get('duration'))
118
119 extract_format(webpage, 'default')
120
121 for href, resolution in re.findall(
122 r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)',
123 webpage):
124 webpage = self._download_webpage(
125 self._BASE_URL + href, video_id,
126 'Downloading %s version information' % resolution, fatal=False)
127 if not webpage:
128 # Manually report warning because empty page is returned when
129 # invalid version is requested.
130 self.report_warning('Unable to download %s version information' % resolution)
131 continue
132 extract_format(webpage, resolution)
133
134 self._sort_formats(formats)
135
136 return info_dict