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.

154 lines
5.1 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 ..compat import compat_b64decode
  12. from ..utils import (
  13. bytes_to_intlist,
  14. bytes_to_long,
  15. extract_attributes,
  16. ExtractorError,
  17. intlist_to_bytes,
  18. js_to_json,
  19. int_or_none,
  20. long_to_bytes,
  21. pkcs1pad,
  22. )
  23. class DaisukiMottoIE(InfoExtractor):
  24. _VALID_URL = r'https?://motto\.daisuki\.net/framewatch/embed/[^/]+/(?P<id>[0-9a-zA-Z]{3})'
  25. _TEST = {
  26. 'url': 'http://motto.daisuki.net/framewatch/embed/embedDRAGONBALLSUPERUniverseSurvivalsaga/V2e/760/428',
  27. 'info_dict': {
  28. 'id': 'V2e',
  29. 'ext': 'mp4',
  30. 'title': '#117 SHOWDOWN OF LOVE! ANDROIDS VS UNIVERSE 2!!',
  31. 'subtitles': {
  32. 'mul': [{
  33. 'ext': 'ttml',
  34. }],
  35. },
  36. },
  37. 'params': {
  38. 'skip_download': True, # AES-encrypted HLS stream
  39. },
  40. }
  41. # The public key in PEM format can be found in clientlibs_anime_watch.min.js
  42. _RSA_KEY = (0xc5524c25e8e14b366b3754940beeb6f96cb7e2feef0b932c7659a0c5c3bf173d602464c2df73d693b513ae06ff1be8f367529ab30bf969c5640522181f2a0c51ea546ae120d3d8d908595e4eff765b389cde080a1ef7f1bbfb07411cc568db73b7f521cedf270cbfbe0ddbc29b1ac9d0f2d8f4359098caffee6d07915020077d, 65537)
  43. def _real_extract(self, url):
  44. video_id = self._match_id(url)
  45. webpage = self._download_webpage(url, video_id)
  46. flashvars = self._parse_json(self._search_regex(
  47. r'(?s)var\s+flashvars\s*=\s*({.+?});', webpage, 'flashvars'),
  48. video_id, transform_source=js_to_json)
  49. iv = [0] * 16
  50. data = {}
  51. for key in ('device_cd', 'mv_id', 'ss1_prm', 'ss2_prm', 'ss3_prm', 'ss_id'):
  52. data[key] = flashvars.get(key, '')
  53. encrypted_rtn = None
  54. # Some AES keys are rejected. Try it with different AES keys
  55. for idx in range(5):
  56. aes_key = [random.randint(0, 254) for _ in range(32)]
  57. padded_aeskey = intlist_to_bytes(pkcs1pad(aes_key, 128))
  58. n, e = self._RSA_KEY
  59. encrypted_aeskey = long_to_bytes(pow(bytes_to_long(padded_aeskey), e, n))
  60. init_data = self._download_json(
  61. 'http://motto.daisuki.net/fastAPI/bgn/init/',
  62. 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. compat_b64decode(encrypted_rtn)),
  80. aes_key, iv)).decode('utf-8').rstrip('\0'),
  81. video_id)
  82. title = rtn['title_str']
  83. formats = self._extract_m3u8_formats(
  84. rtn['play_url'], video_id, ext='mp4', entry_protocol='m3u8_native')
  85. subtitles = {}
  86. caption_url = rtn.get('caption_url')
  87. if caption_url:
  88. # mul: multiple languages
  89. subtitles['mul'] = [{
  90. 'url': caption_url,
  91. 'ext': 'ttml',
  92. }]
  93. return {
  94. 'id': video_id,
  95. 'title': title,
  96. 'formats': formats,
  97. 'subtitles': subtitles,
  98. }
  99. class DaisukiMottoPlaylistIE(InfoExtractor):
  100. _VALID_URL = r'https?://motto\.daisuki\.net/(?P<id>information)/'
  101. _TEST = {
  102. 'url': 'http://motto.daisuki.net/information/',
  103. 'info_dict': {
  104. 'title': 'DRAGON BALL SUPER',
  105. },
  106. 'playlist_mincount': 117,
  107. }
  108. def _real_extract(self, url):
  109. playlist_id = self._match_id(url)
  110. webpage = self._download_webpage(url, playlist_id)
  111. entries = []
  112. for li in re.findall(r'(<li[^>]+?data-product_id="[a-zA-Z0-9]{3}"[^>]+>)', webpage):
  113. attr = extract_attributes(li)
  114. ad_id = attr.get('data-ad_id')
  115. product_id = attr.get('data-product_id')
  116. if ad_id and product_id:
  117. episode_id = attr.get('data-chapter')
  118. entries.append({
  119. '_type': 'url_transparent',
  120. 'url': 'http://motto.daisuki.net/framewatch/embed/%s/%s/760/428' % (ad_id, product_id),
  121. 'episode_id': episode_id,
  122. 'episode_number': int_or_none(episode_id),
  123. 'ie_key': 'DaisukiMotto',
  124. })
  125. return self.playlist_result(entries, playlist_title='DRAGON BALL SUPER')