from ..aes import aes_ecb_decrypt
from ..utils import (
ExtractorError,
+ OnDemandPagedList,
bytes_to_intlist,
decode_base_n,
int_or_none,
intlist_to_bytes,
- OnDemandPagedList,
- request_to_url,
time_seconds,
traverse_obj,
update_url_query,
)
+from ..utils.networking import clean_proxies
-# NOTE: network handler related code is temporary thing until network stack overhaul PRs are merged (#2861/#2862)
-
-
-def add_opener(ydl, handler):
- ''' Add a handler for opening URLs, like _download_webpage '''
- # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L426
- # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L605
- assert isinstance(ydl._opener, urllib.request.OpenerDirector)
- ydl._opener.add_handler(handler)
-
-def remove_opener(ydl, handler):
- '''
- Remove handler(s) for opening URLs
- @param handler Either handler object itself or handler type.
- Specifying handler type will remove all handler which isinstance returns True.
- '''
+def add_opener(ydl, handler): # FIXME: Create proper API in .networking
+ """Add a handler for opening URLs, like _download_webpage"""
# https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L426
# https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L605
- opener = ydl._opener
- assert isinstance(ydl._opener, urllib.request.OpenerDirector)
- if isinstance(handler, (type, tuple)):
- find_cp = lambda x: isinstance(x, handler)
- else:
- find_cp = lambda x: x is handler
-
- removed = []
- for meth in dir(handler):
- if meth in ["redirect_request", "do_open", "proxy_open"]:
- # oops, coincidental match
- continue
-
- i = meth.find("_")
- protocol = meth[:i]
- condition = meth[i + 1:]
-
- if condition.startswith("error"):
- j = condition.find("_") + i + 1
- kind = meth[j + 1:]
- try:
- kind = int(kind)
- except ValueError:
- pass
- lookup = opener.handle_error.get(protocol, {})
- opener.handle_error[protocol] = lookup
- elif condition == "open":
- kind = protocol
- lookup = opener.handle_open
- elif condition == "response":
- kind = protocol
- lookup = opener.process_response
- elif condition == "request":
- kind = protocol
- lookup = opener.process_request
- else:
- continue
-
- handlers = lookup.setdefault(kind, [])
- if handlers:
- handlers[:] = [x for x in handlers if not find_cp(x)]
-
- removed.append(x for x in handlers if find_cp(x))
-
- if removed:
- for x in opener.handlers:
- if find_cp(x):
- x.add_parent(None)
- opener.handlers[:] = [x for x in opener.handlers if not find_cp(x)]
+ rh = ydl._request_director.handlers['Urllib']
+ if 'abematv-license' in rh._SUPPORTED_URL_SCHEMES:
+ return
+ headers = ydl.params['http_headers'].copy()
+ proxies = ydl.proxies.copy()
+ clean_proxies(proxies, headers)
+ opener = rh._get_instance(cookiejar=ydl.cookiejar, proxies=proxies)
+ assert isinstance(opener, urllib.request.OpenerDirector)
+ opener.add_handler(handler)
+ rh._SUPPORTED_URL_SCHEMES = (*rh._SUPPORTED_URL_SCHEMES, 'abematv-license')
class AbemaLicenseHandler(urllib.request.BaseHandler):
# the protocol that this should really handle is 'abematv-license://'
# abematv_license_open is just a placeholder for development purposes
# ref. https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/urllib/request.py#L510
- setattr(self, 'abematv-license_open', getattr(self, 'abematv_license_open'))
+ setattr(self, 'abematv-license_open', getattr(self, 'abematv_license_open', None))
self.ie = ie
def _get_videokey_from_ticket(self, ticket):
query={'t': media_token},
data=json.dumps({
'kv': 'a',
- 'lt': ticket
- }).encode('utf-8'),
+ 'lt': ticket,
+ }).encode(),
headers={
'Content-Type': 'application/json',
})
h = hmac.new(
binascii.unhexlify(self.HKEY),
- (license_response['cid'] + self.ie._DEVICE_ID).encode('utf-8'),
+ (license_response['cid'] + self.ie._DEVICE_ID).encode(),
digestmod=hashlib.sha256)
enckey = bytes_to_intlist(h.digest())
return intlist_to_bytes(aes_ecb_decrypt(encvideokey, enckey))
def abematv_license_open(self, url):
- url = request_to_url(url)
+ url = url.get_full_url() if isinstance(url, urllib.request.Request) else url
ticket = urllib.parse.urlparse(url).netloc
response_data = self._get_videokey_from_ticket(ticket)
return urllib.response.addinfourl(io.BytesIO(response_data), headers={
- 'Content-Length': len(response_data),
+ 'Content-Length': str(len(response_data)),
}, url=url, code=200)
class AbemaTVBaseIE(InfoExtractor):
+ _NETRC_MACHINE = 'abematv'
+
_USERTOKEN = None
_DEVICE_ID = None
_MEDIATOKEN = None
@classmethod
def _generate_aks(cls, deviceid):
- deviceid = deviceid.encode('utf-8')
+ deviceid = deviceid.encode()
# add 1 hour and then drop minute and secs
- ts_1hour = int((time_seconds(hours=9) // 3600 + 1) * 3600)
+ ts_1hour = int((time_seconds() // 3600 + 1) * 3600)
time_struct = time.gmtime(ts_1hour)
- ts_1hour_str = str(ts_1hour).encode('utf-8')
+ ts_1hour_str = str(ts_1hour).encode()
tmp = None
def mix_tmp(count):
nonlocal tmp
- for i in range(count):
+ for _ in range(count):
mix_once(tmp)
def mix_twist(nonce):
if self._USERTOKEN:
return self._USERTOKEN
+ add_opener(self._downloader, AbemaLicenseHandler(self))
+
+ username, _ = self._get_login_info()
+ auth_cache = username and self.cache.load(self._NETRC_MACHINE, username, min_ver='2024.01.19')
+ AbemaTVBaseIE._USERTOKEN = auth_cache and auth_cache.get('usertoken')
+ if AbemaTVBaseIE._USERTOKEN:
+ # try authentication with locally stored token
+ try:
+ AbemaTVBaseIE._DEVICE_ID = auth_cache.get('device_id')
+ self._get_media_token(True)
+ return
+ except ExtractorError as e:
+ self.report_warning(f'Failed to login with cached user token; obtaining a fresh one ({e})')
+
AbemaTVBaseIE._DEVICE_ID = str(uuid.uuid4())
aks = self._generate_aks(self._DEVICE_ID)
user_data = self._download_json(
data=json.dumps({
'deviceId': self._DEVICE_ID,
'applicationKeySecret': aks,
- }).encode('utf-8'),
+ }).encode(),
headers={
'Content-Type': 'application/json',
})
AbemaTVBaseIE._USERTOKEN = user_data['token']
- # don't allow adding it 2 times or more, though it's guarded
- remove_opener(self._downloader, AbemaLicenseHandler)
- add_opener(self._downloader, AbemaLicenseHandler(self))
-
return self._USERTOKEN
def _get_media_token(self, invalidate=False, to_show=True):
'osLang': 'ja_JP',
'osTimezone': 'Asia/Tokyo',
'appId': 'tv.abema',
- 'appVersion': '3.27.1'
+ 'appVersion': '3.27.1',
}, headers={
'Authorization': f'bearer {self._get_device_token()}',
})['token']
return self._MEDIATOKEN
+ def _perform_login(self, username, password):
+ self._get_device_token()
+ if self.cache.load(self._NETRC_MACHINE, username, min_ver='2024.01.19') and self._get_media_token():
+ self.write_debug('Skipping logging in')
+ return
+
+ if '@' in username: # don't strictly check if it's email address or not
+ ep, method = 'user/email', 'email'
+ else:
+ ep, method = 'oneTimePassword', 'userId'
+
+ login_response = self._download_json(
+ f'https://api.abema.io/v1/auth/{ep}', None, note='Logging in',
+ data=json.dumps({
+ method: username,
+ 'password': password,
+ }).encode(), headers={
+ 'Authorization': f'bearer {self._get_device_token()}',
+ 'Origin': 'https://abema.tv',
+ 'Referer': 'https://abema.tv/',
+ 'Content-Type': 'application/json',
+ })
+
+ AbemaTVBaseIE._USERTOKEN = login_response['token']
+ self._get_media_token(True)
+ auth_cache = {
+ 'device_id': AbemaTVBaseIE._DEVICE_ID,
+ 'usertoken': AbemaTVBaseIE._USERTOKEN,
+ }
+ self.cache.store(self._NETRC_MACHINE, username, auth_cache)
+
def _call_api(self, endpoint, video_id, query=None, note='Downloading JSON metadata'):
return self._download_json(
f'https://api.abema.io/{endpoint}', video_id, query=query or {},
class AbemaTVIE(AbemaTVBaseIE):
_VALID_URL = r'https?://abema\.tv/(?P<type>now-on-air|video/episode|channels/.+?/slots)/(?P<id>[^?/]+)'
- _NETRC_MACHINE = 'abematv'
_TESTS = [{
'url': 'https://abema.tv/video/episode/194-25_s2_p1',
'info_dict': {
'id': '194-25_s2_p1',
'title': '第1話 「チーズケーキ」 「モーニング再び」',
'series': '異世界食堂2',
- 'series_number': 2,
+ 'season': 'シーズン2',
+ 'season_number': 2,
'episode': '第1話 「チーズケーキ」 「モーニング再び」',
'episode_number': 1,
},
'title': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ72時間】',
'series': 'ゆるキャン△ SEASON2',
'episode': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ72時間】',
- 'series_number': 2,
+ 'season_number': 2,
'episode_number': 1,
'description': 'md5:9c5a3172ae763278f9303922f0ea5b17',
},
}]
_TIMETABLE = None
- def _perform_login(self, username, password):
- if '@' in username: # don't strictly check if it's email address or not
- ep, method = 'user/email', 'email'
- else:
- ep, method = 'oneTimePassword', 'userId'
-
- login_response = self._download_json(
- f'https://api.abema.io/v1/auth/{ep}', None, note='Logging in',
- data=json.dumps({
- method: username,
- 'password': password
- }).encode('utf-8'), headers={
- 'Authorization': f'bearer {self._get_device_token()}',
- 'Origin': 'https://abema.tv',
- 'Referer': 'https://abema.tv/',
- 'Content-Type': 'application/json',
- })
-
- AbemaTVBaseIE._USERTOKEN = login_response['token']
- self._get_media_token(True)
-
def _real_extract(self, url):
# starting download using infojson from this extractor is undefined behavior,
# and never be fixed in the future; you must trigger downloads by directly specifying URL.
# read breadcrumb on top of page
breadcrumb = self._extract_breadcrumb_list(webpage, video_id)
if breadcrumb:
- # breadcrumb list translates to: (example is 1st test for this IE)
+ # breadcrumb list translates to: (e.g. 1st test for this IE)
# Home > Anime (genre) > Isekai Shokudo 2 (series name) > Episode 1 "Cheese cakes" "Morning again" (episode title)
# hence this works
info['series'] = breadcrumb[-2]
description = self._html_search_regex(
(r'<p\s+class="com-video-EpisodeDetailsBlock__content"><span\s+class=".+?">(.+?)</span></p><div',
- r'<span\s+class=".+?SlotSummary.+?">(.+?)</span></div><div',),
+ r'<span\s+class=".+?SlotSummary.+?">(.+?)</span></div><div'),
webpage, 'description', default=None, group=1)
if not description:
og_desc = self._html_search_meta(
)?
''', r'\1', og_desc)
- # canonical URL may contain series and episode number
+ # canonical URL may contain season and episode number
mobj = re.search(r's(\d+)_p(\d+)$', canonical_url)
if mobj:
seri = int_or_none(mobj.group(1), default=float('inf'))
epis = int_or_none(mobj.group(2), default=float('inf'))
- info['series_number'] = seri if seri < 100 else None
+ info['season_number'] = seri if seri < 100 else None
# some anime like Detective Conan (though not available in AbemaTV)
# has more than 1000 episodes (1026 as of 2021/11/15)
info['episode_number'] = epis if epis < 2000 else None
f'https://api.abema.io/v1/video/programs/{video_id}', video_id,
note='Checking playability',
headers=headers)
- ondemand_types = traverse_obj(api_response, ('terms', ..., 'onDemandType'), default=[])
+ ondemand_types = traverse_obj(api_response, ('terms', ..., 'onDemandType'))
if 3 not in ondemand_types:
# cannot acquire decryption key for these streams
self.report_warning('This is a premium-only stream')
+ info.update(traverse_obj(api_response, {
+ 'series': ('series', 'title'),
+ 'season': ('season', 'name'),
+ 'season_number': ('season', 'sequence'),
+ 'episode_number': ('episode', 'number'),
+ }))
+ if not title:
+ title = traverse_obj(api_response, ('episode', 'title'))
+ if not description:
+ description = traverse_obj(api_response, ('episode', 'content'))
m3u8_url = f'https://vod-abematv.akamaized.net/program/{video_id}/playlist.m3u8'
elif video_type == 'slots':
})
yield from (
self.url_result(f'https://abema.tv/video/episode/{x}')
- for x in traverse_obj(programs, ('programs', ..., 'id'), default=[]))
+ for x in traverse_obj(programs, ('programs', ..., 'id')))
def _entries(self, playlist_id, series_version):
return OnDemandPagedList(