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.

150 lines
5.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import json
  5. import os
  6. from .common import InfoExtractor
  7. from ..aes import aes_cbc_decrypt
  8. from ..compat import compat_ord
  9. from ..utils import (
  10. bytes_to_intlist,
  11. ExtractorError,
  12. float_or_none,
  13. intlist_to_bytes,
  14. srt_subtitles_timecode,
  15. strip_or_none,
  16. urljoin,
  17. )
  18. class ADNIE(InfoExtractor):
  19. IE_DESC = 'Anime Digital Network'
  20. _VALID_URL = r'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)'
  21. _TEST = {
  22. 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites',
  23. 'md5': 'e497370d847fd79d9d4c74be55575c7a',
  24. 'info_dict': {
  25. 'id': '7778',
  26. 'ext': 'mp4',
  27. 'title': 'Blue Exorcist - Kyôto Saga - Épisode 1',
  28. 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5',
  29. }
  30. }
  31. _BASE_URL = 'http://animedigitalnetwork.fr'
  32. def _get_subtitles(self, sub_path, video_id):
  33. if not sub_path:
  34. return None
  35. enc_subtitles = self._download_webpage(
  36. urljoin(self._BASE_URL, sub_path),
  37. video_id, fatal=False, headers={
  38. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0',
  39. })
  40. if not enc_subtitles:
  41. return None
  42. # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
  43. dec_subtitles = intlist_to_bytes(aes_cbc_decrypt(
  44. bytes_to_intlist(base64.b64decode(enc_subtitles[24:])),
  45. bytes_to_intlist(b'\x1b\xe0\x29\x61\x38\x94\x24\x00\x12\xbd\xc5\x80\xac\xce\xbe\xb0'),
  46. bytes_to_intlist(base64.b64decode(enc_subtitles[:24]))
  47. ))
  48. subtitles_json = self._parse_json(
  49. dec_subtitles[:-compat_ord(dec_subtitles[-1])].decode(),
  50. None, fatal=False)
  51. if not subtitles_json:
  52. return None
  53. subtitles = {}
  54. for sub_lang, sub in subtitles_json.items():
  55. srt = ''
  56. for num, current in enumerate(sub):
  57. start, end, text = (
  58. float_or_none(current.get('startTime')),
  59. float_or_none(current.get('endTime')),
  60. current.get('text'))
  61. if start is None or end is None or text is None:
  62. continue
  63. srt += os.linesep.join(
  64. (
  65. '%d' % num,
  66. '%s --> %s' % (
  67. srt_subtitles_timecode(start),
  68. srt_subtitles_timecode(end)),
  69. text,
  70. os.linesep,
  71. ))
  72. if sub_lang == 'vostf':
  73. sub_lang = 'fr'
  74. subtitles.setdefault(sub_lang, []).extend([{
  75. 'ext': 'json',
  76. 'data': json.dumps(sub),
  77. }, {
  78. 'ext': 'srt',
  79. 'data': srt,
  80. }])
  81. return subtitles
  82. def _real_extract(self, url):
  83. video_id = self._match_id(url)
  84. webpage = self._download_webpage(url, video_id)
  85. player_config = self._parse_json(self._search_regex(
  86. r'playerConfig\s*=\s*({.+});', webpage, 'player config'), video_id)
  87. video_info = {}
  88. video_info_str = self._search_regex(
  89. r'videoInfo\s*=\s*({.+});', webpage,
  90. 'video info', fatal=False)
  91. if video_info_str:
  92. video_info = self._parse_json(
  93. video_info_str, video_id, fatal=False) or {}
  94. options = player_config.get('options') or {}
  95. metas = options.get('metas') or {}
  96. title = metas.get('title') or video_info['title']
  97. links = player_config.get('links') or {}
  98. error = None
  99. if not links:
  100. links_url = player_config['linksurl']
  101. links_data = self._download_json(urljoin(
  102. self._BASE_URL, links_url), video_id)
  103. links = links_data.get('links') or {}
  104. error = links_data.get('error')
  105. formats = []
  106. for format_id, qualities in links.items():
  107. if not isinstance(qualities, dict):
  108. continue
  109. for load_balancer_url in qualities.values():
  110. load_balancer_data = self._download_json(
  111. load_balancer_url, video_id, fatal=False) or {}
  112. m3u8_url = load_balancer_data.get('location')
  113. if not m3u8_url:
  114. continue
  115. m3u8_formats = self._extract_m3u8_formats(
  116. m3u8_url, video_id, 'mp4', 'm3u8_native',
  117. m3u8_id=format_id, fatal=False)
  118. if format_id == 'vf':
  119. for f in m3u8_formats:
  120. f['language'] = 'fr'
  121. formats.extend(m3u8_formats)
  122. if not error:
  123. error = options.get('error')
  124. if not formats and error:
  125. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  126. self._sort_formats(formats)
  127. return {
  128. 'id': video_id,
  129. 'title': title,
  130. 'description': strip_or_none(metas.get('summary') or video_info.get('resume')),
  131. 'thumbnail': video_info.get('image'),
  132. 'formats': formats,
  133. 'subtitles': self.extract_subtitles(player_config.get('subtitles'), video_id),
  134. 'episode': metas.get('subtitle') or video_info.get('videoTitle'),
  135. 'series': video_info.get('playlistTitle'),
  136. }