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