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.

425 lines
18 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
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
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
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
  1. # encoding: 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,
  13. compat_urllib_parse_unquote,
  14. compat_urllib_request,
  15. compat_urlparse,
  16. )
  17. from ..utils import (
  18. ExtractorError,
  19. bytes_to_intlist,
  20. intlist_to_bytes,
  21. int_or_none,
  22. lowercase_escape,
  23. remove_end,
  24. sanitized_Request,
  25. unified_strdate,
  26. urlencode_postdata,
  27. xpath_text,
  28. )
  29. from ..aes import (
  30. aes_cbc_decrypt,
  31. )
  32. class CrunchyrollBaseIE(InfoExtractor):
  33. _NETRC_MACHINE = 'crunchyroll'
  34. def _login(self):
  35. (username, password) = self._get_login_info()
  36. if username is None:
  37. return
  38. self.report_login()
  39. login_url = 'https://www.crunchyroll.com/?a=formhandler'
  40. data = urlencode_postdata({
  41. 'formname': 'RpcApiUser_Login',
  42. 'name': username,
  43. 'password': password,
  44. })
  45. login_request = sanitized_Request(login_url, data)
  46. login_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  47. self._download_webpage(login_request, None, False, 'Wrong login info')
  48. def _real_initialize(self):
  49. self._login()
  50. def _download_webpage(self, url_or_request, video_id, note=None, errnote=None, fatal=True, tries=1, timeout=5, encoding=None):
  51. request = (url_or_request if isinstance(url_or_request, compat_urllib_request.Request)
  52. else sanitized_Request(url_or_request))
  53. # Accept-Language must be set explicitly to accept any language to avoid issues
  54. # similar to https://github.com/rg3/youtube-dl/issues/6797.
  55. # Along with IP address Crunchyroll uses Accept-Language to guess whether georestriction
  56. # should be imposed or not (from what I can see it just takes the first language
  57. # ignoring the priority and requires it to correspond the IP). By the way this causes
  58. # Crunchyroll to not work in georestriction cases in some browsers that don't place
  59. # the locale lang first in header. However allowing any language seems to workaround the issue.
  60. request.add_header('Accept-Language', '*')
  61. return super(CrunchyrollBaseIE, self)._download_webpage(
  62. request, video_id, note, errnote, fatal, tries, timeout, encoding)
  63. @staticmethod
  64. def _add_skip_wall(url):
  65. parsed_url = compat_urlparse.urlparse(url)
  66. qs = compat_urlparse.parse_qs(parsed_url.query)
  67. # Always force skip_wall to bypass maturity wall, namely 18+ confirmation message:
  68. # > This content may be inappropriate for some people.
  69. # > Are you sure you want to continue?
  70. # since it's not disabled by default in crunchyroll account's settings.
  71. # See https://github.com/rg3/youtube-dl/issues/7202.
  72. qs['skip_wall'] = ['1']
  73. return compat_urlparse.urlunparse(
  74. parsed_url._replace(query=compat_urllib_parse.urlencode(qs, True)))
  75. class CrunchyrollIE(CrunchyrollBaseIE):
  76. _VALID_URL = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.(?:com|fr)/(?:media(?:-|/\?id=)|[^/]*/[^/?&]*?)(?P<video_id>[0-9]+))(?:[/?&]|$)'
  77. _TESTS = [{
  78. 'url': 'http://www.crunchyroll.com/wanna-be-the-strongest-in-the-world/episode-1-an-idol-wrestler-is-born-645513',
  79. 'info_dict': {
  80. 'id': '645513',
  81. 'ext': 'flv',
  82. 'title': 'Wanna be the Strongest in the World Episode 1 – An Idol-Wrestler is Born!',
  83. 'description': 'md5:2d17137920c64f2f49981a7797d275ef',
  84. 'thumbnail': 'http://img1.ak.crunchyroll.com/i/spire1-tmb/20c6b5e10f1a47b10516877d3c039cae1380951166_full.jpg',
  85. 'uploader': 'Yomiuri Telecasting Corporation (YTV)',
  86. 'upload_date': '20131013',
  87. 'url': 're:(?!.*&amp)',
  88. },
  89. 'params': {
  90. # rtmp
  91. 'skip_download': True,
  92. },
  93. }, {
  94. 'url': 'http://www.crunchyroll.com/media-589804/culture-japan-1',
  95. 'info_dict': {
  96. 'id': '589804',
  97. 'ext': 'flv',
  98. 'title': 'Culture Japan Episode 1 – Rebuilding Japan after the 3.11',
  99. 'description': 'md5:2fbc01f90b87e8e9137296f37b461c12',
  100. 'thumbnail': 're:^https?://.*\.jpg$',
  101. 'uploader': 'Danny Choo Network',
  102. 'upload_date': '20120213',
  103. },
  104. 'params': {
  105. # rtmp
  106. 'skip_download': True,
  107. },
  108. }, {
  109. 'url': 'http://www.crunchyroll.fr/girl-friend-beta/episode-11-goodbye-la-mode-661697',
  110. 'only_matching': True,
  111. }, {
  112. # geo-restricted (US), 18+ maturity wall, non-premium available
  113. 'url': 'http://www.crunchyroll.com/cosplay-complex-ova/episode-1-the-birth-of-the-cosplay-club-565617',
  114. 'only_matching': True,
  115. }]
  116. _FORMAT_IDS = {
  117. '360': ('60', '106'),
  118. '480': ('61', '106'),
  119. '720': ('62', '106'),
  120. '1080': ('80', '108'),
  121. }
  122. def _decrypt_subtitles(self, data, iv, id):
  123. data = bytes_to_intlist(base64.b64decode(data.encode('utf-8')))
  124. iv = bytes_to_intlist(base64.b64decode(iv.encode('utf-8')))
  125. id = int(id)
  126. def obfuscate_key_aux(count, modulo, start):
  127. output = list(start)
  128. for _ in range(count):
  129. output.append(output[-1] + output[-2])
  130. # cut off start values
  131. output = output[2:]
  132. output = list(map(lambda x: x % modulo + 33, output))
  133. return output
  134. def obfuscate_key(key):
  135. num1 = int(floor(pow(2, 25) * sqrt(6.9)))
  136. num2 = (num1 ^ key) << 5
  137. num3 = key ^ num1
  138. num4 = num3 ^ (num3 >> 3) ^ num2
  139. prefix = intlist_to_bytes(obfuscate_key_aux(20, 97, (1, 2)))
  140. shaHash = bytes_to_intlist(sha1(prefix + str(num4).encode('ascii')).digest())
  141. # Extend 160 Bit hash to 256 Bit
  142. return shaHash + [0] * 12
  143. key = obfuscate_key(id)
  144. decrypted_data = intlist_to_bytes(aes_cbc_decrypt(data, key, iv))
  145. return zlib.decompress(decrypted_data)
  146. def _convert_subtitles_to_srt(self, sub_root):
  147. output = ''
  148. for i, event in enumerate(sub_root.findall('./events/event'), 1):
  149. start = event.attrib['start'].replace('.', ',')
  150. end = event.attrib['end'].replace('.', ',')
  151. text = event.attrib['text'].replace('\\N', '\n')
  152. output += '%d\n%s --> %s\n%s\n\n' % (i, start, end, text)
  153. return output
  154. def _convert_subtitles_to_ass(self, sub_root):
  155. output = ''
  156. def ass_bool(strvalue):
  157. assvalue = '0'
  158. if strvalue == '1':
  159. assvalue = '-1'
  160. return assvalue
  161. output = '[Script Info]\n'
  162. output += 'Title: %s\n' % sub_root.attrib['title']
  163. output += 'ScriptType: v4.00+\n'
  164. output += 'WrapStyle: %s\n' % sub_root.attrib['wrap_style']
  165. output += 'PlayResX: %s\n' % sub_root.attrib['play_res_x']
  166. output += 'PlayResY: %s\n' % sub_root.attrib['play_res_y']
  167. output += """ScaledBorderAndShadow: yes
  168. [V4+ Styles]
  169. Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
  170. """
  171. for style in sub_root.findall('./styles/style'):
  172. output += 'Style: ' + style.attrib['name']
  173. output += ',' + style.attrib['font_name']
  174. output += ',' + style.attrib['font_size']
  175. output += ',' + style.attrib['primary_colour']
  176. output += ',' + style.attrib['secondary_colour']
  177. output += ',' + style.attrib['outline_colour']
  178. output += ',' + style.attrib['back_colour']
  179. output += ',' + ass_bool(style.attrib['bold'])
  180. output += ',' + ass_bool(style.attrib['italic'])
  181. output += ',' + ass_bool(style.attrib['underline'])
  182. output += ',' + ass_bool(style.attrib['strikeout'])
  183. output += ',' + style.attrib['scale_x']
  184. output += ',' + style.attrib['scale_y']
  185. output += ',' + style.attrib['spacing']
  186. output += ',' + style.attrib['angle']
  187. output += ',' + style.attrib['border_style']
  188. output += ',' + style.attrib['outline']
  189. output += ',' + style.attrib['shadow']
  190. output += ',' + style.attrib['alignment']
  191. output += ',' + style.attrib['margin_l']
  192. output += ',' + style.attrib['margin_r']
  193. output += ',' + style.attrib['margin_v']
  194. output += ',' + style.attrib['encoding']
  195. output += '\n'
  196. output += """
  197. [Events]
  198. Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
  199. """
  200. for event in sub_root.findall('./events/event'):
  201. output += 'Dialogue: 0'
  202. output += ',' + event.attrib['start']
  203. output += ',' + event.attrib['end']
  204. output += ',' + event.attrib['style']
  205. output += ',' + event.attrib['name']
  206. output += ',' + event.attrib['margin_l']
  207. output += ',' + event.attrib['margin_r']
  208. output += ',' + event.attrib['margin_v']
  209. output += ',' + event.attrib['effect']
  210. output += ',' + event.attrib['text']
  211. output += '\n'
  212. return output
  213. def _extract_subtitles(self, subtitle):
  214. sub_root = compat_etree_fromstring(subtitle)
  215. return [{
  216. 'ext': 'srt',
  217. 'data': self._convert_subtitles_to_srt(sub_root),
  218. }, {
  219. 'ext': 'ass',
  220. 'data': self._convert_subtitles_to_ass(sub_root),
  221. }]
  222. def _get_subtitles(self, video_id, webpage):
  223. subtitles = {}
  224. for sub_id, sub_name in re.findall(r'\bssid=([0-9]+)"[^>]+?\btitle="([^"]+)', webpage):
  225. sub_page = self._download_webpage(
  226. 'http://www.crunchyroll.com/xml/?req=RpcApiSubtitle_GetXml&subtitle_script_id=' + sub_id,
  227. video_id, note='Downloading subtitles for ' + sub_name)
  228. id = self._search_regex(r'id=\'([0-9]+)', sub_page, 'subtitle_id', fatal=False)
  229. iv = self._search_regex(r'<iv>([^<]+)', sub_page, 'subtitle_iv', fatal=False)
  230. data = self._search_regex(r'<data>([^<]+)', sub_page, 'subtitle_data', fatal=False)
  231. if not id or not iv or not data:
  232. continue
  233. subtitle = self._decrypt_subtitles(data, iv, id).decode('utf-8')
  234. lang_code = self._search_regex(r'lang_code=["\']([^"\']+)', subtitle, 'subtitle_lang_code', fatal=False)
  235. if not lang_code:
  236. continue
  237. subtitles[lang_code] = self._extract_subtitles(subtitle)
  238. return subtitles
  239. def _real_extract(self, url):
  240. mobj = re.match(self._VALID_URL, url)
  241. video_id = mobj.group('video_id')
  242. if mobj.group('prefix') == 'm':
  243. mobile_webpage = self._download_webpage(url, video_id, 'Downloading mobile webpage')
  244. webpage_url = self._search_regex(r'<link rel="canonical" href="([^"]+)" />', mobile_webpage, 'webpage_url')
  245. else:
  246. webpage_url = 'http://www.' + mobj.group('url')
  247. webpage = self._download_webpage(self._add_skip_wall(webpage_url), video_id, 'Downloading webpage')
  248. note_m = self._html_search_regex(
  249. r'<div class="showmedia-trailer-notice">(.+?)</div>',
  250. webpage, 'trailer-notice', default='')
  251. if note_m:
  252. raise ExtractorError(note_m)
  253. mobj = re.search(r'Page\.messaging_box_controller\.addItems\(\[(?P<msg>{.+?})\]\)', webpage)
  254. if mobj:
  255. msg = json.loads(mobj.group('msg'))
  256. if msg.get('type') == 'error':
  257. raise ExtractorError('crunchyroll returned error: %s' % msg['message_body'], expected=True)
  258. if 'To view this, please log in to verify you are 18 or older.' in webpage:
  259. self.raise_login_required()
  260. video_title = self._html_search_regex(
  261. r'(?s)<h1[^>]*>((?:(?!<h1).)*?<span[^>]+itemprop=["\']title["\'][^>]*>(?:(?!<h1).)+?)</h1>',
  262. webpage, 'video_title')
  263. video_title = re.sub(r' {2,}', ' ', video_title)
  264. video_description = self._html_search_regex(
  265. r'<script[^>]*>\s*.+?\[media_id=%s\].+?"description"\s*:\s*"([^"]+)' % video_id,
  266. webpage, 'description', default=None)
  267. if video_description:
  268. video_description = lowercase_escape(video_description.replace(r'\r\n', '\n'))
  269. video_upload_date = self._html_search_regex(
  270. [r'<div>Availability for free users:(.+?)</div>', r'<div>[^<>]+<span>\s*(.+?\d{4})\s*</span></div>'],
  271. webpage, 'video_upload_date', fatal=False, flags=re.DOTALL)
  272. if video_upload_date:
  273. video_upload_date = unified_strdate(video_upload_date)
  274. video_uploader = self._html_search_regex(
  275. r'<a[^>]+href="/publisher/[^"]+"[^>]*>([^<]+)</a>', webpage,
  276. 'video_uploader', fatal=False)
  277. playerdata_url = compat_urllib_parse_unquote(self._html_search_regex(r'"config_url":"([^"]+)', webpage, 'playerdata_url'))
  278. playerdata_req = sanitized_Request(playerdata_url)
  279. playerdata_req.data = compat_urllib_parse.urlencode({'current_page': webpage_url})
  280. playerdata_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  281. playerdata = self._download_webpage(playerdata_req, video_id, note='Downloading media info')
  282. stream_id = self._search_regex(r'<media_id>([^<]+)', playerdata, 'stream_id')
  283. video_thumbnail = self._search_regex(r'<episode_image_url>([^<]+)', playerdata, 'thumbnail', fatal=False)
  284. formats = []
  285. for fmt in re.findall(r'showmedia\.([0-9]{3,4})p', webpage):
  286. stream_quality, stream_format = self._FORMAT_IDS[fmt]
  287. video_format = fmt + 'p'
  288. streamdata_req = sanitized_Request(
  289. 'http://www.crunchyroll.com/xml/?req=RpcApiVideoPlayer_GetStandardConfig&media_id=%s&video_format=%s&video_quality=%s'
  290. % (stream_id, stream_format, stream_quality),
  291. compat_urllib_parse.urlencode({'current_page': url}).encode('utf-8'))
  292. streamdata_req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  293. streamdata = self._download_xml(
  294. streamdata_req, video_id,
  295. note='Downloading media info for %s' % video_format)
  296. stream_info = streamdata.find('./{default}preload/stream_info')
  297. video_url = xpath_text(stream_info, './host')
  298. video_play_path = xpath_text(stream_info, './file')
  299. if not video_url or not video_play_path:
  300. continue
  301. metadata = stream_info.find('./metadata')
  302. format_info = {
  303. 'format': video_format,
  304. 'format_id': video_format,
  305. 'height': int_or_none(xpath_text(metadata, './height')),
  306. 'width': int_or_none(xpath_text(metadata, './width')),
  307. }
  308. if '.fplive.net/' in video_url:
  309. video_url = re.sub(r'^rtmpe?://', 'http://', video_url.strip())
  310. parsed_video_url = compat_urlparse.urlparse(video_url)
  311. direct_video_url = compat_urlparse.urlunparse(parsed_video_url._replace(
  312. netloc='v.lvlt.crcdn.net',
  313. path='%s/%s' % (remove_end(parsed_video_url.path, '/'), video_play_path.split(':')[-1])))
  314. if self._is_valid_url(direct_video_url, video_id, video_format):
  315. format_info.update({
  316. 'url': direct_video_url,
  317. })
  318. formats.append(format_info)
  319. continue
  320. format_info.update({
  321. 'url': video_url,
  322. 'play_path': video_play_path,
  323. 'ext': 'flv',
  324. })
  325. formats.append(format_info)
  326. subtitles = self.extract_subtitles(video_id, webpage)
  327. return {
  328. 'id': video_id,
  329. 'title': video_title,
  330. 'description': video_description,
  331. 'thumbnail': video_thumbnail,
  332. 'uploader': video_uploader,
  333. 'upload_date': video_upload_date,
  334. 'subtitles': subtitles,
  335. 'formats': formats,
  336. }
  337. class CrunchyrollShowPlaylistIE(CrunchyrollBaseIE):
  338. IE_NAME = 'crunchyroll:playlist'
  339. _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\-]+))/?(?:\?|$)'
  340. _TESTS = [{
  341. 'url': 'http://www.crunchyroll.com/a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi',
  342. 'info_dict': {
  343. 'id': 'a-bridge-to-the-starry-skies-hoshizora-e-kakaru-hashi',
  344. 'title': 'A Bridge to the Starry Skies - Hoshizora e Kakaru Hashi'
  345. },
  346. 'playlist_count': 13,
  347. }, {
  348. # geo-restricted (US), 18+ maturity wall, non-premium available
  349. 'url': 'http://www.crunchyroll.com/cosplay-complex-ova',
  350. 'info_dict': {
  351. 'id': 'cosplay-complex-ova',
  352. 'title': 'Cosplay Complex OVA'
  353. },
  354. 'playlist_count': 3,
  355. 'skip': 'Georestricted',
  356. }, {
  357. # geo-restricted (US), 18+ maturity wall, non-premium will be available since 2015.11.14
  358. 'url': 'http://www.crunchyroll.com/ladies-versus-butlers?skip_wall=1',
  359. 'only_matching': True,
  360. }]
  361. def _real_extract(self, url):
  362. show_id = self._match_id(url)
  363. webpage = self._download_webpage(self._add_skip_wall(url), show_id)
  364. title = self._html_search_regex(
  365. r'(?s)<h1[^>]*>\s*<span itemprop="name">(.*?)</span>',
  366. webpage, 'title')
  367. episode_paths = re.findall(
  368. r'(?s)<li id="showview_videos_media_[0-9]+"[^>]+>.*?<a href="([^"]+)"',
  369. webpage)
  370. entries = [
  371. self.url_result('http://www.crunchyroll.com' + ep, 'Crunchyroll')
  372. for ep in episode_paths
  373. ]
  374. entries.reverse()
  375. return {
  376. '_type': 'playlist',
  377. 'id': show_id,
  378. 'title': title,
  379. 'entries': entries,
  380. }