]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
38a40276 | 2 | from __future__ import unicode_literals |
3 | ||
34440095 | 4 | import re |
1d430674 | 5 | import json |
34440095 S |
6 | import base64 |
7 | import zlib | |
8 | ||
c8434e83 | 9 | from hashlib import sha1 |
10 | from math import pow, sqrt, floor | |
b5857f62 | 11 | from .common import InfoExtractor |
1cc79574 | 12 | from ..compat import ( |
36e6f62c | 13 | compat_etree_fromstring, |
15707c7e | 14 | compat_urllib_parse_urlencode, |
c8434e83 | 15 | compat_urllib_request, |
a01da8bb | 16 | compat_urlparse, |
1cc79574 PH |
17 | ) |
18 | from ..utils import ( | |
19 | ExtractorError, | |
c8434e83 | 20 | bytes_to_intlist, |
21 | intlist_to_bytes, | |
725d1c58 | 22 | int_or_none, |
6d02b9a3 | 23 | lowercase_escape, |
a01da8bb | 24 | remove_end, |
5c2266df | 25 | sanitized_Request, |
c8434e83 | 26 | unified_strdate, |
723e04d0 | 27 | urlencode_postdata, |
725d1c58 | 28 | xpath_text, |
065216d9 | 29 | extract_attributes, |
c8434e83 | 30 | ) |
31 | from ..aes import ( | |
32 | aes_cbc_decrypt, | |
c8434e83 | 33 | ) |
34 | ||
34440095 | 35 | |
12810c9c | 36 | class CrunchyrollBaseIE(InfoExtractor): |
eb5b1fc0 S |
37 | _LOGIN_URL = 'https://www.crunchyroll.com/login' |
38 | _LOGIN_FORM = 'login_form' | |
80f48920 S |
39 | _NETRC_MACHINE = 'crunchyroll' |
40 | ||
41 | def _login(self): | |
42 | (username, password) = self._get_login_info() | |
43 | if username is None: | |
44 | return | |
eb5b1fc0 S |
45 | |
46 | login_page = self._download_webpage( | |
47 | self._LOGIN_URL, None, 'Downloading login page') | |
48 | ||
70b4cf9b S |
49 | def is_logged(webpage): |
50 | return '<title>Redirecting' in webpage | |
51 | ||
52 | # Already logged in | |
53 | if is_logged(login_page): | |
54 | return | |
55 | ||
eb5b1fc0 S |
56 | login_form_str = self._search_regex( |
57 | r'(?P<form><form[^>]+?id=(["\'])%s\2[^>]*>)' % self._LOGIN_FORM, | |
58 | login_page, 'login form', group='form') | |
59 | ||
60 | post_url = extract_attributes(login_form_str).get('action') | |
61 | if not post_url: | |
62 | post_url = self._LOGIN_URL | |
63 | elif not post_url.startswith('http'): | |
64 | post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) | |
65 | ||
66 | login_form = self._form_hidden_inputs(self._LOGIN_FORM, login_page) | |
67 | ||
68 | login_form.update({ | |
69 | 'login_form[name]': username, | |
70 | 'login_form[password]': password, | |
80f48920 | 71 | }) |
eb5b1fc0 S |
72 | |
73 | response = self._download_webpage( | |
74 | post_url, None, 'Logging in', 'Wrong login info', | |
75 | data=urlencode_postdata(login_form), | |
76 | headers={'Content-Type': 'application/x-www-form-urlencoded'}) | |
77 | ||
78 | # Successful login | |
70b4cf9b | 79 | if is_logged(response): |
eb5b1fc0 S |
80 | return |
81 | ||
82 | error = self._html_search_regex( | |
83 | '(?s)<ul[^>]+class=["\']messages["\'][^>]*>(.+?)</ul>', | |
84 | response, 'error message', default=None) | |
85 | if error: | |
86 | raise ExtractorError('Unable to login: %s' % error, expected=True) | |
87 | ||
88 | raise ExtractorError('Unable to log in') | |
80f48920 S |
89 | |
90 | def _real_initialize(self): | |
91 | self._login() | |
92 | ||
cc162f6a | 93 | def _download_webpage(self, url_or_request, *args, **kwargs): |
12810c9c | 94 | request = (url_or_request if isinstance(url_or_request, compat_urllib_request.Request) |
5c2266df | 95 | else sanitized_Request(url_or_request)) |
12810c9c S |
96 | # Accept-Language must be set explicitly to accept any language to avoid issues |
97 | # similar to https://github.com/rg3/youtube-dl/issues/6797. | |
98 | # Along with IP address Crunchyroll uses Accept-Language to guess whether georestriction | |
99 | # should be imposed or not (from what I can see it just takes the first language | |
100 | # ignoring the priority and requires it to correspond the IP). By the way this causes | |
101 | # Crunchyroll to not work in georestriction cases in some browsers that don't place | |
102 | # the locale lang first in header. However allowing any language seems to workaround the issue. | |
103 | request.add_header('Accept-Language', '*') | |
cc162f6a | 104 | return super(CrunchyrollBaseIE, self)._download_webpage(request, *args, **kwargs) |
12810c9c | 105 | |
80f48920 S |
106 | @staticmethod |
107 | def _add_skip_wall(url): | |
108 | parsed_url = compat_urlparse.urlparse(url) | |
109 | qs = compat_urlparse.parse_qs(parsed_url.query) | |
110 | # Always force skip_wall to bypass maturity wall, namely 18+ confirmation message: | |
111 | # > This content may be inappropriate for some people. | |
112 | # > Are you sure you want to continue? | |
113 | # since it's not disabled by default in crunchyroll account's settings. | |
114 | # See https://github.com/rg3/youtube-dl/issues/7202. | |
115 | qs['skip_wall'] = ['1'] | |
116 | return compat_urlparse.urlunparse( | |
15707c7e | 117 | parsed_url._replace(query=compat_urllib_parse_urlencode(qs, True))) |
80f48920 | 118 | |
12810c9c S |
119 | |
120 | class CrunchyrollIE(CrunchyrollBaseIE): | |
ede21449 | 121 | _VALID_URL = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.(?:com|fr)/(?:media(?:-|/\?id=)|[^/]*/[^/?&]*?)(?P<video_id>[0-9]+))(?:[/?&]|$)' |
a8896c5a | 122 | _TESTS = [{ |
38a40276 | 123 | 'url': 'http://www.crunchyroll.com/wanna-be-the-strongest-in-the-world/episode-1-an-idol-wrestler-is-born-645513', |
38a40276 | 124 | 'info_dict': { |
34440095 S |
125 | 'id': '645513', |
126 | 'ext': 'flv', | |
38a40276 | 127 | 'title': 'Wanna be the Strongest in the World Episode 1 – An Idol-Wrestler is Born!', |
128 | 'description': 'md5:2d17137920c64f2f49981a7797d275ef', | |
129 | 'thumbnail': 'http://img1.ak.crunchyroll.com/i/spire1-tmb/20c6b5e10f1a47b10516877d3c039cae1380951166_full.jpg', | |
130 | 'uploader': 'Yomiuri Telecasting Corporation (YTV)', | |
131 | 'upload_date': '20131013', | |
b1edd7a4 | 132 | 'url': 're:(?!.*&)', |
c8434e83 | 133 | }, |
38a40276 | 134 | 'params': { |
c8434e83 | 135 | # rtmp |
38a40276 | 136 | 'skip_download': True, |
c8434e83 | 137 | }, |
ede21449 S |
138 | }, { |
139 | 'url': 'http://www.crunchyroll.com/media-589804/culture-japan-1', | |
140 | 'info_dict': { | |
141 | 'id': '589804', | |
142 | 'ext': 'flv', | |
143 | 'title': 'Culture Japan Episode 1 – Rebuilding Japan after the 3.11', | |
6d02b9a3 | 144 | 'description': 'md5:2fbc01f90b87e8e9137296f37b461c12', |
ede21449 S |
145 | 'thumbnail': 're:^https?://.*\.jpg$', |
146 | 'uploader': 'Danny Choo Network', | |
147 | 'upload_date': '20120213', | |
148 | }, | |
149 | 'params': { | |
150 | # rtmp | |
151 | 'skip_download': True, | |
152 | }, | |
77c5b98d | 153 | 'skip': 'Video gone', |
990d533e S |
154 | }, { |
155 | 'url': 'http://www.crunchyroll.com/rezero-starting-life-in-another-world-/episode-5-the-morning-of-our-promise-is-still-distant-702409', | |
156 | 'info_dict': { | |
157 | 'id': '702409', | |
158 | 'ext': 'mp4', | |
159 | 'title': 'Re:ZERO -Starting Life in Another World- Episode 5 – The Morning of Our Promise Is Still Distant', | |
160 | 'description': 'md5:97664de1ab24bbf77a9c01918cb7dca9', | |
161 | 'thumbnail': 're:^https?://.*\.jpg$', | |
162 | 'uploader': 'TV TOKYO', | |
163 | 'upload_date': '20160508', | |
164 | }, | |
165 | 'params': { | |
166 | # m3u8 download | |
167 | 'skip_download': True, | |
168 | }, | |
a8896c5a S |
169 | }, { |
170 | 'url': 'http://www.crunchyroll.fr/girl-friend-beta/episode-11-goodbye-la-mode-661697', | |
171 | 'only_matching': True, | |
49941c4e S |
172 | }, { |
173 | # geo-restricted (US), 18+ maturity wall, non-premium available | |
174 | 'url': 'http://www.crunchyroll.com/cosplay-complex-ova/episode-1-the-birth-of-the-cosplay-club-565617', | |
175 | 'only_matching': True, | |
a8896c5a | 176 | }] |
c8434e83 | 177 | |
178 | _FORMAT_IDS = { | |
38a40276 | 179 | '360': ('60', '106'), |
180 | '480': ('61', '106'), | |
181 | '720': ('62', '106'), | |
182 | '1080': ('80', '108'), | |
c8434e83 | 183 | } |
184 | ||
185 | def _decrypt_subtitles(self, data, iv, id): | |
1a5b77dc S |
186 | data = bytes_to_intlist(base64.b64decode(data.encode('utf-8'))) |
187 | iv = bytes_to_intlist(base64.b64decode(iv.encode('utf-8'))) | |
c8434e83 | 188 | id = int(id) |
189 | ||
190 | def obfuscate_key_aux(count, modulo, start): | |
191 | output = list(start) | |
192 | for _ in range(count): | |
193 | output.append(output[-1] + output[-2]) | |
194 | # cut off start values | |
195 | output = output[2:] | |
196 | output = list(map(lambda x: x % modulo + 33, output)) | |
197 | return output | |
198 | ||
199 | def obfuscate_key(key): | |
200 | num1 = int(floor(pow(2, 25) * sqrt(6.9))) | |
201 | num2 = (num1 ^ key) << 5 | |
202 | num3 = key ^ num1 | |
203 | num4 = num3 ^ (num3 >> 3) ^ num2 | |
204 | prefix = intlist_to_bytes(obfuscate_key_aux(20, 97, (1, 2))) | |
38a40276 | 205 | shaHash = bytes_to_intlist(sha1(prefix + str(num4).encode('ascii')).digest()) |
c8434e83 | 206 | # Extend 160 Bit hash to 256 Bit |
207 | return shaHash + [0] * 12 | |
34440095 | 208 | |
c8434e83 | 209 | key = obfuscate_key(id) |
5f6a1245 | 210 | |
c8434e83 | 211 | decrypted_data = intlist_to_bytes(aes_cbc_decrypt(data, key, iv)) |
212 | return zlib.decompress(decrypted_data) | |
213 | ||
d65d6286 | 214 | def _convert_subtitles_to_srt(self, sub_root): |
38a40276 | 215 | output = '' |
d65d6286 JMF |
216 | |
217 | for i, event in enumerate(sub_root.findall('./events/event'), 1): | |
218 | start = event.attrib['start'].replace('.', ',') | |
219 | end = event.attrib['end'].replace('.', ',') | |
220 | text = event.attrib['text'].replace('\\N', '\n') | |
38a40276 | 221 | output += '%d\n%s --> %s\n%s\n\n' % (i, start, end, text) |
c8434e83 | 222 | return output |
223 | ||
d65d6286 | 224 | def _convert_subtitles_to_ass(self, sub_root): |
78272a07 A |
225 | output = '' |
226 | ||
227 | def ass_bool(strvalue): | |
228 | assvalue = '0' | |
229 | if strvalue == '1': | |
230 | assvalue = '-1' | |
231 | return assvalue | |
232 | ||
78272a07 | 233 | output = '[Script Info]\n' |
611c1dd9 | 234 | output += 'Title: %s\n' % sub_root.attrib['title'] |
78272a07 | 235 | output += 'ScriptType: v4.00+\n' |
611c1dd9 S |
236 | output += 'WrapStyle: %s\n' % sub_root.attrib['wrap_style'] |
237 | output += 'PlayResX: %s\n' % sub_root.attrib['play_res_x'] | |
238 | output += 'PlayResY: %s\n' % sub_root.attrib['play_res_y'] | |
6cbb20bb | 239 | output += """ScaledBorderAndShadow: no |
78272a07 A |
240 | |
241 | [V4+ Styles] | |
242 | Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding | |
243 | """ | |
244 | for style in sub_root.findall('./styles/style'): | |
611c1dd9 S |
245 | output += 'Style: ' + style.attrib['name'] |
246 | output += ',' + style.attrib['font_name'] | |
247 | output += ',' + style.attrib['font_size'] | |
248 | output += ',' + style.attrib['primary_colour'] | |
249 | output += ',' + style.attrib['secondary_colour'] | |
250 | output += ',' + style.attrib['outline_colour'] | |
251 | output += ',' + style.attrib['back_colour'] | |
252 | output += ',' + ass_bool(style.attrib['bold']) | |
253 | output += ',' + ass_bool(style.attrib['italic']) | |
254 | output += ',' + ass_bool(style.attrib['underline']) | |
255 | output += ',' + ass_bool(style.attrib['strikeout']) | |
256 | output += ',' + style.attrib['scale_x'] | |
257 | output += ',' + style.attrib['scale_y'] | |
258 | output += ',' + style.attrib['spacing'] | |
259 | output += ',' + style.attrib['angle'] | |
260 | output += ',' + style.attrib['border_style'] | |
261 | output += ',' + style.attrib['outline'] | |
262 | output += ',' + style.attrib['shadow'] | |
263 | output += ',' + style.attrib['alignment'] | |
264 | output += ',' + style.attrib['margin_l'] | |
265 | output += ',' + style.attrib['margin_r'] | |
266 | output += ',' + style.attrib['margin_v'] | |
267 | output += ',' + style.attrib['encoding'] | |
78272a07 A |
268 | output += '\n' |
269 | ||
270 | output += """ | |
271 | [Events] | |
272 | Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text | |
273 | """ | |
274 | for event in sub_root.findall('./events/event'): | |
275 | output += 'Dialogue: 0' | |
611c1dd9 S |
276 | output += ',' + event.attrib['start'] |
277 | output += ',' + event.attrib['end'] | |
278 | output += ',' + event.attrib['style'] | |
279 | output += ',' + event.attrib['name'] | |
280 | output += ',' + event.attrib['margin_l'] | |
281 | output += ',' + event.attrib['margin_r'] | |
282 | output += ',' + event.attrib['margin_v'] | |
283 | output += ',' + event.attrib['effect'] | |
284 | output += ',' + event.attrib['text'] | |
78272a07 A |
285 | output += '\n' |
286 | ||
287 | return output | |
288 | ||
0385d642 | 289 | def _extract_subtitles(self, subtitle): |
36e6f62c | 290 | sub_root = compat_etree_fromstring(subtitle) |
0385d642 S |
291 | return [{ |
292 | 'ext': 'srt', | |
293 | 'data': self._convert_subtitles_to_srt(sub_root), | |
294 | }, { | |
295 | 'ext': 'ass', | |
296 | 'data': self._convert_subtitles_to_ass(sub_root), | |
297 | }] | |
298 | ||
b5857f62 JMF |
299 | def _get_subtitles(self, video_id, webpage): |
300 | subtitles = {} | |
76907875 | 301 | for sub_id, sub_name in re.findall(r'\bssid=([0-9]+)"[^>]+?\btitle="([^"]+)', webpage): |
b5857f62 JMF |
302 | sub_page = self._download_webpage( |
303 | 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + sub_id, | |
304 | video_id, note='Downloading subtitles for ' + sub_name) | |
305 | id = self._search_regex(r'id=\'([0-9]+)', sub_page, 'subtitle_id', fatal=False) | |
306 | iv = self._search_regex(r'<iv>([^<]+)', sub_page, 'subtitle_iv', fatal=False) | |
307 | data = self._search_regex(r'<data>([^<]+)', sub_page, 'subtitle_data', fatal=False) | |
308 | if not id or not iv or not data: | |
309 | continue | |
b5857f62 JMF |
310 | subtitle = self._decrypt_subtitles(data, iv, id).decode('utf-8') |
311 | lang_code = self._search_regex(r'lang_code=["\']([^"\']+)', subtitle, 'subtitle_lang_code', fatal=False) | |
312 | if not lang_code: | |
313 | continue | |
0385d642 | 314 | subtitles[lang_code] = self._extract_subtitles(subtitle) |
b5857f62 JMF |
315 | return subtitles |
316 | ||
5f6a1245 | 317 | def _real_extract(self, url): |
c8434e83 | 318 | mobj = re.match(self._VALID_URL, url) |
38a40276 | 319 | video_id = mobj.group('video_id') |
320 | ||
321 | if mobj.group('prefix') == 'm': | |
322 | mobile_webpage = self._download_webpage(url, video_id, 'Downloading mobile webpage') | |
323 | webpage_url = self._search_regex(r'<link rel="canonical" href="([^"]+)" />', mobile_webpage, 'webpage_url') | |
324 | else: | |
325 | webpage_url = 'http://www.' + mobj.group('url') | |
c8434e83 | 326 | |
80f48920 | 327 | webpage = self._download_webpage(self._add_skip_wall(webpage_url), video_id, 'Downloading webpage') |
2f72e83b S |
328 | note_m = self._html_search_regex( |
329 | r'<div class="showmedia-trailer-notice">(.+?)</div>', | |
330 | webpage, 'trailer-notice', default='') | |
c8434e83 | 331 | if note_m: |
332 | raise ExtractorError(note_m) | |
333 | ||
1d430674 S |
334 | mobj = re.search(r'Page\.messaging_box_controller\.addItems\(\[(?P<msg>{.+?})\]\)', webpage) |
335 | if mobj: | |
336 | msg = json.loads(mobj.group('msg')) | |
337 | if msg.get('type') == 'error': | |
338 | raise ExtractorError('crunchyroll returned error: %s' % msg['message_body'], expected=True) | |
2f72e83b S |
339 | |
340 | if 'To view this, please log in to verify you are 18 or older.' in webpage: | |
39affb5a | 341 | self.raise_login_required() |
1d430674 | 342 | |
5214f1e3 S |
343 | video_title = self._html_search_regex( |
344 | r'(?s)<h1[^>]*>((?:(?!<h1).)*?<span[^>]+itemprop=["\']title["\'][^>]*>(?:(?!<h1).)+?)</h1>', | |
345 | webpage, 'video_title') | |
38a40276 | 346 | video_title = re.sub(r' {2,}', ' ', video_title) |
2c740cf2 | 347 | video_description = self._html_search_regex( |
6d02b9a3 S |
348 | r'<script[^>]*>\s*.+?\[media_id=%s\].+?"description"\s*:\s*"([^"]+)' % video_id, |
349 | webpage, 'description', default=None) | |
350 | if video_description: | |
351 | video_description = lowercase_escape(video_description.replace(r'\r\n', '\n')) | |
47004d95 S |
352 | video_upload_date = self._html_search_regex( |
353 | [r'<div>Availability for free users:(.+?)</div>', r'<div>[^<>]+<span>\s*(.+?\d{4})\s*</span></div>'], | |
354 | webpage, 'video_upload_date', fatal=False, flags=re.DOTALL) | |
c8434e83 | 355 | if video_upload_date: |
356 | video_upload_date = unified_strdate(video_upload_date) | |
47004d95 S |
357 | video_uploader = self._html_search_regex( |
358 | r'<a[^>]+href="/publisher/[^"]+"[^>]*>([^<]+)</a>', webpage, | |
359 | 'video_uploader', fatal=False) | |
c8434e83 | 360 | |
065216d9 | 361 | available_fmts = [] |
6ff44695 | 362 | for a, fmt in re.findall(r'(<a[^>]+token=["\']showmedia\.([0-9]{3,4})p["\'][^>]+>)', webpage): |
065216d9 | 363 | attrs = extract_attributes(a) |
364 | href = attrs.get('href') | |
365 | if href and '/freetrial' in href: | |
366 | continue | |
367 | available_fmts.append(fmt) | |
368 | if not available_fmts: | |
8312b1a3 S |
369 | for p in (r'token=["\']showmedia\.([0-9]{3,4})p"', r'showmedia\.([0-9]{3,4})p'): |
370 | available_fmts = re.findall(p, webpage) | |
371 | if available_fmts: | |
372 | break | |
e757fb3d | 373 | video_encode_ids = [] |
065216d9 | 374 | formats = [] |
375 | for fmt in available_fmts: | |
c8434e83 | 376 | stream_quality, stream_format = self._FORMAT_IDS[fmt] |
2514d263 | 377 | video_format = fmt + 'p' |
5c2266df | 378 | streamdata_req = sanitized_Request( |
ede21449 | 379 | 'http://www.crunchyroll.com/xml/?req=RpcApiVideoPlayer_GetStandardConfig&media_id=%s&video_format=%s&video_quality=%s' |
e757fb3d | 380 | % (video_id, stream_format, stream_quality), |
15707c7e | 381 | compat_urllib_parse_urlencode({'current_page': url}).encode('utf-8')) |
38a40276 | 382 | streamdata_req.add_header('Content-Type', 'application/x-www-form-urlencoded') |
b1edd7a4 PH |
383 | streamdata = self._download_xml( |
384 | streamdata_req, video_id, | |
385 | note='Downloading media info for %s' % video_format) | |
ede21449 | 386 | stream_info = streamdata.find('./{default}preload/stream_info') |
e757fb3d | 387 | video_encode_id = xpath_text(stream_info, './video_encode_id') |
388 | if video_encode_id in video_encode_ids: | |
389 | continue | |
390 | video_encode_ids.append(video_encode_id) | |
990d533e S |
391 | |
392 | video_file = xpath_text(stream_info, './file') | |
393 | if not video_file: | |
394 | continue | |
395 | if video_file.startswith('http'): | |
396 | formats.extend(self._extract_m3u8_formats( | |
397 | video_file, video_id, 'mp4', entry_protocol='m3u8_native', | |
398 | m3u8_id='hls', fatal=False)) | |
399 | continue | |
400 | ||
e4f49a87 | 401 | video_url = xpath_text(stream_info, './host') |
990d533e | 402 | if not video_url: |
e4f49a87 | 403 | continue |
725d1c58 JMF |
404 | metadata = stream_info.find('./metadata') |
405 | format_info = { | |
406 | 'format': video_format, | |
407 | 'format_id': video_format, | |
408 | 'height': int_or_none(xpath_text(metadata, './height')), | |
409 | 'width': int_or_none(xpath_text(metadata, './width')), | |
410 | } | |
a01da8bb S |
411 | |
412 | if '.fplive.net/' in video_url: | |
413 | video_url = re.sub(r'^rtmpe?://', 'http://', video_url.strip()) | |
414 | parsed_video_url = compat_urlparse.urlparse(video_url) | |
415 | direct_video_url = compat_urlparse.urlunparse(parsed_video_url._replace( | |
416 | netloc='v.lvlt.crcdn.net', | |
990d533e | 417 | path='%s/%s' % (remove_end(parsed_video_url.path, '/'), video_file.split(':')[-1]))) |
a01da8bb | 418 | if self._is_valid_url(direct_video_url, video_id, video_format): |
725d1c58 | 419 | format_info.update({ |
a01da8bb | 420 | 'url': direct_video_url, |
a01da8bb | 421 | }) |
725d1c58 | 422 | formats.append(format_info) |
a01da8bb S |
423 | continue |
424 | ||
725d1c58 | 425 | format_info.update({ |
38a40276 | 426 | 'url': video_url, |
990d533e | 427 | 'play_path': video_file, |
38a40276 | 428 | 'ext': 'flv', |
c8434e83 | 429 | }) |
725d1c58 | 430 | formats.append(format_info) |
00a17a9e | 431 | self._sort_formats(formats) |
c8434e83 | 432 | |
e757fb3d | 433 | metadata = self._download_xml( |
434 | 'http://www.crunchyroll.com/xml', video_id, | |
435 | note='Downloading media info', query={ | |
436 | 'req': 'RpcApiVideoPlayer_GetMediaMetadata', | |
437 | 'media_id': video_id, | |
438 | }) | |
439 | ||
b5857f62 | 440 | subtitles = self.extract_subtitles(video_id, webpage) |
11b3ce85 | 441 | |
c8434e83 | 442 | return { |
8bcc8756 JW |
443 | 'id': video_id, |
444 | 'title': video_title, | |
38a40276 | 445 | 'description': video_description, |
e757fb3d | 446 | 'thumbnail': xpath_text(metadata, 'episode_image_url'), |
8bcc8756 | 447 | 'uploader': video_uploader, |
38a40276 | 448 | 'upload_date': video_upload_date, |
e757fb3d | 449 | 'series': xpath_text(metadata, 'series_title'), |
450 | 'episode': xpath_text(metadata, 'episode_title'), | |
451 | 'episode_number': int_or_none(xpath_text(metadata, 'episode_number')), | |
8bcc8756 JW |
452 | 'subtitles': subtitles, |
453 | 'formats': formats, | |
d0a72674 | 454 | } |
8230018c GS |
455 | |
456 | ||
12810c9c | 457 | class CrunchyrollShowPlaylistIE(CrunchyrollBaseIE): |
611c1dd9 | 458 | IE_NAME = 'crunchyroll:playlist' |
80f48920 | 459 | _VALID_URL = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.com/(?!(?:news|anime-news|library|forum|launchcalendar|lineup|store|comics|freetrial|login))(?P<id>[\w\-]+))/?(?:\?|$)' |
8230018c GS |
460 | |
461 | _TESTS = [{ | |
09e5d6a6 PH |
462 | 'url': 'http://www.crunchyroll.com/a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi', |
463 | 'info_dict': { | |
464 | 'id': 'a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi', | |
465 | 'title': 'A Bridge to the Starry Skies - Hoshizora e Kakaru Hashi' | |
8230018c | 466 | }, |
09e5d6a6 | 467 | 'playlist_count': 13, |
49941c4e S |
468 | }, { |
469 | # geo-restricted (US), 18+ maturity wall, non-premium available | |
470 | 'url': 'http://www.crunchyroll.com/cosplay-complex-ova', | |
471 | 'info_dict': { | |
472 | 'id': 'cosplay-complex-ova', | |
473 | 'title': 'Cosplay Complex OVA' | |
474 | }, | |
475 | 'playlist_count': 3, | |
476 | 'skip': 'Georestricted', | |
477 | }, { | |
478 | # geo-restricted (US), 18+ maturity wall, non-premium will be available since 2015.11.14 | |
479 | 'url': 'http://www.crunchyroll.com/ladies-versus-butlers?skip_wall=1', | |
480 | 'only_matching': True, | |
8230018c GS |
481 | }] |
482 | ||
8230018c | 483 | def _real_extract(self, url): |
09e5d6a6 PH |
484 | show_id = self._match_id(url) |
485 | ||
80f48920 | 486 | webpage = self._download_webpage(self._add_skip_wall(url), show_id) |
09e5d6a6 PH |
487 | title = self._html_search_regex( |
488 | r'(?s)<h1[^>]*>\s*<span itemprop="name">(.*?)</span>', | |
489 | webpage, 'title') | |
490 | episode_paths = re.findall( | |
491 | r'(?s)<li id="showview_videos_media_[0-9]+"[^>]+>.*?<a href="([^"]+)"', | |
492 | webpage) | |
493 | entries = [ | |
494 | self.url_result('http://www.crunchyroll.com' + ep, 'Crunchyroll') | |
495 | for ep in episode_paths | |
496 | ] | |
497 | entries.reverse() | |
498 | ||
8230018c | 499 | return { |
09e5d6a6 PH |
500 | '_type': 'playlist', |
501 | 'id': show_id, | |
502 | 'title': title, | |
503 | 'entries': entries, | |
504 | } |