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.

159 lines
5.4 KiB

  1. from __future__ import unicode_literals
  2. import base64
  3. import json
  4. import random
  5. import re
  6. from .common import InfoExtractor
  7. from ..aes import (
  8. aes_cbc_decrypt,
  9. aes_cbc_encrypt,
  10. )
  11. from ..utils import (
  12. bytes_to_intlist,
  13. bytes_to_long,
  14. clean_html,
  15. ExtractorError,
  16. intlist_to_bytes,
  17. get_element_by_id,
  18. js_to_json,
  19. int_or_none,
  20. long_to_bytes,
  21. pkcs1pad,
  22. remove_end,
  23. )
  24. class DaisukiIE(InfoExtractor):
  25. _VALID_URL = r'https?://(?:www\.)?daisuki\.net/[^/]+/[^/]+/[^/]+/watch\.[^.]+\.(?P<id>\d+)\.html'
  26. _TEST = {
  27. 'url': 'http://www.daisuki.net/tw/en/anime/watch.TheIdolMasterCG.11213.html',
  28. 'info_dict': {
  29. 'id': '11213',
  30. 'ext': 'mp4',
  31. 'title': '#01 Who is in the pumpkin carriage? - THE IDOLM@STER CINDERELLA GIRLS',
  32. 'subtitles': {
  33. 'mul': [{
  34. 'ext': 'ttml',
  35. }],
  36. },
  37. 'creator': 'BANDAI NAMCO Entertainment',
  38. },
  39. 'params': {
  40. 'skip_download': True, # AES-encrypted HLS stream
  41. },
  42. }
  43. # The public key in PEM format can be found in clientlibs_anime_watch.min.js
  44. _RSA_KEY = (0xc5524c25e8e14b366b3754940beeb6f96cb7e2feef0b932c7659a0c5c3bf173d602464c2df73d693b513ae06ff1be8f367529ab30bf969c5640522181f2a0c51ea546ae120d3d8d908595e4eff765b389cde080a1ef7f1bbfb07411cc568db73b7f521cedf270cbfbe0ddbc29b1ac9d0f2d8f4359098caffee6d07915020077d, 65537)
  45. def _real_extract(self, url):
  46. video_id = self._match_id(url)
  47. webpage = self._download_webpage(url, video_id)
  48. flashvars = self._parse_json(self._search_regex(
  49. r'(?s)var\s+flashvars\s*=\s*({.+?});', webpage, 'flashvars'),
  50. video_id, transform_source=js_to_json)
  51. iv = [0] * 16
  52. data = {}
  53. for key in ('device_cd', 'mv_id', 'ss1_prm', 'ss2_prm', 'ss3_prm', 'ss_id'):
  54. data[key] = flashvars.get(key, '')
  55. encrypted_rtn = None
  56. # Some AES keys are rejected. Try it with different AES keys
  57. for idx in range(5):
  58. aes_key = [random.randint(0, 254) for _ in range(32)]
  59. padded_aeskey = intlist_to_bytes(pkcs1pad(aes_key, 128))
  60. n, e = self._RSA_KEY
  61. encrypted_aeskey = long_to_bytes(pow(bytes_to_long(padded_aeskey), e, n))
  62. init_data = self._download_json('http://www.daisuki.net/bin/bgn/init', video_id, query={
  63. 's': flashvars.get('s', ''),
  64. 'c': flashvars.get('ss3_prm', ''),
  65. 'e': url,
  66. 'd': base64.b64encode(intlist_to_bytes(aes_cbc_encrypt(
  67. bytes_to_intlist(json.dumps(data)),
  68. aes_key, iv))).decode('ascii'),
  69. 'a': base64.b64encode(encrypted_aeskey).decode('ascii'),
  70. }, note='Downloading JSON metadata' + (' (try #%d)' % (idx + 1) if idx > 0 else ''))
  71. if 'rtn' in init_data:
  72. encrypted_rtn = init_data['rtn']
  73. break
  74. self._sleep(5, video_id)
  75. if encrypted_rtn is None:
  76. raise ExtractorError('Failed to fetch init data')
  77. rtn = self._parse_json(
  78. intlist_to_bytes(aes_cbc_decrypt(bytes_to_intlist(
  79. base64.b64decode(encrypted_rtn)),
  80. aes_key, iv)).decode('utf-8').rstrip('\0'),
  81. video_id)
  82. formats = self._extract_m3u8_formats(
  83. rtn['play_url'], video_id, ext='mp4', entry_protocol='m3u8_native')
  84. title = remove_end(self._og_search_title(webpage), ' - DAISUKI')
  85. creator = self._html_search_regex(
  86. r'Creator\s*:\s*([^<]+)', webpage, 'creator', fatal=False)
  87. subtitles = {}
  88. caption_url = rtn.get('caption_url')
  89. if caption_url:
  90. # mul: multiple languages
  91. subtitles['mul'] = [{
  92. 'url': caption_url,
  93. 'ext': 'ttml',
  94. }]
  95. return {
  96. 'id': video_id,
  97. 'title': title,
  98. 'formats': formats,
  99. 'subtitles': subtitles,
  100. 'creator': creator,
  101. }
  102. class DaisukiPlaylistIE(InfoExtractor):
  103. _VALID_URL = r'https?://(?:www\.)daisuki\.net/[^/]+/[^/]+/[^/]+/detail\.(?P<id>[a-zA-Z0-9]+)\.html'
  104. _TEST = {
  105. 'url': 'http://www.daisuki.net/tw/en/anime/detail.TheIdolMasterCG.html',
  106. 'info_dict': {
  107. 'id': 'TheIdolMasterCG',
  108. 'title': 'THE IDOLM@STER CINDERELLA GIRLS',
  109. 'description': 'md5:0f2c028a9339f7a2c7fbf839edc5c5d8',
  110. },
  111. 'playlist_count': 26,
  112. }
  113. def _real_extract(self, url):
  114. playlist_id = self._match_id(url)
  115. webpage = self._download_webpage(url, playlist_id)
  116. episode_pattern = r'''(?sx)
  117. <img[^>]+delay="[^"]+/(\d+)/movie\.jpg".+?
  118. <p[^>]+class=".*?\bepisodeNumber\b.*?">(?:<a[^>]+>)?([^<]+)'''
  119. entries = [{
  120. '_type': 'url_transparent',
  121. 'url': url.replace('detail', 'watch').replace('.html', '.' + movie_id + '.html'),
  122. 'episode_id': episode_id,
  123. 'episode_number': int_or_none(episode_id),
  124. } for movie_id, episode_id in re.findall(episode_pattern, webpage)]
  125. playlist_title = remove_end(
  126. self._og_search_title(webpage, fatal=False), ' - Anime - DAISUKI')
  127. playlist_description = clean_html(get_element_by_id('synopsisTxt', webpage))
  128. return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)