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.

136 lines
4.9 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. )
  17. class ADNIE(InfoExtractor):
  18. IE_DESC = 'Anime Digital Network'
  19. _VALID_URL = r'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)'
  20. _TEST = {
  21. 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites',
  22. 'md5': 'e497370d847fd79d9d4c74be55575c7a',
  23. 'info_dict': {
  24. 'id': '7778',
  25. 'ext': 'mp4',
  26. 'title': 'Blue Exorcist - Kyôto Saga - Épisode 1',
  27. 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5',
  28. }
  29. }
  30. def _get_subtitles(self, sub_path, video_id):
  31. if not sub_path:
  32. return None
  33. enc_subtitles = self._download_webpage(
  34. 'http://animedigitalnetwork.fr/' + sub_path,
  35. video_id, fatal=False)
  36. if not enc_subtitles:
  37. return None
  38. # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
  39. dec_subtitles = intlist_to_bytes(aes_cbc_decrypt(
  40. bytes_to_intlist(base64.b64decode(enc_subtitles[24:])),
  41. bytes_to_intlist(b'\nd\xaf\xd2J\xd0\xfc\xe1\xfc\xdf\xb61\xe8\xe1\xf0\xcc'),
  42. bytes_to_intlist(base64.b64decode(enc_subtitles[:24]))
  43. ))
  44. subtitles_json = self._parse_json(
  45. dec_subtitles[:-compat_ord(dec_subtitles[-1])],
  46. None, fatal=False)
  47. if not subtitles_json:
  48. return None
  49. subtitles = {}
  50. for sub_lang, sub in subtitles_json.items():
  51. srt = ''
  52. for num, current in enumerate(sub):
  53. start, end, text = (
  54. float_or_none(current.get('startTime')),
  55. float_or_none(current.get('endTime')),
  56. current.get('text'))
  57. if start is None or end is None or text is None:
  58. continue
  59. srt += os.linesep.join(
  60. (
  61. '%d' % num,
  62. '%s --> %s' % (
  63. srt_subtitles_timecode(start),
  64. srt_subtitles_timecode(end)),
  65. text,
  66. os.linesep,
  67. ))
  68. if sub_lang == 'vostf':
  69. sub_lang = 'fr'
  70. subtitles.setdefault(sub_lang, []).extend([{
  71. 'ext': 'json',
  72. 'data': json.dumps(sub),
  73. }, {
  74. 'ext': 'srt',
  75. 'data': srt,
  76. }])
  77. return subtitles
  78. def _real_extract(self, url):
  79. video_id = self._match_id(url)
  80. webpage = self._download_webpage(url, video_id)
  81. player_config = self._parse_json(self._search_regex(
  82. r'playerConfig\s*=\s*({.+});', webpage, 'player config'), video_id)
  83. video_info = {}
  84. video_info_str = self._search_regex(
  85. r'videoInfo\s*=\s*({.+});', webpage,
  86. 'video info', fatal=False)
  87. if video_info_str:
  88. video_info = self._parse_json(
  89. video_info_str, video_id, fatal=False) or {}
  90. options = player_config.get('options') or {}
  91. metas = options.get('metas') or {}
  92. title = metas.get('title') or video_info['title']
  93. links = player_config.get('links') or {}
  94. formats = []
  95. for format_id, qualities in links.items():
  96. for load_balancer_url in qualities.values():
  97. load_balancer_data = self._download_json(
  98. load_balancer_url, video_id, fatal=False) or {}
  99. m3u8_url = load_balancer_data.get('location')
  100. if not m3u8_url:
  101. continue
  102. m3u8_formats = self._extract_m3u8_formats(
  103. m3u8_url, video_id, 'mp4', 'm3u8_native',
  104. m3u8_id=format_id, fatal=False)
  105. if format_id == 'vf':
  106. for f in m3u8_formats:
  107. f['language'] = 'fr'
  108. formats.extend(m3u8_formats)
  109. error = options.get('error')
  110. if not formats and error:
  111. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  112. self._sort_formats(formats)
  113. return {
  114. 'id': video_id,
  115. 'title': title,
  116. 'description': strip_or_none(metas.get('summary') or video_info.get('resume')),
  117. 'thumbnail': video_info.get('image'),
  118. 'formats': formats,
  119. 'subtitles': self.extract_subtitles(player_config.get('subtitles'), video_id),
  120. 'episode': metas.get('subtitle') or video_info.get('videoTitle'),
  121. 'series': video_info.get('playlistTitle'),
  122. }