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