13 import urllib
.response
16 from .common
import InfoExtractor
17 from ..aes
import aes_ecb_decrypt
31 # NOTE: network handler related code is temporary thing until network stack overhaul PRs are merged (#2861/#2862)
34 def add_opener(ydl
, handler
):
35 ''' Add a handler for opening URLs, like _download_webpage '''
36 # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L426
37 # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L605
38 assert isinstance(ydl
._opener
, urllib
.request
.OpenerDirector
)
39 ydl
._opener
.add_handler(handler
)
42 def remove_opener(ydl
, handler
):
44 Remove handler(s) for opening URLs
45 @param handler Either handler object itself or handler type.
46 Specifying handler type will remove all handler which isinstance returns True.
48 # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L426
49 # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L605
51 assert isinstance(ydl
._opener
, urllib
.request
.OpenerDirector
)
52 if isinstance(handler
, (type, tuple)):
53 find_cp
= lambda x
: isinstance(x
, handler
)
55 find_cp
= lambda x
: x
is handler
58 for meth
in dir(handler
):
59 if meth
in ["redirect_request", "do_open", "proxy_open"]:
60 # oops, coincidental match
65 condition
= meth
[i
+ 1:]
67 if condition
.startswith("error"):
68 j
= condition
.find("_") + i
+ 1
74 lookup
= opener
.handle_error
.get(protocol
, {})
75 opener
.handle_error
[protocol
] = lookup
76 elif condition
== "open":
78 lookup
= opener
.handle_open
79 elif condition
== "response":
81 lookup
= opener
.process_response
82 elif condition
== "request":
84 lookup
= opener
.process_request
88 handlers
= lookup
.setdefault(kind
, [])
90 handlers
[:] = [x
for x
in handlers
if not find_cp(x
)]
92 removed
.append(x
for x
in handlers
if find_cp(x
))
95 for x
in opener
.handlers
:
98 opener
.handlers
[:] = [x
for x
in opener
.handlers
if not find_cp(x
)]
101 class AbemaLicenseHandler(urllib
.request
.BaseHandler
):
103 STRTABLE
= '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
104 HKEY
= b
'3AF0298C219469522A313570E8583005A642E73EDD58E3EA2FB7339D3DF1597E'
106 def __init__(self
, ie
: 'AbemaTVIE'):
107 # the protocol that this should really handle is 'abematv-license://'
108 # abematv_license_open is just a placeholder for development purposes
109 # ref. https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/urllib/request.py#L510
110 setattr(self
, 'abematv-license_open', getattr(self
, 'abematv_license_open'))
113 def _get_videokey_from_ticket(self
, ticket
):
114 to_show
= self
.ie
.get_param('verbose', False)
115 media_token
= self
.ie
._get
_media
_token
(to_show
=to_show
)
117 license_response
= self
.ie
._download
_json
(
118 'https://license.abema.io/abematv-hls', None, note
='Requesting playback license' if to_show
else False,
119 query
={'t': media_token}
,
125 'Content-Type': 'application/json',
128 res
= decode_base_n(license_response
['k'], table
=self
.STRTABLE
)
129 encvideokey
= bytes_to_intlist(struct
.pack('>QQ', res
>> 64, res
& 0xffffffffffffffff))
132 binascii
.unhexlify(self
.HKEY
),
133 (license_response
['cid'] + self
.ie
._DEVICE
_ID
).encode('utf-8'),
134 digestmod
=hashlib
.sha256
)
135 enckey
= bytes_to_intlist(h
.digest())
137 return intlist_to_bytes(aes_ecb_decrypt(encvideokey
, enckey
))
139 def abematv_license_open(self
, url
):
140 url
= request_to_url(url
)
141 ticket
= urllib
.parse
.urlparse(url
).netloc
142 response_data
= self
._get
_videokey
_from
_ticket
(ticket
)
143 return urllib
.response
.addinfourl(io
.BytesIO(response_data
), headers
={
144 'Content-Length': len(response_data
),
145 }, url
=url
, code
=200)
148 class AbemaTVBaseIE(InfoExtractor
):
153 _SECRETKEY
= b
'v+Gjs=25Aw5erR!J8ZuvRrCx*rGswhB&qdHd_SYerEWdU&a?3DzN9BRbp5KwY4hEmcj5#fykMjJ=AuWz5GSMY-d@H7DMEh3M@9n2G552Us$$k9cD=3TxwWe86!x#Zyhe'
156 def _generate_aks(cls
, deviceid
):
157 deviceid
= deviceid
.encode('utf-8')
158 # add 1 hour and then drop minute and secs
159 ts_1hour
= int((time_seconds(hours
=9) // 3600 + 1) * 3600)
160 time_struct
= time
.gmtime(ts_1hour
)
161 ts_1hour_str
= str(ts_1hour
).encode('utf-8')
167 h
= hmac
.new(cls
._SECRETKEY
, digestmod
=hashlib
.sha256
)
173 for i
in range(count
):
176 def mix_twist(nonce
):
178 mix_once(base64
.urlsafe_b64encode(tmp
).rstrip(b
'=') + nonce
)
180 mix_once(cls
._SECRETKEY
)
181 mix_tmp(time_struct
.tm_mon
)
183 mix_tmp(time_struct
.tm_mday
% 5)
184 mix_twist(ts_1hour_str
)
185 mix_tmp(time_struct
.tm_hour
% 5)
187 return base64
.urlsafe_b64encode(tmp
).rstrip(b
'=').decode('utf-8')
189 def _get_device_token(self
):
191 return self
._USERTOKEN
193 AbemaTVBaseIE
._DEVICE
_ID
= str(uuid
.uuid4())
194 aks
= self
._generate
_aks
(self
._DEVICE
_ID
)
195 user_data
= self
._download
_json
(
196 'https://api.abema.io/v1/users', None, note
='Authorizing',
198 'deviceId': self
._DEVICE
_ID
,
199 'applicationKeySecret': aks
,
202 'Content-Type': 'application/json',
204 AbemaTVBaseIE
._USERTOKEN
= user_data
['token']
206 # don't allow adding it 2 times or more, though it's guarded
207 remove_opener(self
._downloader
, AbemaLicenseHandler
)
208 add_opener(self
._downloader
, AbemaLicenseHandler(self
))
210 return self
._USERTOKEN
212 def _get_media_token(self
, invalidate
=False, to_show
=True):
213 if not invalidate
and self
._MEDIATOKEN
:
214 return self
._MEDIATOKEN
216 AbemaTVBaseIE
._MEDIATOKEN
= self
._download
_json
(
217 'https://api.abema.io/v1/media/token', None, note
='Fetching media token' if to_show
else False,
220 'osVersion': '6.0.1',
222 'osTimezone': 'Asia/Tokyo',
224 'appVersion': '3.27.1'
226 'Authorization': f
'bearer {self._get_device_token()}',
229 return self
._MEDIATOKEN
231 def _call_api(self
, endpoint
, video_id
, query
=None, note
='Downloading JSON metadata'):
232 return self
._download
_json
(
233 f
'https://api.abema.io/{endpoint}', video_id
, query
=query
or {},
236 'Authorization': f
'bearer {self._get_device_token()}',
239 def _extract_breadcrumb_list(self
, webpage
, video_id
):
240 for jld
in re
.finditer(
241 r
'(?is)</span></li></ul><script[^>]+type=(["\']?
)application
/ld\
+json\
1[^
>]*>(?P
<json_ld
>.+?
)</script
>',
243 jsonld = self._parse_json(jld.group('json_ld
'), video_id, fatal=False)
244 if traverse_obj(jsonld, '@type') != 'BreadcrumbList
':
246 items = traverse_obj(jsonld, ('itemListElement
', ..., 'name
'))
252 class AbemaTVIE(AbemaTVBaseIE):
253 _VALID_URL = r'https?
://abema\
.tv
/(?P
<type>now
-on
-air|video
/episode|channels
/.+?
/slots
)/(?P
<id>[^?
/]+)'
254 _NETRC_MACHINE = 'abematv
'
256 'url
': 'https
://abema
.tv
/video
/episode
/194-25_s
2_p
1',
258 'id': '194-25_s
2_p
1',
259 'title
': '第
1話 「チーズケーキ」 「モーニング再び」
',
262 'episode
': '第
1話 「チーズケーキ」 「モーニング再び」
',
267 'url
': 'https
://abema
.tv
/channels
/anime
-live2
/slots
/E8tvAnMJ7a9a5d
',
269 'id': 'E8tvAnMJ7a9a5d
',
270 'title
': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ
72時間】
',
271 'series
': 'ゆるキャン△ SEASON2
',
272 'episode
': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ
72時間】
',
275 'description
': 'md5
:9c5a3172ae763278f9303922f0ea5b17
',
279 'url
': 'https
://abema
.tv
/video
/episode
/87-877_s
1282_p
31047',
281 'id': 'E8tvAnMJ7a9a5d
',
283 'description
': 'md5
:56d4fc1b4f7769ded5f923c55bb4695d
',
284 'thumbnail
': r're
:https
://hayabusa\
.io
/.+',
286 'episode
': '第
5話『光射す』
',
290 'url
': 'https
://abema
.tv
/now
-on
-air
/abema
-anime
',
294 # 'title
': '女子高生の無駄づかい 全話一挙【無料ビデオ
72時間】
',
295 'description
': 'md5
:55f2e61f46a17e9230802d7bcc913d5f
',
298 'skip
': 'Not supported until yt
-dlp implements native live downloader OR AbemaTV can start a local HTTP server
',
302 def _perform_login(self, username, password):
303 if '@' in username: # don't strictly check
if it
's email address or not
304 ep, method = 'user
/email
', 'email
'
306 ep, method = 'oneTimePassword
', 'userId
'
308 login_response = self._download_json(
309 f'https
://api
.abema
.io
/v1
/auth
/{ep}
', None, note='Logging
in',
313 }).encode('utf
-8'), headers={
314 'Authorization
': f'bearer {self._get_device_token()}
',
315 'Origin
': 'https
://abema
.tv
',
316 'Referer
': 'https
://abema
.tv
/',
317 'Content
-Type
': 'application
/json
',
320 AbemaTVBaseIE._USERTOKEN = login_response['token
']
321 self._get_media_token(True)
323 def _real_extract(self, url):
324 # starting download using infojson from this extractor is undefined behavior,
325 # and never be fixed in the future; you must trigger downloads by directly specifying URL.
326 # (unless there's a way to hook before downloading by extractor
)
327 video_id
, video_type
= self
._match
_valid
_url
(url
).group('id', 'type')
329 'Authorization': 'Bearer ' + self
._get
_device
_token
(),
331 video_type
= video_type
.split('/')[-1]
333 webpage
= self
._download
_webpage
(url
, video_id
)
334 canonical_url
= self
._search
_regex
(
335 r
'<link\s+rel="canonical"\s*href="(.+?)"', webpage
, 'canonical URL',
337 info
= self
._search
_json
_ld
(webpage
, video_id
, default
={})
339 title
= self
._search
_regex
(
340 r
'<span\s*class=".+?EpisodeTitleBlock__title">(.+?)</span>', webpage
, 'title', default
=None)
343 for jld
in re
.finditer(
344 r
'(?is)<span\s*class="com-m-Thumbnail__image">(?:</span>)?<script[^>]+type=(["\']?
)application
/ld\
+json\
1[^
>]*>(?P
<json_ld
>.+?
)</script
>',
346 jsonld = self._parse_json(jld.group('json_ld
'), video_id, fatal=False)
350 title = jsonld.get('caption
')
351 if not title and video_type == 'now
-on
-air
':
352 if not self._TIMETABLE:
353 # cache the timetable because it goes to 5MiB in size (!!)
354 self._TIMETABLE = self._download_json(
355 'https
://api
.abema
.io
/v1
/timetable
/dataSet?debug
=false
', video_id,
357 now = time_seconds(hours=9)
358 for slot in self._TIMETABLE.get('slots
', []):
359 if slot.get('channelId
') != video_id:
361 if slot['startAt
'] <= now and now < slot['endAt
']:
362 title = slot['title
']
365 # read breadcrumb on top of page
366 breadcrumb = self._extract_breadcrumb_list(webpage, video_id)
368 # breadcrumb list translates to: (e.g. 1st test for this IE)
369 # Home > Anime (genre) > Isekai Shokudo 2 (series name) > Episode 1 "Cheese cakes" "Morning again" (episode title)
371 info['series
'] = breadcrumb[-2]
372 info['episode
'] = breadcrumb[-1]
374 title = info['episode
']
376 description = self._html_search_regex(
377 (r'<p\s
+class="com-video-EpisodeDetailsBlock__content"><span\s
+class=".+?">(.+?
)</span
></p
><div
',
378 r'<span\s
+class=".+?SlotSummary.+?">(.+?
)</span
></div
><div
',),
379 webpage, 'description
', default=None, group=1)
381 og_desc = self._html_search_meta(
382 ('description
', 'og
:description
', 'twitter
:description
'), webpage)
384 description = re.sub(r'''(?sx)
386 アニメの動画を無料で見るならABEMA!| # anime
387 等、.+ # applies for most of categories
391 # canonical URL may contain series and episode number
392 mobj = re.search(r's(\d
+)_p(\d
+)$
', canonical_url)
394 seri = int_or_none(mobj.group(1), default=float('inf
'))
395 epis = int_or_none(mobj.group(2), default=float('inf
'))
396 info['series_number
'] = seri if seri < 100 else None
397 # some anime like Detective Conan (though not available in AbemaTV)
398 # has more than 1000 episodes (1026 as of 2021/11/15)
399 info['episode_number
'] = epis if epis < 2000 else None
401 is_live, m3u8_url = False, None
402 if video_type == 'now
-on
-air
':
404 channel_url = 'https
://api
.abema
.io
/v1
/channels
'
405 if video_id == 'news
-global':
406 channel_url = update_url_query(channel_url, {'division': '1'})
407 onair_channels = self._download_json(channel_url, video_id)
408 for ch in onair_channels['channels
']:
409 if video_id == ch['id']:
410 m3u8_url = ch['playback
']['hls
']
413 raise ExtractorError(f'Cannot find on
-air {video_id} channel
.', expected=True)
414 elif video_type == 'episode
':
415 api_response = self._download_json(
416 f'https
://api
.abema
.io
/v1
/video
/programs
/{video_id}
', video_id,
417 note='Checking playability
',
419 ondemand_types = traverse_obj(api_response, ('terms
', ..., 'onDemandType
'), default=[])
420 if 3 not in ondemand_types:
421 # cannot acquire decryption key for these streams
422 self.report_warning('This
is a premium
-only stream
')
424 m3u8_url = f'https
://vod
-abematv
.akamaized
.net
/program
/{video_id}
/playlist
.m3u8
'
425 elif video_type == 'slots
':
426 api_response = self._download_json(
427 f'https
://api
.abema
.io
/v1
/media
/slots
/{video_id}
', video_id,
428 note='Checking playability
',
430 if not traverse_obj(api_response, ('slot
', 'flags
', 'timeshiftFree
'), default=False):
431 self.report_warning('This
is a premium
-only stream
')
433 m3u8_url = f'https
://vod
-abematv
.akamaized
.net
/slot
/{video_id}
/playlist
.m3u8
'
435 raise ExtractorError('Unreachable
')
438 self.report_warning("This is a livestream; yt-dlp doesn't support downloading natively
, but FFmpeg cannot handle m3u8 manifests
from AbemaTV
")
439 self.report_warning('Please consider using Streamlink to download these streams (https://github.com/streamlink/streamlink)')
440 formats = self._extract_m3u8_formats(
441 m3u8_url, video_id, ext='mp4', live=is_live)
446 'description': description,
453 class AbemaTVTitleIE(AbemaTVBaseIE):
454 _VALID_URL = r'https?://abema\.tv/video/title/(?P<id>[^?/]+)'
458 'url': 'https://abema.tv/video/title/90-1597',
461 'title': 'シャッフルアイランド',
463 'playlist_mincount': 2,
465 'url': 'https://abema.tv/video/title/193-132',
468 'title': '真心が届く~僕とスターのオフィス・ラブ!?~',
470 'playlist_mincount': 16,
472 'url': 'https://abema.tv/video/title/25-102',
475 'title': 'ソードアート・オンライン アリシゼーション',
477 'playlist_mincount': 24,
480 def _fetch_page(self, playlist_id, series_version, page):
481 programs = self._call_api(
482 f'v1/video/series/{playlist_id}/programs', playlist_id,
483 note=f'Downloading page {page + 1}',
485 'seriesVersion': series_version,
486 'offset': str(page * self._PAGE_SIZE),
488 'limit': str(self._PAGE_SIZE),
491 self.url_result(f'https://abema.tv/video/episode/{x}')
492 for x in traverse_obj(programs, ('programs', ..., 'id'), default=[]))
494 def _entries(self, playlist_id, series_version):
495 return OnDemandPagedList(
496 functools.partial(self._fetch_page, playlist_id, series_version),
499 def _real_extract(self, url):
500 playlist_id = self._match_id(url)
501 series_info = self._call_api(f'v1/video/series/{playlist_id}', playlist_id)
503 return self.playlist_result(
504 self._entries(playlist_id, series_info['version']), playlist_id=playlist_id,
505 playlist_title=series_info.get('title'),
506 playlist_description=series_info.get('content'))