7 from .common
import InfoExtractor
8 from ..aes
import aes_cbc_decrypt_bytes
, unpad_pkcs7
14 ass_subtitles_timecode
,
30 class ADNIE(InfoExtractor
):
31 IE_DESC
= 'Anime Digital Network'
32 _VALID_URL
= r
'https?://(?:www\.)?animedigitalnetwork\.fr/video/[^/]+/(?P<id>\d+)'
34 'url': 'http://animedigitalnetwork.fr/video/blue-exorcist-kyoto-saga/7778-episode-1-debut-des-hostilites',
35 'md5': '0319c99885ff5547565cacb4f3f9348d',
39 'title': 'Blue Exorcist - Kyôto Saga - Episode 1',
40 'description': 'md5:2f7b5aa76edbc1a7a92cedcda8a528d5',
41 'series': 'Blue Exorcist - Kyôto Saga',
43 'release_date': '20170106',
45 'average_rating': float,
47 'episode': 'Début des hostilités',
52 _NETRC_MACHINE
= 'animedigitalnetwork'
53 _BASE_URL
= 'http://animedigitalnetwork.fr'
54 _API_BASE_URL
= 'https://gw.api.animedigitalnetwork.fr/'
55 _PLAYER_BASE_URL
= _API_BASE_URL
+ 'player/'
57 _LOGIN_ERR_MESSAGE
= 'Unable to log in'
58 _RSA_KEY
= (0x9B42B08905199A5CCE2026274399CA560ECB209EE9878A708B1C0812E1BB8CB5D1FB7441861147C1A1F2F3A0476DD63A9CAC20D3E983613346850AA6CB38F16DC7D720FD7D86FC6E5B3D5BBC72E14CD0BF9E869F2CEA2CCAD648F1DCE38F1FF916CEFB2D339B64AA0264372344BC775E265E8A852F88144AB0BD9AA06C1A4ABB, 65537)
68 def _get_subtitles(self
, sub_url
, video_id
):
72 enc_subtitles
= self
._download
_webpage
(
73 sub_url
, video_id
, 'Downloading subtitles location', fatal
=False) or '{}'
74 subtitle_location
= (self
._parse
_json
(enc_subtitles
, video_id
, fatal
=False) or {}).get('location')
76 enc_subtitles
= self
._download
_webpage
(
77 subtitle_location
, video_id
, 'Downloading subtitles data',
78 fatal
=False, headers
={'Origin': 'https://animedigitalnetwork.fr'}
)
82 # http://animedigitalnetwork.fr/components/com_vodvideo/videojs/adn-vjs.min.js
83 dec_subtitles
= unpad_pkcs7(aes_cbc_decrypt_bytes(
84 compat_b64decode(enc_subtitles
[24:]),
85 binascii
.unhexlify(self
._K
+ '7fac1178830cfe0c'),
86 compat_b64decode(enc_subtitles
[:24])))
87 subtitles_json
= self
._parse
_json
(dec_subtitles
.decode(), None, fatal
=False)
88 if not subtitles_json
:
92 for sub_lang
, sub
in subtitles_json
.items():
93 ssa
= '''[Script Info]
96 Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,TertiaryColour,BackColour,Bold,Italic,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,AlphaLevel,Encoding
97 Style: Default,Arial,18,16777215,16777215,16777215,0,-1,0,1,1,0,2,20,20,20,0,0
99 Format: Marked,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text'''
101 start
, end
, text
, line_align
, position_align
= (
102 float_or_none(current
.get('startTime')),
103 float_or_none(current
.get('endTime')),
104 current
.get('text'), current
.get('lineAlign'),
105 current
.get('positionAlign'))
106 if start
is None or end
is None or text
is None:
108 alignment
= self
._POS
_ALIGN
_MAP
.get(position_align
, 2) + self
._LINE
_ALIGN
_MAP
.get(line_align
, 0)
109 ssa
+= os
.linesep
+ 'Dialogue: Marked=0,%s,%s,Default,,0,0,0,,%s%s' % (
110 ass_subtitles_timecode(start
),
111 ass_subtitles_timecode(end
),
112 '{\\a%d}' % alignment
if alignment
!= 2 else '',
113 text
.replace('\n', '\\N').replace('<i>', '{\\i1}').replace('</i>', '{\\i0}'))
115 if sub_lang
== 'vostf':
117 subtitles
.setdefault(sub_lang
, []).extend([{
119 'data': json
.dumps(sub
),
126 def _perform_login(self
, username
, password
):
128 access_token
= (self
._download
_json
(
129 self
._API
_BASE
_URL
+ 'authentication/login', None,
130 'Logging in', self
._LOGIN
_ERR
_MESSAGE
, fatal
=False,
131 data
=urlencode_postdata({
132 'password': password
,
135 'username': username
,
136 })) or {}).get('accessToken')
138 self
._HEADERS
= {'authorization': 'Bearer ' + access_token}
139 except ExtractorError
as e
:
141 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 401:
142 resp
= self
._parse
_json
(
143 e
.cause
.read().decode(), None, fatal
=False) or {}
144 message
= resp
.get('message') or resp
.get('code')
145 self
.report_warning(message
or self
._LOGIN
_ERR
_MESSAGE
)
147 def _real_extract(self
, url
):
148 video_id
= self
._match
_id
(url
)
149 video_base_url
= self
._PLAYER
_BASE
_URL
+ 'video/%s/' % video_id
150 player
= self
._download
_json
(
151 video_base_url
+ 'configuration', video_id
,
152 'Downloading player config JSON metadata',
153 headers
=self
._HEADERS
)['player']
154 options
= player
['options']
156 user
= options
['user']
157 if not user
.get('hasAccess'):
158 self
.raise_login_required()
160 token
= self
._download
_json
(
161 user
.get('refreshTokenUrl') or (self
._PLAYER
_BASE
_URL
+ 'refresh/token'),
162 video_id
, 'Downloading access token', headers
={
163 'x-player-refresh-token': user
['refreshToken']
164 }, data
=b
'')['token']
166 links_url
= try_get(options
, lambda x
: x
['video']['url']) or (video_base_url
+ 'link')
167 self
._K
= ''.join([random
.choice('0123456789abcdef') for _
in range(16)])
168 message
= bytes_to_intlist(json
.dumps({
173 # Sometimes authentication fails for no good reason, retry with
174 # a different random padding
177 padded_message
= intlist_to_bytes(pkcs1pad(message
, 128))
179 encrypted_message
= long_to_bytes(pow(bytes_to_long(padded_message
), e
, n
))
180 authorization
= base64
.b64encode(encrypted_message
).decode()
183 links_data
= self
._download
_json
(
184 links_url
, video_id
, 'Downloading links JSON metadata', headers
={
185 'X-Player-Token': authorization
187 'freeWithAds': 'true',
189 'withMetadata': 'true',
193 except ExtractorError
as e
:
194 if not isinstance(e
.cause
, compat_HTTPError
):
197 if e
.cause
.code
== 401:
198 # This usually goes away with a different random pkcs1pad, so retry
201 error
= self
._parse
_json
(e
.cause
.read(), video_id
)
202 message
= error
.get('message')
203 if e
.cause
.code
== 403 and error
.get('code') == 'player-bad-geolocation-country':
204 self
.raise_geo_restricted(msg
=message
)
205 raise ExtractorError(message
)
207 raise ExtractorError('Giving up retrying')
209 links
= links_data
.get('links') or {}
210 metas
= links_data
.get('metadata') or {}
211 sub_url
= (links
.get('subtitles') or {}).get('all')
212 video_info
= links_data
.get('video') or {}
213 title
= metas
['title']
216 for format_id
, qualities
in (links
.get('streaming') or {}).items():
217 if not isinstance(qualities
, dict):
219 for quality
, load_balancer_url
in qualities
.items():
220 load_balancer_data
= self
._download
_json
(
221 load_balancer_url
, video_id
,
222 'Downloading %s %s JSON metadata' % (format_id
, quality
),
224 m3u8_url
= load_balancer_data
.get('location')
227 m3u8_formats
= self
._extract
_m
3u8_formats
(
228 m3u8_url
, video_id
, 'mp4', 'm3u8_native',
229 m3u8_id
=format_id
, fatal
=False)
230 if format_id
== 'vf':
231 for f
in m3u8_formats
:
233 formats
.extend(m3u8_formats
)
234 self
._sort
_formats
(formats
)
236 video
= (self
._download
_json
(
237 self
._API
_BASE
_URL
+ 'video/%s' % video_id
, video_id
,
238 'Downloading additional video metadata', fatal
=False) or {}).get('video') or {}
239 show
= video
.get('show') or {}
244 'description': strip_or_none(metas
.get('summary') or video
.get('summary')),
245 'thumbnail': video_info
.get('image') or player
.get('image'),
247 'subtitles': self
.extract_subtitles(sub_url
, video_id
),
248 'episode': metas
.get('subtitle') or video
.get('name'),
249 'episode_number': int_or_none(video
.get('shortNumber')),
250 'series': show
.get('title'),
251 'season_number': int_or_none(video
.get('season')),
252 'duration': int_or_none(video_info
.get('duration') or video
.get('duration')),
253 'release_date': unified_strdate(video
.get('releaseDate')),
254 'average_rating': float_or_none(video
.get('rating') or metas
.get('rating')),
255 'comment_count': int_or_none(video
.get('commentsCount')),