]>
Commit | Line | Data |
---|---|---|
82be732b RA |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
1ea559c4 RA |
4 | import base64 |
5 | import binascii | |
82be732b RA |
6 | import json |
7 | import os | |
1ea559c4 | 8 | import random |
82be732b RA |
9 | |
10 | from .common import InfoExtractor | |
11 | from ..aes import aes_cbc_decrypt | |
cf282071 S |
12 | from ..compat import ( |
13 | compat_b64decode, | |
14 | compat_ord, | |
15 | ) | |
82be732b RA |
16 | from ..utils import ( |
17 | bytes_to_intlist, | |
1ea559c4 | 18 | bytes_to_long, |
82be732b RA |
19 | ExtractorError, |
20 | float_or_none, | |
21 | intlist_to_bytes, | |
1ea559c4 RA |
22 | long_to_bytes, |
23 | pkcs1pad, | |
82be732b | 24 | strip_or_none, |
20e2c9de | 25 | urljoin, |
82be732b RA |
26 | ) |
27 | ||
28 | ||
29 | class ADNIE(InfoExtractor): | |
30 | IE_DESC = 'Anime Digital Network' | |
31 | _VALID_URL = r'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)' | |
32 | _TEST = { | |
33 | 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites', | |
34 | 'md5': 'e497370d847fd79d9d4c74be55575c7a', | |
35 | 'info_dict': { | |
36 | 'id': '7778', | |
37 | 'ext': 'mp4', | |
38 | 'title': 'Blue Exorcist - Kyôto Saga - Épisode 1', | |
39 | 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5', | |
40 | } | |
41 | } | |
20e2c9de | 42 | _BASE_URL = 'http://animedigitalnetwork.fr' |
1ea559c4 | 43 | _RSA_KEY = (0xc35ae1e4356b65a73b551493da94b8cb443491c0aa092a357a5aee57ffc14dda85326f42d716e539a34542a0d3f363adf16c5ec222d713d5997194030ee2e4f0d1fb328c01a81cf6868c090d50de8e169c6b13d1675b9eeed1cbc51e1fffca9b38af07f37abd790924cd3bee59d0257cfda4fe5f3f0534877e21ce5821447d1b, 65537) |
b966740c RA |
44 | _POS_ALIGN_MAP = { |
45 | 'start': 1, | |
46 | 'end': 3, | |
47 | } | |
48 | _LINE_ALIGN_MAP = { | |
49 | 'middle': 8, | |
50 | 'end': 4, | |
51 | } | |
52 | ||
53 | @staticmethod | |
54 | def _ass_subtitles_timecode(seconds): | |
55 | return '%01d:%02d:%02d.%02d' % (seconds / 3600, (seconds % 3600) / 60, seconds % 60, (seconds % 1) * 100) | |
82be732b RA |
56 | |
57 | def _get_subtitles(self, sub_path, video_id): | |
58 | if not sub_path: | |
59 | return None | |
60 | ||
61 | enc_subtitles = self._download_webpage( | |
20e2c9de | 62 | urljoin(self._BASE_URL, sub_path), |
b966740c | 63 | video_id, 'Downloading subtitles data', fatal=False) |
82be732b RA |
64 | if not enc_subtitles: |
65 | return None | |
66 | ||
67 | # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js | |
68 | dec_subtitles = intlist_to_bytes(aes_cbc_decrypt( | |
cf282071 | 69 | bytes_to_intlist(compat_b64decode(enc_subtitles[24:])), |
b966740c | 70 | bytes_to_intlist(binascii.unhexlify(self._K + '083db5aebd9353b4')), |
cf282071 | 71 | bytes_to_intlist(compat_b64decode(enc_subtitles[:24])) |
82be732b RA |
72 | )) |
73 | subtitles_json = self._parse_json( | |
20e2c9de | 74 | dec_subtitles[:-compat_ord(dec_subtitles[-1])].decode(), |
82be732b RA |
75 | None, fatal=False) |
76 | if not subtitles_json: | |
77 | return None | |
78 | ||
79 | subtitles = {} | |
80 | for sub_lang, sub in subtitles_json.items(): | |
b966740c RA |
81 | ssa = '''[Script Info] |
82 | ScriptType:V4.00 | |
83 | [V4 Styles] | |
2bbde1d0 RA |
84 | Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,TertiaryColour,BackColour,Bold,Italic,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,AlphaLevel,Encoding |
85 | Style: Default,Arial,18,16777215,16777215,16777215,0,-1,0,1,1,0,2,20,20,20,0,0 | |
b966740c | 86 | [Events] |
2bbde1d0 | 87 | Format: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text''' |
b966740c RA |
88 | for current in sub: |
89 | start, end, text, line_align, position_align = ( | |
82be732b RA |
90 | float_or_none(current.get('startTime')), |
91 | float_or_none(current.get('endTime')), | |
b966740c RA |
92 | current.get('text'), current.get('lineAlign'), |
93 | current.get('positionAlign')) | |
82be732b RA |
94 | if start is None or end is None or text is None: |
95 | continue | |
b966740c | 96 | alignment = self._POS_ALIGN_MAP.get(position_align, 2) + self._LINE_ALIGN_MAP.get(line_align, 0) |
2bbde1d0 | 97 | ssa += os.linesep + 'Dialogue: Marked=0,%s,%s,Default,,0,0,0,,%s%s' % ( |
b966740c RA |
98 | self._ass_subtitles_timecode(start), |
99 | self._ass_subtitles_timecode(end), | |
100 | '{\\a%d}' % alignment if alignment != 2 else '', | |
101 | text.replace('\n', '\\N').replace('<i>', '{\\i1}').replace('</i>', '{\\i0}')) | |
82be732b RA |
102 | |
103 | if sub_lang == 'vostf': | |
104 | sub_lang = 'fr' | |
105 | subtitles.setdefault(sub_lang, []).extend([{ | |
106 | 'ext': 'json', | |
107 | 'data': json.dumps(sub), | |
108 | }, { | |
b966740c RA |
109 | 'ext': 'ssa', |
110 | 'data': ssa, | |
82be732b RA |
111 | }]) |
112 | return subtitles | |
113 | ||
114 | def _real_extract(self, url): | |
115 | video_id = self._match_id(url) | |
116 | webpage = self._download_webpage(url, video_id) | |
117 | player_config = self._parse_json(self._search_regex( | |
b966740c RA |
118 | r'playerConfig\s*=\s*({.+});', webpage, |
119 | 'player config', default='{}'), video_id, fatal=False) | |
120 | if not player_config: | |
121 | config_url = urljoin(self._BASE_URL, self._search_regex( | |
122 | r'(?:id="player"|class="[^"]*adn-player-container[^"]*")[^>]+data-url="([^"]+)"', | |
123 | webpage, 'config url')) | |
124 | player_config = self._download_json( | |
125 | config_url, video_id, | |
126 | 'Downloading player config JSON metadata')['player'] | |
82be732b RA |
127 | |
128 | video_info = {} | |
129 | video_info_str = self._search_regex( | |
130 | r'videoInfo\s*=\s*({.+});', webpage, | |
131 | 'video info', fatal=False) | |
132 | if video_info_str: | |
133 | video_info = self._parse_json( | |
134 | video_info_str, video_id, fatal=False) or {} | |
135 | ||
136 | options = player_config.get('options') or {} | |
137 | metas = options.get('metas') or {} | |
82be732b | 138 | links = player_config.get('links') or {} |
44dc11db | 139 | sub_path = player_config.get('subtitles') |
83d00044 | 140 | error = None |
20e2c9de | 141 | if not links: |
44dc11db | 142 | links_url = player_config.get('linksurl') or options['videoUrl'] |
1ea559c4 RA |
143 | token = options['token'] |
144 | self._K = ''.join([random.choice('0123456789abcdef') for _ in range(16)]) | |
145 | message = bytes_to_intlist(json.dumps({ | |
146 | 'k': self._K, | |
147 | 'e': 60, | |
148 | 't': token, | |
149 | })) | |
150 | padded_message = intlist_to_bytes(pkcs1pad(message, 128)) | |
151 | n, e = self._RSA_KEY | |
152 | encrypted_message = long_to_bytes(pow(bytes_to_long(padded_message), e, n)) | |
153 | authorization = base64.b64encode(encrypted_message).decode() | |
154 | links_data = self._download_json( | |
b966740c RA |
155 | urljoin(self._BASE_URL, links_url), video_id, |
156 | 'Downloading links JSON metadata', headers={ | |
1ea559c4 RA |
157 | 'Authorization': 'Bearer ' + authorization, |
158 | }) | |
20e2c9de | 159 | links = links_data.get('links') or {} |
44dc11db | 160 | metas = metas or links_data.get('meta') or {} |
b966740c RA |
161 | sub_path = sub_path or links_data.get('subtitles') or \ |
162 | 'index.php?option=com_vodapi&task=subtitles.getJSON&format=json&id=' + video_id | |
163 | sub_path += '&token=' + token | |
83d00044 | 164 | error = links_data.get('error') |
44dc11db | 165 | title = metas.get('title') or video_info['title'] |
82be732b RA |
166 | |
167 | formats = [] | |
168 | for format_id, qualities in links.items(): | |
20e2c9de RA |
169 | if not isinstance(qualities, dict): |
170 | continue | |
b966740c | 171 | for quality, load_balancer_url in qualities.items(): |
82be732b | 172 | load_balancer_data = self._download_json( |
b966740c RA |
173 | load_balancer_url, video_id, |
174 | 'Downloading %s %s JSON metadata' % (format_id, quality), | |
175 | fatal=False) or {} | |
82be732b RA |
176 | m3u8_url = load_balancer_data.get('location') |
177 | if not m3u8_url: | |
178 | continue | |
179 | m3u8_formats = self._extract_m3u8_formats( | |
180 | m3u8_url, video_id, 'mp4', 'm3u8_native', | |
181 | m3u8_id=format_id, fatal=False) | |
182 | if format_id == 'vf': | |
183 | for f in m3u8_formats: | |
184 | f['language'] = 'fr' | |
185 | formats.extend(m3u8_formats) | |
83d00044 S |
186 | if not error: |
187 | error = options.get('error') | |
82be732b RA |
188 | if not formats and error: |
189 | raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True) | |
190 | self._sort_formats(formats) | |
191 | ||
192 | return { | |
193 | 'id': video_id, | |
194 | 'title': title, | |
195 | 'description': strip_or_none(metas.get('summary') or video_info.get('resume')), | |
196 | 'thumbnail': video_info.get('image'), | |
197 | 'formats': formats, | |
44dc11db | 198 | 'subtitles': self.extract_subtitles(sub_path, video_id), |
82be732b RA |
199 | 'episode': metas.get('subtitle') or video_info.get('videoTitle'), |
200 | 'series': video_info.get('playlistTitle'), | |
201 | } |