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.

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