You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

682 lines
28KB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. import zlib
  6. from hashlib import sha1
  7. from math import pow, sqrt, floor
  8. from .common import InfoExtractor
  9. from .vrv import VRVIE
  10. from ..compat import (
  11. compat_b64decode,
  12. compat_etree_Element,
  13. compat_etree_fromstring,
  14. compat_urllib_parse_urlencode,
  15. compat_urllib_request,
  16. compat_urlparse,
  17. )
  18. from ..utils import (
  19. ExtractorError,
  20. bytes_to_intlist,
  21. extract_attributes,
  22. float_or_none,
  23. intlist_to_bytes,
  24. int_or_none,
  25. lowercase_escape,
  26. remove_end,
  27. sanitized_Request,
  28. unified_strdate,
  29. urlencode_postdata,
  30. xpath_text,
  31. )
  32. from ..aes import (
  33. aes_cbc_decrypt,
  34. )
  35. class CrunchyrollBaseIE(InfoExtractor):
  36. _LOGIN_URL = 'https://www.crunchyroll.com/login'
  37. _LOGIN_FORM = 'login_form'
  38. _NETRC_MACHINE = 'crunchyroll'
  39. def _call_rpc_api(self, method, video_id, note=None, data=None):
  40. data = data or {}
  41. data['req'] = 'RpcApi' + method
  42. data = compat_urllib_parse_urlencode(data).encode('utf-8')
  43. return self._download_xml(
  44. 'https://www.crunchyroll.com/xml/',
  45. video_id, note, fatal=False, data=data, headers={
  46. 'Content-Type': 'application/x-www-form-urlencoded',
  47. })
  48. def _login(self):
  49. username, password = self._get_login_info()
  50. if username is None:
  51. return
  52. login_page = self._download_webpage(
  53. self._LOGIN_URL, None, 'Downloading login page')
  54. def is_logged(webpage):
  55. return 'href="/logout"' in webpage
  56. # Already logged in
  57. if is_logged(login_page):
  58. return
  59. login_form_str = self._search_regex(
  60. r'(?P<form><form[^>]+?id=(["\'])%s\2[^>]*>)' % self._LOGIN_FORM,
  61. login_page, 'login form', group='form')
  62. post_url = extract_attributes(login_form_str).get('action')
  63. if not post_url:
  64. post_url = self._LOGIN_URL
  65. elif not post_url.startswith('http'):
  66. post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url)
  67. login_form = self._form_hidden_inputs(self._LOGIN_FORM, login_page)
  68. login_form.update({
  69. 'login_form[name]': username,
  70. 'login_form[password]': password,
  71. })
  72. response = self._download_webpage(
  73. post_url, None, 'Logging in', 'Wrong login info',
  74. data=urlencode_postdata(login_form),
  75. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  76. # Successful login
  77. if is_logged(response):
  78. return
  79. error = self._html_search_regex(
  80. '(?s)<ul[^>]+class=["\']messages["\'][^>]*>(.+?)</ul>',
  81. response, 'error message', default=None)
  82. if error:
  83. raise ExtractorError('Unable to login: %s' % error, expected=True)
  84. raise ExtractorError('Unable to log in')
  85. def _real_initialize(self):
  86. self._login()
  87. def _download_webpage(self, url_or_request, *args, **kwargs):
  88. request = (url_or_request if isinstance(url_or_request, compat_urllib_request.Request)
  89. else sanitized_Request(url_or_request))
  90. # Accept-Language must be set explicitly to accept any language to avoid issues
  91. # similar to https://github.com/ytdl-org/youtube-dl/issues/6797.
  92. # Along with IP address Crunchyroll uses Accept-Language to guess whether georestriction
  93. # should be imposed or not (from what I can see it just takes the first language
  94. # ignoring the priority and requires it to correspond the IP). By the way this causes
  95. # Crunchyroll to not work in georestriction cases in some browsers that don't place
  96. # the locale lang first in header. However allowing any language seems to workaround the issue.
  97. request.add_header('Accept-Language', '*')
  98. return super(CrunchyrollBaseIE, self)._download_webpage(request, *args, **kwargs)
  99. @staticmethod
  100. def _add_skip_wall(url):
  101. parsed_url = compat_urlparse.urlparse(url)
  102. qs = compat_urlparse.parse_qs(parsed_url.query)
  103. # Always force skip_wall to bypass maturity wall, namely 18+ confirmation message:
  104. # > This content may be inappropriate for some people.
  105. # > Are you sure you want to continue?
  106. # since it's not disabled by default in crunchyroll account's settings.
  107. # See https://github.com/ytdl-org/youtube-dl/issues/7202.
  108. qs['skip_wall'] = ['1']
  109. return compat_urlparse.urlunparse(
  110. parsed_url._replace(query=compat_urllib_parse_urlencode(qs, True)))
  111. class CrunchyrollIE(CrunchyrollBaseIE, VRVIE):
  112. IE_NAME = 'crunchyroll'
  113. _VALID_URL = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.(?:com|fr)/(?:media(?:-|/\?id=)|(?:[^/]*/){1,2}[^/?&]*?)(?P<video_id>[0-9]+))(?:[/?&]|$)'
  114. _TESTS = [{
  115. 'url': 'http://www.crunchyroll.com/wanna-be-the-strongest-in-the-world/episode-1-an-idol-wrestler-is-born-645513',
  116. 'info_dict': {
  117. 'id': '645513',
  118. 'ext': 'mp4',
  119. 'title': 'Wanna be the Strongest in the World Episode 1 – An Idol-Wrestler is Born!',
  120. 'description': 'md5:2d17137920c64f2f49981a7797d275ef',
  121. 'thumbnail': r're:^https?://.*\.jpg$',
  122. 'uploader': 'Yomiuri Telecasting Corporation (YTV)',
  123. 'upload_date': '20131013',
  124. 'url': 're:(?!.*&amp)',
  125. },
  126. 'params': {
  127. # rtmp
  128. 'skip_download': True,
  129. },
  130. }, {
  131. 'url': 'http://www.crunchyroll.com/media-589804/culture-japan-1',
  132. 'info_dict': {
  133. 'id': '589804',
  134. 'ext': 'flv',
  135. 'title': 'Culture Japan Episode 1 – Rebuilding Japan after the 3.11',
  136. 'description': 'md5:2fbc01f90b87e8e9137296f37b461c12',
  137. 'thumbnail': r're:^https?://.*\.jpg$',
  138. 'uploader': 'Danny Choo Network',
  139. 'upload_date': '20120213',
  140. },
  141. 'params': {
  142. # rtmp
  143. 'skip_download': True,
  144. },
  145. 'skip': 'Video gone',
  146. }, {
  147. 'url': 'http://www.crunchyroll.com/rezero-starting-life-in-another-world-/episode-5-the-morning-of-our-promise-is-still-distant-702409',
  148. 'info_dict': {
  149. 'id': '702409',
  150. 'ext': 'mp4',
  151. 'title': 'Re:ZERO -Starting Life in Another World- Episode 5 – The Morning of Our Promise Is Still Distant',
  152. 'description': 'md5:97664de1ab24bbf77a9c01918cb7dca9',
  153. 'thumbnail': r're:^https?://.*\.jpg$',
  154. 'uploader': 'TV TOKYO',
  155. 'upload_date': '20160508',
  156. },
  157. 'params': {
  158. # m3u8 download
  159. 'skip_download': True,
  160. },
  161. }, {
  162. 'url': 'http://www.crunchyroll.com/konosuba-gods-blessing-on-this-wonderful-world/episode-1-give-me-deliverance-from-this-judicial-injustice-727589',
  163. 'info_dict': {
  164. 'id': '727589',
  165. 'ext': 'mp4',
  166. 'title': "KONOSUBA -God's blessing on this wonderful world! 2 Episode 1 – Give Me Deliverance From This Judicial Injustice!",
  167. 'description': 'md5:cbcf05e528124b0f3a0a419fc805ea7d',
  168. 'thumbnail': r're:^https?://.*\.jpg$',
  169. 'uploader': 'Kadokawa Pictures Inc.',
  170. 'upload_date': '20170118',
  171. 'series': "KONOSUBA -God's blessing on this wonderful world!",
  172. 'season': "KONOSUBA -God's blessing on this wonderful world! 2",
  173. 'season_number': 2,
  174. 'episode': 'Give Me Deliverance From This Judicial Injustice!',
  175. 'episode_number': 1,
  176. },
  177. 'params': {
  178. # m3u8 download
  179. 'skip_download': True,
  180. },
  181. }, {
  182. 'url': 'http://www.crunchyroll.fr/girl-friend-beta/episode-11-goodbye-la-mode-661697',
  183. 'only_matching': True,
  184. }, {
  185. # geo-restricted (US), 18+ maturity wall, non-premium available
  186. 'url': 'http://www.crunchyroll.com/cosplay-complex-ova/episode-1-the-birth-of-the-cosplay-club-565617',
  187. 'only_matching': True,
  188. }, {
  189. # A description with double quotes
  190. 'url': 'http://www.crunchyroll.com/11eyes/episode-1-piros-jszaka-red-night-535080',
  191. 'info_dict': {
  192. 'id': '535080',
  193. 'ext': 'mp4',
  194. 'title': '11eyes Episode 1 – Red Night ~ Piros éjszaka',
  195. 'description': 'Kakeru and Yuka are thrown into an alternate nightmarish world they call "Red Night".',
  196. 'uploader': 'Marvelous AQL Inc.',
  197. 'upload_date': '20091021',
  198. },
  199. 'params': {
  200. # Just test metadata extraction
  201. 'skip_download': True,
  202. },
  203. }, {
  204. # make sure we can extract an uploader name that's not a link
  205. 'url': 'http://www.crunchyroll.com/hakuoki-reimeiroku/episode-1-dawn-of-the-divine-warriors-606899',
  206. 'info_dict': {
  207. 'id': '606899',
  208. 'ext': 'mp4',
  209. 'title': 'Hakuoki Reimeiroku Episode 1 – Dawn of the Divine Warriors',
  210. 'description': 'Ryunosuke was left to die, but Serizawa-san asked him a simple question "Do you want to live?"',
  211. 'uploader': 'Geneon Entertainment',
  212. 'upload_date': '20120717',
  213. },
  214. 'params': {
  215. # just test metadata extraction
  216. 'skip_download': True,
  217. },
  218. }, {
  219. # A video with a vastly different season name compared to the series name
  220. 'url': 'http://www.crunchyroll.com/nyarko-san-another-crawling-chaos/episode-1-test-590532',
  221. 'info_dict': {
  222. 'id': '590532',
  223. 'ext': 'mp4',
  224. 'title': 'Haiyoru! Nyaruani (ONA) Episode 1 – Test',
  225. 'description': 'Mahiro and Nyaruko talk about official certification.',
  226. 'uploader': 'TV TOKYO',
  227. 'upload_date': '20120305',
  228. 'series': 'Nyarko-san: Another Crawling Chaos',
  229. 'season': 'Haiyoru! Nyaruani (ONA)',
  230. },
  231. 'params': {
  232. # Just test metadata extraction
  233. 'skip_download': True,
  234. },
  235. }, {
  236. 'url': 'http://www.crunchyroll.com/media-723735',
  237. 'only_matching': True,
  238. }, {
  239. 'url': 'https://www.crunchyroll.com/en-gb/mob-psycho-100/episode-2-urban-legends-encountering-rumors-780921',
  240. 'only_matching': True,
  241. }]
  242. _FORMAT_IDS = {
  243. '360': ('60', '106'),
  244. '480': ('61', '106'),
  245. '720': ('62', '106'),
  246. '1080': ('80', '108'),
  247. }
  248. def _decrypt_subtitles(self, data, iv, id):
  249. data = bytes_to_intlist(compat_b64decode(data))
  250. iv = bytes_to_intlist(compat_b64decode(iv))
  251. id = int(id)
  252. def obfuscate_key_aux(count, modulo, start):
  253. output = list(start)
  254. for _ in range(count):
  255. output.append(output[-1] + output[-2])
  256. # cut off start values
  257. output = output[2:]
  258. output = list(map(lambda x: x % modulo + 33, output))
  259. return output
  260. def obfuscate_key(key):
  261. num1 = int(floor(pow(2, 25) * sqrt(6.9)))
  262. num2 = (num1 ^ key) << 5
  263. num3 = key ^ num1
  264. num4 = num3 ^ (num3 >> 3) ^ num2
  265. prefix = intlist_to_bytes(obfuscate_key_aux(20, 97, (1, 2)))
  266. shaHash = bytes_to_intlist(sha1(prefix + str(num4).encode('ascii')).digest())
  267. # Extend 160 Bit hash to 256 Bit
  268. return shaHash + [0] * 12
  269. key = obfuscate_key(id)
  270. decrypted_data = intlist_to_bytes(aes_cbc_decrypt(data, key, iv))
  271. return zlib.decompress(decrypted_data)
  272. def _convert_subtitles_to_srt(self, sub_root):
  273. output = ''
  274. for i, event in enumerate(sub_root.findall('./events/event'), 1):
  275. start = event.attrib['start'].replace('.', ',')
  276. end = event.attrib['end'].replace('.', ',')
  277. text = event.attrib['text'].replace('\\N', '\n')
  278. output += '%d\n%s --> %s\n%s\n\n' % (i, start, end, text)
  279. return output
  280. def _convert_subtitles_to_ass(self, sub_root):
  281. output = ''
  282. def ass_bool(strvalue):
  283. assvalue = '0'
  284. if strvalue == '1':
  285. assvalue = '-1'
  286. return assvalue
  287. output = '[Script Info]\n'
  288. output += 'Title: %s\n' % sub_root.attrib['title']
  289. output += 'ScriptType: v4.00+\n'
  290. output += 'WrapStyle: %s\n' % sub_root.attrib['wrap_style']
  291. output += 'PlayResX: %s\n' % sub_root.attrib['play_res_x']
  292. output += 'PlayResY: %s\n' % sub_root.attrib['play_res_y']
  293. output += """
  294. [V4+ Styles]
  295. Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
  296. """
  297. for style in sub_root.findall('./styles/style'):
  298. output += 'Style: ' + style.attrib['name']
  299. output += ',' + style.attrib['font_name']
  300. output += ',' + style.attrib['font_size']
  301. output += ',' + style.attrib['primary_colour']
  302. output += ',' + style.attrib['secondary_colour']
  303. output += ',' + style.attrib['outline_colour']
  304. output += ',' + style.attrib['back_colour']
  305. output += ',' + ass_bool(style.attrib['bold'])
  306. output += ',' + ass_bool(style.attrib['italic'])
  307. output += ',' + ass_bool(style.attrib['underline'])
  308. output += ',' + ass_bool(style.attrib['strikeout'])
  309. output += ',' + style.attrib['scale_x']
  310. output += ',' + style.attrib['scale_y']
  311. output += ',' + style.attrib['spacing']
  312. output += ',' + style.attrib['angle']
  313. output += ',' + style.attrib['border_style']
  314. output += ',' + style.attrib['outline']
  315. output += ',' + style.attrib['shadow']
  316. output += ',' + style.attrib['alignment']
  317. output += ',' + style.attrib['margin_l']
  318. output += ',' + style.attrib['margin_r']
  319. output += ',' + style.attrib['margin_v']
  320. output += ',' + style.attrib['encoding']
  321. output += '\n'
  322. output += """
  323. [Events]
  324. Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
  325. """
  326. for event in sub_root.findall('./events/event'):
  327. output += 'Dialogue: 0'
  328. output += ',' + event.attrib['start']
  329. output += ',' + event.attrib['end']
  330. output += ',' + event.attrib['style']
  331. output += ',' + event.attrib['name']
  332. output += ',' + event.attrib['margin_l']
  333. output += ',' + event.attrib['margin_r']
  334. output += ',' + event.attrib['margin_v']
  335. output += ',' + event.attrib['effect']
  336. output += ',' + event.attrib['text']
  337. output += '\n'
  338. return output
  339. def _extract_subtitles(self, subtitle):
  340. sub_root = compat_etree_fromstring(subtitle)
  341. return [{
  342. 'ext': 'srt',
  343. 'data': self._convert_subtitles_to_srt(sub_root),
  344. }, {
  345. 'ext': 'ass',
  346. 'data': self._convert_subtitles_to_ass(sub_root),
  347. }]
  348. def _get_subtitles(self, video_id, webpage):
  349. subtitles = {}
  350. for sub_id, sub_name in re.findall(r'\bssid=([0-9]+)"[^>]+?\btitle="([^"]+)', webpage):
  351. sub_doc = self._call_rpc_api(
  352. 'Subtitle_GetXml', video_id,
  353. 'Downloading subtitles for ' + sub_name, data={
  354. 'subtitle_script_id': sub_id,
  355. })
  356. if not isinstance(sub_doc, compat_etree_Element):
  357. continue
  358. sid = sub_doc.get('id')
  359. iv = xpath_text(sub_doc, 'iv', 'subtitle iv')
  360. data = xpath_text(sub_doc, 'data', 'subtitle data')
  361. if not sid or not iv or not data:
  362. continue
  363. subtitle = self._decrypt_subtitles(data, iv, sid).decode('utf-8')
  364. lang_code = self._search_regex(r'lang_code=["\']([^"\']+)', subtitle, 'subtitle_lang_code', fatal=False)
  365. if not lang_code:
  366. continue
  367. subtitles[lang_code] = self._extract_subtitles(subtitle)
  368. return subtitles
  369. def _real_extract(self, url):
  370. mobj = re.match(self._VALID_URL, url)
  371. video_id = mobj.group('video_id')
  372. if mobj.group('prefix') == 'm':
  373. mobile_webpage = self._download_webpage(url, video_id, 'Downloading mobile webpage')
  374. webpage_url = self._search_regex(r'<link rel="canonical" href="([^"]+)" />', mobile_webpage, 'webpage_url')
  375. else:
  376. webpage_url = 'http://www.' + mobj.group('url')
  377. webpage = self._download_webpage(
  378. self._add_skip_wall(webpage_url), video_id,
  379. headers=self.geo_verification_headers())
  380. note_m = self._html_search_regex(
  381. r'<div class="showmedia-trailer-notice">(.+?)</div>',
  382. webpage, 'trailer-notice', default='')
  383. if note_m:
  384. raise ExtractorError(note_m)
  385. mobj = re.search(r'Page\.messaging_box_controller\.addItems\(\[(?P<msg>{.+?})\]\)', webpage)
  386. if mobj:
  387. msg = json.loads(mobj.group('msg'))
  388. if msg.get('type') == 'error':
  389. raise ExtractorError('crunchyroll returned error: %s' % msg['message_body'], expected=True)
  390. if 'To view this, please log in to verify you are 18 or older.' in webpage:
  391. self.raise_login_required()
  392. media = self._parse_json(self._search_regex(
  393. r'vilos\.config\.media\s*=\s*({.+?});',
  394. webpage, 'vilos media', default='{}'), video_id)
  395. media_metadata = media.get('metadata') or {}
  396. language = self._search_regex(
  397. r'(?:vilos\.config\.player\.language|LOCALE)\s*=\s*(["\'])(?P<lang>(?:(?!\1).)+)\1',
  398. webpage, 'language', default=None, group='lang')
  399. video_title = self._html_search_regex(
  400. r'(?s)<h1[^>]*>((?:(?!<h1).)*?<span[^>]+itemprop=["\']title["\'][^>]*>(?:(?!<h1).)+?)</h1>',
  401. webpage, 'video_title')
  402. video_title = re.sub(r' {2,}', ' ', video_title)
  403. video_description = (self._parse_json(self._html_search_regex(
  404. r'<script[^>]*>\s*.+?\[media_id=%s\].+?({.+?"description"\s*:.+?})\);' % video_id,
  405. webpage, 'description', default='{}'), video_id) or media_metadata).get('description')
  406. if video_description:
  407. video_description = lowercase_escape(video_description.replace(r'\r\n', '\n'))
  408. video_upload_date = self._html_search_regex(
  409. [r'<div>Availability for free users:(.+?)</div>', r'<div>[^<>]+<span>\s*(.+?\d{4})\s*</span></div>'],
  410. webpage, 'video_upload_date', fatal=False, flags=re.DOTALL)
  411. if video_upload_date:
  412. video_upload_date = unified_strdate(video_upload_date)
  413. video_uploader = self._html_search_regex(
  414. # try looking for both an uploader that's a link and one that's not
  415. [r'<a[^>]+href="/publisher/[^"]+"[^>]*>([^<]+)</a>', r'<div>\s*Publisher:\s*<span>\s*(.+?)\s*</span>\s*</div>'],
  416. webpage, 'video_uploader', fatal=False)
  417. formats = []
  418. for stream in media.get('streams', []):
  419. audio_lang = stream.get('audio_lang')
  420. hardsub_lang = stream.get('hardsub_lang')
  421. vrv_formats = self._extract_vrv_formats(
  422. stream.get('url'), video_id, stream.get('format'),
  423. audio_lang, hardsub_lang)
  424. for f in vrv_formats:
  425. if not hardsub_lang:
  426. f['preference'] = 1
  427. language_preference = 0
  428. if audio_lang == language:
  429. language_preference += 1
  430. if hardsub_lang == language:
  431. language_preference += 1
  432. if language_preference:
  433. f['language_preference'] = language_preference
  434. formats.extend(vrv_formats)
  435. if not formats:
  436. available_fmts = []
  437. for a, fmt in re.findall(r'(<a[^>]+token=["\']showmedia\.([0-9]{3,4})p["\'][^>]+>)', webpage):
  438. attrs = extract_attributes(a)
  439. href = attrs.get('href')
  440. if href and '/freetrial' in href:
  441. continue
  442. available_fmts.append(fmt)
  443. if not available_fmts:
  444. for p in (r'token=["\']showmedia\.([0-9]{3,4})p"', r'showmedia\.([0-9]{3,4})p'):
  445. available_fmts = re.findall(p, webpage)
  446. if available_fmts:
  447. break
  448. if not available_fmts:
  449. available_fmts = self._FORMAT_IDS.keys()
  450. video_encode_ids = []
  451. for fmt in available_fmts:
  452. stream_quality, stream_format = self._FORMAT_IDS[fmt]
  453. video_format = fmt + 'p'
  454. stream_infos = []
  455. streamdata = self._call_rpc_api(
  456. 'VideoPlayer_GetStandardConfig', video_id,
  457. 'Downloading media info for %s' % video_format, data={
  458. 'media_id': video_id,
  459. 'video_format': stream_format,
  460. 'video_quality': stream_quality,
  461. 'current_page': url,
  462. })
  463. if isinstance(streamdata, compat_etree_Element):
  464. stream_info = streamdata.find('./{default}preload/stream_info')
  465. if stream_info is not None:
  466. stream_infos.append(stream_info)
  467. stream_info = self._call_rpc_api(
  468. 'VideoEncode_GetStreamInfo', video_id,
  469. 'Downloading stream info for %s' % video_format, data={
  470. 'media_id': video_id,
  471. 'video_format': stream_format,
  472. 'video_encode_quality': stream_quality,
  473. })
  474. if isinstance(stream_info, compat_etree_Element):
  475. stream_infos.append(stream_info)
  476. for stream_info in stream_infos:
  477. video_encode_id = xpath_text(stream_info, './video_encode_id')
  478. if video_encode_id in video_encode_ids:
  479. continue
  480. video_encode_ids.append(video_encode_id)
  481. video_file = xpath_text(stream_info, './file')
  482. if not video_file:
  483. continue
  484. if video_file.startswith('http'):
  485. formats.extend(self._extract_m3u8_formats(
  486. video_file, video_id, 'mp4', entry_protocol='m3u8_native',
  487. m3u8_id='hls', fatal=False))
  488. continue
  489. video_url = xpath_text(stream_info, './host')
  490. if not video_url:
  491. continue
  492. metadata = stream_info.find('./metadata')
  493. format_info = {
  494. 'format': video_format,
  495. 'height': int_or_none(xpath_text(metadata, './height')),
  496. 'width': int_or_none(xpath_text(metadata, './width')),
  497. }
  498. if '.fplive.net/' in video_url:
  499. video_url = re.sub(r'^rtmpe?://', 'http://', video_url.strip())
  500. parsed_video_url = compat_urlparse.urlparse(video_url)
  501. direct_video_url = compat_urlparse.urlunparse(parsed_video_url._replace(
  502. netloc='v.lvlt.crcdn.net',
  503. path='%s/%s' % (remove_end(parsed_video_url.path, '/'), video_file.split(':')[-1])))
  504. if self._is_valid_url(direct_video_url, video_id, video_format):
  505. format_info.update({
  506. 'format_id': 'http-' + video_format,
  507. 'url': direct_video_url,
  508. })
  509. formats.append(format_info)
  510. continue
  511. format_info.update({
  512. 'format_id': 'rtmp-' + video_format,
  513. 'url': video_url,
  514. 'play_path': video_file,
  515. 'ext': 'flv',
  516. })
  517. formats.append(format_info)
  518. self._sort_formats(formats, ('preference', 'language_preference', 'height', 'width', 'tbr', 'fps'))
  519. metadata = self._call_rpc_api(
  520. 'VideoPlayer_GetMediaMetadata', video_id,
  521. note='Downloading media info', data={
  522. 'media_id': video_id,
  523. })
  524. subtitles = {}
  525. for subtitle in media.get('subtitles', []):
  526. subtitle_url = subtitle.get('url')
  527. if not subtitle_url:
  528. continue
  529. subtitles.setdefault(subtitle.get('language', 'enUS'), []).append({
  530. 'url': subtitle_url,
  531. 'ext': subtitle.get('format', 'ass'),
  532. })
  533. if not subtitles:
  534. subtitles = self.extract_subtitles(video_id, webpage)
  535. # webpage provide more accurate data than series_title from XML
  536. series = self._html_search_regex(
  537. r'(?s)<h\d[^>]+\bid=["\']showmedia_about_episode_num[^>]+>(.+?)</h\d',
  538. webpage, 'series', fatal=False)
  539. season = episode = episode_number = duration = thumbnail = None
  540. if isinstance(metadata, compat_etree_Element):
  541. season = xpath_text(metadata, 'series_title')
  542. episode = xpath_text(metadata, 'episode_title')
  543. episode_number = int_or_none(xpath_text(metadata, 'episode_number'))
  544. duration = float_or_none(media_metadata.get('duration'), 1000)
  545. thumbnail = xpath_text(metadata, 'episode_image_url')
  546. if not episode:
  547. episode = media_metadata.get('title')
  548. if not episode_number:
  549. episode_number = int_or_none(media_metadata.get('episode_number'))
  550. if not thumbnail:
  551. thumbnail = media_metadata.get('thumbnail', {}).get('url')
  552. season_number = int_or_none(self._search_regex(
  553. r'(?s)<h\d[^>]+id=["\']showmedia_about_episode_num[^>]+>.+?</h\d>\s*<h4>\s*Season (\d+)',
  554. webpage, 'season number', default=None))
  555. return {
  556. 'id': video_id,
  557. 'title': video_title,
  558. 'description': video_description,
  559. 'duration': duration,
  560. 'thumbnail': thumbnail,
  561. 'uploader': video_uploader,
  562. 'upload_date': video_upload_date,
  563. 'series': series,
  564. 'season': season,
  565. 'season_number': season_number,
  566. 'episode': episode,
  567. 'episode_number': episode_number,
  568. 'subtitles': subtitles,
  569. 'formats': formats,
  570. }
  571. class CrunchyrollShowPlaylistIE(CrunchyrollBaseIE):
  572. IE_NAME = 'crunchyroll:playlist'
  573. _VALID_URL = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.com/(?!(?:news|anime-news|library|forum|launchcalendar|lineup|store|comics|freetrial|login|media-\d+))(?P<id>[\w\-]+))/?(?:\?|$)'
  574. _TESTS = [{
  575. 'url': 'http://www.crunchyroll.com/a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi',
  576. 'info_dict': {
  577. 'id': 'a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi',
  578. 'title': 'A Bridge to the Starry Skies - Hoshizora e Kakaru Hashi'
  579. },
  580. 'playlist_count': 13,
  581. }, {
  582. # geo-restricted (US), 18+ maturity wall, non-premium available
  583. 'url': 'http://www.crunchyroll.com/cosplay-complex-ova',
  584. 'info_dict': {
  585. 'id': 'cosplay-complex-ova',
  586. 'title': 'Cosplay Complex OVA'
  587. },
  588. 'playlist_count': 3,
  589. 'skip': 'Georestricted',
  590. }, {
  591. # geo-restricted (US), 18+ maturity wall, non-premium will be available since 2015.11.14
  592. 'url': 'http://www.crunchyroll.com/ladies-versus-butlers?skip_wall=1',
  593. 'only_matching': True,
  594. }]
  595. def _real_extract(self, url):
  596. show_id = self._match_id(url)
  597. webpage = self._download_webpage(
  598. self._add_skip_wall(url), show_id,
  599. headers=self.geo_verification_headers())
  600. title = self._html_search_regex(
  601. r'(?s)<h1[^>]*>\s*<span itemprop="name">(.*?)</span>',
  602. webpage, 'title')
  603. episode_paths = re.findall(
  604. r'(?s)<li id="showview_videos_media_(\d+)"[^>]+>.*?<a href="([^"]+)"',
  605. webpage)
  606. entries = [
  607. self.url_result('http://www.crunchyroll.com' + ep, 'Crunchyroll', ep_id)
  608. for ep_id, ep in episode_paths
  609. ]
  610. entries.reverse()
  611. return {
  612. '_type': 'playlist',
  613. 'id': show_id,
  614. 'title': title,
  615. 'entries': entries,
  616. }