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.

630 lines
26 KiB

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