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.

155 lines
5.7 KiB

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