]>
Commit | Line | Data |
---|---|---|
8b0d7a66 KM |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
fdeea726 | 4 | import codecs |
8b0d7a66 KM |
5 | import re |
6 | ||
7 | from .common import InfoExtractor | |
8 | from ..utils import ( | |
8b0d7a66 | 9 | ExtractorError, |
577281b0 KM |
10 | float_or_none, |
11 | int_or_none, | |
0c265486 | 12 | multipart_encode, |
577281b0 | 13 | parse_duration, |
0c265486 YCH |
14 | random_birthday, |
15 | urljoin, | |
8b0d7a66 KM |
16 | ) |
17 | ||
18 | ||
19 | class CDAIE(InfoExtractor): | |
f1ced6df | 20 | _VALID_URL = r'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)' |
577281b0 | 21 | _BASE_URL = 'http://www.cda.pl/' |
f1ced6df S |
22 | _TESTS = [{ |
23 | 'url': 'http://www.cda.pl/video/5749950c', | |
24 | 'md5': '6f844bf51b15f31fae165365707ae970', | |
25 | 'info_dict': { | |
26 | 'id': '5749950c', | |
27 | 'ext': 'mp4', | |
28 | 'height': 720, | |
29 | 'title': 'Oto dlaczego przed zakrętem należy zwolnić.', | |
577281b0 | 30 | 'description': 'md5:269ccd135d550da90d1662651fcb9772', |
ec85ded8 | 31 | 'thumbnail': r're:^https?://.*\.jpg$', |
577281b0 | 32 | 'average_rating': float, |
0c265486 YCH |
33 | 'duration': 39, |
34 | 'age_limit': 0, | |
f1ced6df S |
35 | } |
36 | }, { | |
37 | 'url': 'http://www.cda.pl/video/57413289', | |
38 | 'md5': 'a88828770a8310fc00be6c95faf7f4d5', | |
39 | 'info_dict': { | |
40 | 'id': '57413289', | |
41 | 'ext': 'mp4', | |
42 | 'title': 'Lądowanie na lotnisku na Maderze', | |
577281b0 | 43 | 'description': 'md5:60d76b71186dcce4e0ba6d4bbdb13e1a', |
ec85ded8 | 44 | 'thumbnail': r're:^https?://.*\.jpg$', |
577281b0 KM |
45 | 'uploader': 'crash404', |
46 | 'view_count': int, | |
47 | 'average_rating': float, | |
0c265486 YCH |
48 | 'duration': 137, |
49 | 'age_limit': 0, | |
8b0d7a66 | 50 | } |
0c265486 YCH |
51 | }, { |
52 | # Age-restricted | |
53 | 'url': 'http://www.cda.pl/video/1273454c4', | |
54 | 'info_dict': { | |
55 | 'id': '1273454c4', | |
56 | 'ext': 'mp4', | |
57 | 'title': 'Bronson (2008) napisy HD 1080p', | |
58 | 'description': 'md5:1b6cb18508daf2dc4e0fa4db77fec24c', | |
59 | 'height': 1080, | |
60 | 'uploader': 'boniek61', | |
61 | 'thumbnail': r're:^https?://.*\.jpg$', | |
62 | 'duration': 5554, | |
63 | 'age_limit': 18, | |
64 | 'view_count': int, | |
65 | 'average_rating': float, | |
66 | }, | |
f1ced6df S |
67 | }, { |
68 | 'url': 'http://ebd.cda.pl/0x0/5749950c', | |
69 | 'only_matching': True, | |
70 | }] | |
8b0d7a66 | 71 | |
0c265486 YCH |
72 | def _download_age_confirm_page(self, url, video_id, *args, **kwargs): |
73 | form_data = random_birthday('rok', 'miesiac', 'dzien') | |
74 | form_data.update({'return': url, 'module': 'video', 'module_id': video_id}) | |
75 | data, content_type = multipart_encode(form_data) | |
76 | return self._download_webpage( | |
77 | urljoin(url, '/a/validatebirth'), video_id, *args, | |
78 | data=data, headers={ | |
79 | 'Referer': url, | |
80 | 'Content-Type': content_type, | |
81 | }, **kwargs) | |
82 | ||
8b0d7a66 KM |
83 | def _real_extract(self, url): |
84 | video_id = self._match_id(url) | |
577281b0 KM |
85 | self._set_cookie('cda.pl', 'cda.player', 'html5') |
86 | webpage = self._download_webpage( | |
87 | self._BASE_URL + '/video/' + video_id, video_id) | |
8b0d7a66 KM |
88 | |
89 | if 'Ten film jest dostępny dla użytkowników premium' in webpage: | |
90 | raise ExtractorError('This video is only available for premium users.', expected=True) | |
91 | ||
0c265486 YCH |
92 | need_confirm_age = False |
93 | if self._html_search_regex(r'(<form[^>]+action="/a/validatebirth")', | |
94 | webpage, 'birthday validate form', default=None): | |
95 | webpage = self._download_age_confirm_page( | |
96 | url, video_id, note='Confirming age') | |
97 | need_confirm_age = True | |
98 | ||
8b0d7a66 KM |
99 | formats = [] |
100 | ||
577281b0 KM |
101 | uploader = self._search_regex(r'''(?x) |
102 | <(span|meta)[^>]+itemprop=(["\'])author\2[^>]*> | |
103 | (?:<\1[^>]*>[^<]*</\1>|(?!</\1>)(?:.|\n))*? | |
104 | <(span|meta)[^>]+itemprop=(["\'])name\4[^>]*>(?P<uploader>[^<]+)</\3> | |
105 | ''', webpage, 'uploader', default=None, group='uploader') | |
106 | view_count = self._search_regex( | |
107 | r'Odsłony:(?:\s| )*([0-9]+)', webpage, | |
108 | 'view_count', default=None) | |
109 | average_rating = self._search_regex( | |
110 | r'<(?:span|meta)[^>]+itemprop=(["\'])ratingValue\1[^>]*>(?P<rating_value>[0-9.]+)', | |
111 | webpage, 'rating', fatal=False, group='rating_value') | |
112 | ||
f1ced6df S |
113 | info_dict = { |
114 | 'id': video_id, | |
577281b0 KM |
115 | 'title': self._og_search_title(webpage), |
116 | 'description': self._og_search_description(webpage), | |
117 | 'uploader': uploader, | |
118 | 'view_count': int_or_none(view_count), | |
119 | 'average_rating': float_or_none(average_rating), | |
120 | 'thumbnail': self._og_search_thumbnail(webpage), | |
f1ced6df S |
121 | 'formats': formats, |
122 | 'duration': None, | |
0c265486 | 123 | 'age_limit': 18 if need_confirm_age else 0, |
f1ced6df | 124 | } |
8b0d7a66 | 125 | |
f1ced6df | 126 | def extract_format(page, version): |
f8f18f33 | 127 | json_str = self._html_search_regex( |
577281b0 KM |
128 | r'player_data=(\\?["\'])(?P<player_data>.+?)\1', page, |
129 | '%s player_json' % version, fatal=False, group='player_data') | |
130 | if not json_str: | |
131 | return | |
132 | player_data = self._parse_json( | |
133 | json_str, '%s player_data' % version, fatal=False) | |
134 | if not player_data: | |
135 | return | |
136 | video = player_data.get('video') | |
137 | if not video or 'file' not in video: | |
138 | self.report_warning('Unable to extract %s version information' % version) | |
f1ced6df | 139 | return |
fdeea726 AS |
140 | if video['file'].startswith('uggc'): |
141 | video['file'] = codecs.decode(video['file'], 'rot_13') | |
142 | if video['file'].endswith('adc.mp4'): | |
143 | video['file'] = video['file'].replace('adc.mp4', '.mp4') | |
f1ced6df | 144 | f = { |
577281b0 | 145 | 'url': video['file'], |
f1ced6df S |
146 | } |
147 | m = re.search( | |
148 | r'<a[^>]+data-quality="(?P<format_id>[^"]+)"[^>]+href="[^"]+"[^>]+class="[^"]*quality-btn-active[^"]*">(?P<height>[0-9]+)p', | |
149 | page) | |
150 | if m: | |
151 | f.update({ | |
152 | 'format_id': m.group('format_id'), | |
153 | 'height': int(m.group('height')), | |
154 | }) | |
155 | info_dict['formats'].append(f) | |
156 | if not info_dict['duration']: | |
577281b0 | 157 | info_dict['duration'] = parse_duration(video.get('duration')) |
f1ced6df S |
158 | |
159 | extract_format(webpage, 'default') | |
160 | ||
161 | for href, resolution in re.findall( | |
162 | r'<a[^>]+data-quality="[^"]+"[^>]+href="([^"]+)"[^>]+class="quality-btn"[^>]*>([0-9]+p)', | |
163 | webpage): | |
0c265486 YCH |
164 | if need_confirm_age: |
165 | handler = self._download_age_confirm_page | |
166 | else: | |
167 | handler = self._download_webpage | |
168 | ||
169 | webpage = handler( | |
577281b0 KM |
170 | self._BASE_URL + href, video_id, |
171 | 'Downloading %s version information' % resolution, fatal=False) | |
8b0d7a66 | 172 | if not webpage: |
f1ced6df S |
173 | # Manually report warning because empty page is returned when |
174 | # invalid version is requested. | |
175 | self.report_warning('Unable to download %s version information' % resolution) | |
8b0d7a66 | 176 | continue |
0c265486 | 177 | |
f1ced6df | 178 | extract_format(webpage, resolution) |
8b0d7a66 KM |
179 | |
180 | self._sort_formats(formats) | |
181 | ||
f1ced6df | 182 | return info_dict |