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.

173 lines
6.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import binascii
  5. import json
  6. import os
  7. import random
  8. from .common import InfoExtractor
  9. from ..aes import aes_cbc_decrypt
  10. from ..compat import (
  11. compat_b64decode,
  12. compat_ord,
  13. )
  14. from ..utils import (
  15. bytes_to_intlist,
  16. bytes_to_long,
  17. ExtractorError,
  18. float_or_none,
  19. intlist_to_bytes,
  20. long_to_bytes,
  21. pkcs1pad,
  22. srt_subtitles_timecode,
  23. strip_or_none,
  24. urljoin,
  25. )
  26. class ADNIE(InfoExtractor):
  27. IE_DESC = 'Anime Digital Network'
  28. _VALID_URL = r'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)'
  29. _TEST = {
  30. 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites',
  31. 'md5': 'e497370d847fd79d9d4c74be55575c7a',
  32. 'info_dict': {
  33. 'id': '7778',
  34. 'ext': 'mp4',
  35. 'title': 'Blue Exorcist - Kyôto Saga - Épisode 1',
  36. 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5',
  37. }
  38. }
  39. _BASE_URL = 'http://animedigitalnetwork.fr'
  40. _RSA_KEY = (0xc35ae1e4356b65a73b551493da94b8cb443491c0aa092a357a5aee57ffc14dda85326f42d716e539a34542a0d3f363adf16c5ec222d713d5997194030ee2e4f0d1fb328c01a81cf6868c090d50de8e169c6b13d1675b9eeed1cbc51e1fffca9b38af07f37abd790924cd3bee59d0257cfda4fe5f3f0534877e21ce5821447d1b, 65537)
  41. def _get_subtitles(self, sub_path, video_id):
  42. if not sub_path:
  43. return None
  44. enc_subtitles = self._download_webpage(
  45. urljoin(self._BASE_URL, sub_path),
  46. video_id, fatal=False)
  47. if not enc_subtitles:
  48. return None
  49. # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
  50. dec_subtitles = intlist_to_bytes(aes_cbc_decrypt(
  51. bytes_to_intlist(compat_b64decode(enc_subtitles[24:])),
  52. bytes_to_intlist(binascii.unhexlify(self._K + '9032ad7083106400')),
  53. bytes_to_intlist(compat_b64decode(enc_subtitles[:24]))
  54. ))
  55. subtitles_json = self._parse_json(
  56. dec_subtitles[:-compat_ord(dec_subtitles[-1])].decode(),
  57. None, fatal=False)
  58. if not subtitles_json:
  59. return None
  60. subtitles = {}
  61. for sub_lang, sub in subtitles_json.items():
  62. srt = ''
  63. for num, current in enumerate(sub):
  64. start, end, text = (
  65. float_or_none(current.get('startTime')),
  66. float_or_none(current.get('endTime')),
  67. current.get('text'))
  68. if start is None or end is None or text is None:
  69. continue
  70. srt += os.linesep.join(
  71. (
  72. '%d' % num,
  73. '%s --> %s' % (
  74. srt_subtitles_timecode(start),
  75. srt_subtitles_timecode(end)),
  76. text,
  77. os.linesep,
  78. ))
  79. if sub_lang == 'vostf':
  80. sub_lang = 'fr'
  81. subtitles.setdefault(sub_lang, []).extend([{
  82. 'ext': 'json',
  83. 'data': json.dumps(sub),
  84. }, {
  85. 'ext': 'srt',
  86. 'data': srt,
  87. }])
  88. return subtitles
  89. def _real_extract(self, url):
  90. video_id = self._match_id(url)
  91. webpage = self._download_webpage(url, video_id)
  92. player_config = self._parse_json(self._search_regex(
  93. r'playerConfig\s*=\s*({.+});', webpage, 'player config'), video_id)
  94. video_info = {}
  95. video_info_str = self._search_regex(
  96. r'videoInfo\s*=\s*({.+});', webpage,
  97. 'video info', fatal=False)
  98. if video_info_str:
  99. video_info = self._parse_json(
  100. video_info_str, video_id, fatal=False) or {}
  101. options = player_config.get('options') or {}
  102. metas = options.get('metas') or {}
  103. links = player_config.get('links') or {}
  104. sub_path = player_config.get('subtitles')
  105. error = None
  106. if not links:
  107. links_url = player_config.get('linksurl') or options['videoUrl']
  108. token = options['token']
  109. self._K = ''.join([random.choice('0123456789abcdef') for _ in range(16)])
  110. message = bytes_to_intlist(json.dumps({
  111. 'k': self._K,
  112. 'e': 60,
  113. 't': token,
  114. }))
  115. padded_message = intlist_to_bytes(pkcs1pad(message, 128))
  116. n, e = self._RSA_KEY
  117. encrypted_message = long_to_bytes(pow(bytes_to_long(padded_message), e, n))
  118. authorization = base64.b64encode(encrypted_message).decode()
  119. links_data = self._download_json(
  120. urljoin(self._BASE_URL, links_url), video_id, headers={
  121. 'Authorization': 'Bearer ' + authorization,
  122. })
  123. links = links_data.get('links') or {}
  124. metas = metas or links_data.get('meta') or {}
  125. sub_path = (sub_path or links_data.get('subtitles')) + '&token=' + token
  126. error = links_data.get('error')
  127. title = metas.get('title') or video_info['title']
  128. formats = []
  129. for format_id, qualities in links.items():
  130. if not isinstance(qualities, dict):
  131. continue
  132. for load_balancer_url in qualities.values():
  133. load_balancer_data = self._download_json(
  134. load_balancer_url, video_id, fatal=False) or {}
  135. m3u8_url = load_balancer_data.get('location')
  136. if not m3u8_url:
  137. continue
  138. m3u8_formats = self._extract_m3u8_formats(
  139. m3u8_url, video_id, 'mp4', 'm3u8_native',
  140. m3u8_id=format_id, fatal=False)
  141. if format_id == 'vf':
  142. for f in m3u8_formats:
  143. f['language'] = 'fr'
  144. formats.extend(m3u8_formats)
  145. if not error:
  146. error = options.get('error')
  147. if not formats and error:
  148. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  149. self._sort_formats(formats)
  150. return {
  151. 'id': video_id,
  152. 'title': title,
  153. 'description': strip_or_none(metas.get('summary') or video_info.get('resume')),
  154. 'thumbnail': video_info.get('image'),
  155. 'formats': formats,
  156. 'subtitles': self.extract_subtitles(sub_path, video_id),
  157. 'episode': metas.get('subtitle') or video_info.get('videoTitle'),
  158. 'series': video_info.get('playlistTitle'),
  159. }