abematv.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. import base64
  2. import binascii
  3. import functools
  4. import hashlib
  5. import hmac
  6. import io
  7. import json
  8. import re
  9. import struct
  10. import time
  11. import urllib.parse
  12. import urllib.request
  13. import urllib.response
  14. import uuid
  15. from .common import InfoExtractor
  16. from ..aes import aes_ecb_decrypt
  17. from ..utils import (
  18. ExtractorError,
  19. OnDemandPagedList,
  20. bytes_to_intlist,
  21. decode_base_n,
  22. int_or_none,
  23. intlist_to_bytes,
  24. time_seconds,
  25. traverse_obj,
  26. update_url_query,
  27. )
  28. from ..utils.networking import clean_proxies
  29. def add_opener(ydl, handler): # FIXME: Create proper API in .networking
  30. """Add a handler for opening URLs, like _download_webpage"""
  31. # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L426
  32. # https://github.com/python/cpython/blob/main/Lib/urllib/request.py#L605
  33. rh = ydl._request_director.handlers['Urllib']
  34. if 'abematv-license' in rh._SUPPORTED_URL_SCHEMES:
  35. return
  36. headers = ydl.params['http_headers'].copy()
  37. proxies = ydl.proxies.copy()
  38. clean_proxies(proxies, headers)
  39. opener = rh._get_instance(cookiejar=ydl.cookiejar, proxies=proxies)
  40. assert isinstance(opener, urllib.request.OpenerDirector)
  41. opener.add_handler(handler)
  42. rh._SUPPORTED_URL_SCHEMES = (*rh._SUPPORTED_URL_SCHEMES, 'abematv-license')
  43. class AbemaLicenseHandler(urllib.request.BaseHandler):
  44. handler_order = 499
  45. STRTABLE = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  46. HKEY = b'3AF0298C219469522A313570E8583005A642E73EDD58E3EA2FB7339D3DF1597E'
  47. def __init__(self, ie: 'AbemaTVIE'):
  48. # the protocol that this should really handle is 'abematv-license://'
  49. # abematv_license_open is just a placeholder for development purposes
  50. # ref. https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/urllib/request.py#L510
  51. setattr(self, 'abematv-license_open', getattr(self, 'abematv_license_open', None))
  52. self.ie = ie
  53. def _get_videokey_from_ticket(self, ticket):
  54. to_show = self.ie.get_param('verbose', False)
  55. media_token = self.ie._get_media_token(to_show=to_show)
  56. license_response = self.ie._download_json(
  57. 'https://license.abema.io/abematv-hls', None, note='Requesting playback license' if to_show else False,
  58. query={'t': media_token},
  59. data=json.dumps({
  60. 'kv': 'a',
  61. 'lt': ticket,
  62. }).encode(),
  63. headers={
  64. 'Content-Type': 'application/json',
  65. })
  66. res = decode_base_n(license_response['k'], table=self.STRTABLE)
  67. encvideokey = bytes_to_intlist(struct.pack('>QQ', res >> 64, res & 0xffffffffffffffff))
  68. h = hmac.new(
  69. binascii.unhexlify(self.HKEY),
  70. (license_response['cid'] + self.ie._DEVICE_ID).encode(),
  71. digestmod=hashlib.sha256)
  72. enckey = bytes_to_intlist(h.digest())
  73. return intlist_to_bytes(aes_ecb_decrypt(encvideokey, enckey))
  74. def abematv_license_open(self, url):
  75. url = url.get_full_url() if isinstance(url, urllib.request.Request) else url
  76. ticket = urllib.parse.urlparse(url).netloc
  77. response_data = self._get_videokey_from_ticket(ticket)
  78. return urllib.response.addinfourl(io.BytesIO(response_data), headers={
  79. 'Content-Length': str(len(response_data)),
  80. }, url=url, code=200)
  81. class AbemaTVBaseIE(InfoExtractor):
  82. _NETRC_MACHINE = 'abematv'
  83. _USERTOKEN = None
  84. _DEVICE_ID = None
  85. _MEDIATOKEN = None
  86. _SECRETKEY = b'v+Gjs=25Aw5erR!J8ZuvRrCx*rGswhB&qdHd_SYerEWdU&a?3DzN9BRbp5KwY4hEmcj5#fykMjJ=AuWz5GSMY-d@H7DMEh3M@9n2G552Us$$k9cD=3TxwWe86!x#Zyhe'
  87. @classmethod
  88. def _generate_aks(cls, deviceid):
  89. deviceid = deviceid.encode()
  90. # add 1 hour and then drop minute and secs
  91. ts_1hour = int((time_seconds() // 3600 + 1) * 3600)
  92. time_struct = time.gmtime(ts_1hour)
  93. ts_1hour_str = str(ts_1hour).encode()
  94. tmp = None
  95. def mix_once(nonce):
  96. nonlocal tmp
  97. h = hmac.new(cls._SECRETKEY, digestmod=hashlib.sha256)
  98. h.update(nonce)
  99. tmp = h.digest()
  100. def mix_tmp(count):
  101. nonlocal tmp
  102. for _ in range(count):
  103. mix_once(tmp)
  104. def mix_twist(nonce):
  105. nonlocal tmp
  106. mix_once(base64.urlsafe_b64encode(tmp).rstrip(b'=') + nonce)
  107. mix_once(cls._SECRETKEY)
  108. mix_tmp(time_struct.tm_mon)
  109. mix_twist(deviceid)
  110. mix_tmp(time_struct.tm_mday % 5)
  111. mix_twist(ts_1hour_str)
  112. mix_tmp(time_struct.tm_hour % 5)
  113. return base64.urlsafe_b64encode(tmp).rstrip(b'=').decode('utf-8')
  114. def _get_device_token(self):
  115. if self._USERTOKEN:
  116. return self._USERTOKEN
  117. add_opener(self._downloader, AbemaLicenseHandler(self))
  118. username, _ = self._get_login_info()
  119. auth_cache = username and self.cache.load(self._NETRC_MACHINE, username, min_ver='2024.01.19')
  120. AbemaTVBaseIE._USERTOKEN = auth_cache and auth_cache.get('usertoken')
  121. if AbemaTVBaseIE._USERTOKEN:
  122. # try authentication with locally stored token
  123. try:
  124. AbemaTVBaseIE._DEVICE_ID = auth_cache.get('device_id')
  125. self._get_media_token(True)
  126. return
  127. except ExtractorError as e:
  128. self.report_warning(f'Failed to login with cached user token; obtaining a fresh one ({e})')
  129. AbemaTVBaseIE._DEVICE_ID = str(uuid.uuid4())
  130. aks = self._generate_aks(self._DEVICE_ID)
  131. user_data = self._download_json(
  132. 'https://api.abema.io/v1/users', None, note='Authorizing',
  133. data=json.dumps({
  134. 'deviceId': self._DEVICE_ID,
  135. 'applicationKeySecret': aks,
  136. }).encode(),
  137. headers={
  138. 'Content-Type': 'application/json',
  139. })
  140. AbemaTVBaseIE._USERTOKEN = user_data['token']
  141. return self._USERTOKEN
  142. def _get_media_token(self, invalidate=False, to_show=True):
  143. if not invalidate and self._MEDIATOKEN:
  144. return self._MEDIATOKEN
  145. AbemaTVBaseIE._MEDIATOKEN = self._download_json(
  146. 'https://api.abema.io/v1/media/token', None, note='Fetching media token' if to_show else False,
  147. query={
  148. 'osName': 'android',
  149. 'osVersion': '6.0.1',
  150. 'osLang': 'ja_JP',
  151. 'osTimezone': 'Asia/Tokyo',
  152. 'appId': 'tv.abema',
  153. 'appVersion': '3.27.1',
  154. }, headers={
  155. 'Authorization': f'bearer {self._get_device_token()}',
  156. })['token']
  157. return self._MEDIATOKEN
  158. def _perform_login(self, username, password):
  159. self._get_device_token()
  160. if self.cache.load(self._NETRC_MACHINE, username, min_ver='2024.01.19') and self._get_media_token():
  161. self.write_debug('Skipping logging in')
  162. return
  163. if '@' in username: # don't strictly check if it's email address or not
  164. ep, method = 'user/email', 'email'
  165. else:
  166. ep, method = 'oneTimePassword', 'userId'
  167. login_response = self._download_json(
  168. f'https://api.abema.io/v1/auth/{ep}', None, note='Logging in',
  169. data=json.dumps({
  170. method: username,
  171. 'password': password,
  172. }).encode(), headers={
  173. 'Authorization': f'bearer {self._get_device_token()}',
  174. 'Origin': 'https://abema.tv',
  175. 'Referer': 'https://abema.tv/',
  176. 'Content-Type': 'application/json',
  177. })
  178. AbemaTVBaseIE._USERTOKEN = login_response['token']
  179. self._get_media_token(True)
  180. auth_cache = {
  181. 'device_id': AbemaTVBaseIE._DEVICE_ID,
  182. 'usertoken': AbemaTVBaseIE._USERTOKEN,
  183. }
  184. self.cache.store(self._NETRC_MACHINE, username, auth_cache)
  185. def _call_api(self, endpoint, video_id, query=None, note='Downloading JSON metadata'):
  186. return self._download_json(
  187. f'https://api.abema.io/{endpoint}', video_id, query=query or {},
  188. note=note,
  189. headers={
  190. 'Authorization': f'bearer {self._get_device_token()}',
  191. })
  192. def _extract_breadcrumb_list(self, webpage, video_id):
  193. for jld in re.finditer(
  194. r'(?is)</span></li></ul><script[^>]+type=(["\']?)application/ld\+json\1[^>]*>(?P<json_ld>.+?)</script>',
  195. webpage):
  196. jsonld = self._parse_json(jld.group('json_ld'), video_id, fatal=False)
  197. if traverse_obj(jsonld, '@type') != 'BreadcrumbList':
  198. continue
  199. items = traverse_obj(jsonld, ('itemListElement', ..., 'name'))
  200. if items:
  201. return items
  202. return []
  203. class AbemaTVIE(AbemaTVBaseIE):
  204. _VALID_URL = r'https?://abema\.tv/(?P<type>now-on-air|video/episode|channels/.+?/slots)/(?P<id>[^?/]+)'
  205. _TESTS = [{
  206. 'url': 'https://abema.tv/video/episode/194-25_s2_p1',
  207. 'info_dict': {
  208. 'id': '194-25_s2_p1',
  209. 'title': '第1話 「チーズケーキ」 「モーニング再び」',
  210. 'series': '異世界食堂2',
  211. 'season': 'シーズン2',
  212. 'season_number': 2,
  213. 'episode': '第1話 「チーズケーキ」 「モーニング再び」',
  214. 'episode_number': 1,
  215. },
  216. 'skip': 'expired',
  217. }, {
  218. 'url': 'https://abema.tv/channels/anime-live2/slots/E8tvAnMJ7a9a5d',
  219. 'info_dict': {
  220. 'id': 'E8tvAnMJ7a9a5d',
  221. 'title': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ72時間】',
  222. 'series': 'ゆるキャン△ SEASON2',
  223. 'episode': 'ゆるキャン△ SEASON2 全話一挙【無料ビデオ72時間】',
  224. 'season_number': 2,
  225. 'episode_number': 1,
  226. 'description': 'md5:9c5a3172ae763278f9303922f0ea5b17',
  227. },
  228. 'skip': 'expired',
  229. }, {
  230. 'url': 'https://abema.tv/video/episode/87-877_s1282_p31047',
  231. 'info_dict': {
  232. 'id': 'E8tvAnMJ7a9a5d',
  233. 'title': '第5話『光射す』',
  234. 'description': 'md5:56d4fc1b4f7769ded5f923c55bb4695d',
  235. 'thumbnail': r're:https://hayabusa\.io/.+',
  236. 'series': '相棒',
  237. 'episode': '第5話『光射す』',
  238. },
  239. 'skip': 'expired',
  240. }, {
  241. 'url': 'https://abema.tv/now-on-air/abema-anime',
  242. 'info_dict': {
  243. 'id': 'abema-anime',
  244. # this varies
  245. # 'title': '女子高生の無駄づかい 全話一挙【無料ビデオ72時間】',
  246. 'description': 'md5:55f2e61f46a17e9230802d7bcc913d5f',
  247. 'is_live': True,
  248. },
  249. 'skip': 'Not supported until yt-dlp implements native live downloader OR AbemaTV can start a local HTTP server',
  250. }]
  251. _TIMETABLE = None
  252. def _real_extract(self, url):
  253. # starting download using infojson from this extractor is undefined behavior,
  254. # and never be fixed in the future; you must trigger downloads by directly specifying URL.
  255. # (unless there's a way to hook before downloading by extractor)
  256. video_id, video_type = self._match_valid_url(url).group('id', 'type')
  257. headers = {
  258. 'Authorization': 'Bearer ' + self._get_device_token(),
  259. }
  260. video_type = video_type.split('/')[-1]
  261. webpage = self._download_webpage(url, video_id)
  262. canonical_url = self._search_regex(
  263. r'<link\s+rel="canonical"\s*href="(.+?)"', webpage, 'canonical URL',
  264. default=url)
  265. info = self._search_json_ld(webpage, video_id, default={})
  266. title = self._search_regex(
  267. r'<span\s*class=".+?EpisodeTitleBlock__title">(.+?)</span>', webpage, 'title', default=None)
  268. if not title:
  269. jsonld = None
  270. for jld in re.finditer(
  271. r'(?is)<span\s*class="com-m-Thumbnail__image">(?:</span>)?<script[^>]+type=(["\']?)application/ld\+json\1[^>]*>(?P<json_ld>.+?)</script>',
  272. webpage):
  273. jsonld = self._parse_json(jld.group('json_ld'), video_id, fatal=False)
  274. if jsonld:
  275. break
  276. if jsonld:
  277. title = jsonld.get('caption')
  278. if not title and video_type == 'now-on-air':
  279. if not self._TIMETABLE:
  280. # cache the timetable because it goes to 5MiB in size (!!)
  281. self._TIMETABLE = self._download_json(
  282. 'https://api.abema.io/v1/timetable/dataSet?debug=false', video_id,
  283. headers=headers)
  284. now = time_seconds(hours=9)
  285. for slot in self._TIMETABLE.get('slots', []):
  286. if slot.get('channelId') != video_id:
  287. continue
  288. if slot['startAt'] <= now and now < slot['endAt']:
  289. title = slot['title']
  290. break
  291. # read breadcrumb on top of page
  292. breadcrumb = self._extract_breadcrumb_list(webpage, video_id)
  293. if breadcrumb:
  294. # breadcrumb list translates to: (e.g. 1st test for this IE)
  295. # Home > Anime (genre) > Isekai Shokudo 2 (series name) > Episode 1 "Cheese cakes" "Morning again" (episode title)
  296. # hence this works
  297. info['series'] = breadcrumb[-2]
  298. info['episode'] = breadcrumb[-1]
  299. if not title:
  300. title = info['episode']
  301. description = self._html_search_regex(
  302. (r'<p\s+class="com-video-EpisodeDetailsBlock__content"><span\s+class=".+?">(.+?)</span></p><div',
  303. r'<span\s+class=".+?SlotSummary.+?">(.+?)</span></div><div'),
  304. webpage, 'description', default=None, group=1)
  305. if not description:
  306. og_desc = self._html_search_meta(
  307. ('description', 'og:description', 'twitter:description'), webpage)
  308. if og_desc:
  309. description = re.sub(r'''(?sx)
  310. ^(.+?)(?:
  311. アニメの動画を無料で見るならABEMA!| # anime
  312. 等、.+ # applies for most of categories
  313. )?
  314. ''', r'\1', og_desc)
  315. # canonical URL may contain season and episode number
  316. mobj = re.search(r's(\d+)_p(\d+)$', canonical_url)
  317. if mobj:
  318. seri = int_or_none(mobj.group(1), default=float('inf'))
  319. epis = int_or_none(mobj.group(2), default=float('inf'))
  320. info['season_number'] = seri if seri < 100 else None
  321. # some anime like Detective Conan (though not available in AbemaTV)
  322. # has more than 1000 episodes (1026 as of 2021/11/15)
  323. info['episode_number'] = epis if epis < 2000 else None
  324. is_live, m3u8_url = False, None
  325. if video_type == 'now-on-air':
  326. is_live = True
  327. channel_url = 'https://api.abema.io/v1/channels'
  328. if video_id == 'news-global':
  329. channel_url = update_url_query(channel_url, {'division': '1'})
  330. onair_channels = self._download_json(channel_url, video_id)
  331. for ch in onair_channels['channels']:
  332. if video_id == ch['id']:
  333. m3u8_url = ch['playback']['hls']
  334. break
  335. else:
  336. raise ExtractorError(f'Cannot find on-air {video_id} channel.', expected=True)
  337. elif video_type == 'episode':
  338. api_response = self._download_json(
  339. f'https://api.abema.io/v1/video/programs/{video_id}', video_id,
  340. note='Checking playability',
  341. headers=headers)
  342. ondemand_types = traverse_obj(api_response, ('terms', ..., 'onDemandType'))
  343. if 3 not in ondemand_types:
  344. # cannot acquire decryption key for these streams
  345. self.report_warning('This is a premium-only stream')
  346. info.update(traverse_obj(api_response, {
  347. 'series': ('series', 'title'),
  348. 'season': ('season', 'name'),
  349. 'season_number': ('season', 'sequence'),
  350. 'episode_number': ('episode', 'number'),
  351. }))
  352. if not title:
  353. title = traverse_obj(api_response, ('episode', 'title'))
  354. if not description:
  355. description = traverse_obj(api_response, ('episode', 'content'))
  356. m3u8_url = f'https://vod-abematv.akamaized.net/program/{video_id}/playlist.m3u8'
  357. elif video_type == 'slots':
  358. api_response = self._download_json(
  359. f'https://api.abema.io/v1/media/slots/{video_id}', video_id,
  360. note='Checking playability',
  361. headers=headers)
  362. if not traverse_obj(api_response, ('slot', 'flags', 'timeshiftFree'), default=False):
  363. self.report_warning('This is a premium-only stream')
  364. m3u8_url = f'https://vod-abematv.akamaized.net/slot/{video_id}/playlist.m3u8'
  365. else:
  366. raise ExtractorError('Unreachable')
  367. if is_live:
  368. self.report_warning("This is a livestream; yt-dlp doesn't support downloading natively, but FFmpeg cannot handle m3u8 manifests from AbemaTV")
  369. self.report_warning('Please consider using Streamlink to download these streams (https://github.com/streamlink/streamlink)')
  370. formats = self._extract_m3u8_formats(
  371. m3u8_url, video_id, ext='mp4', live=is_live)
  372. info.update({
  373. 'id': video_id,
  374. 'title': title,
  375. 'description': description,
  376. 'formats': formats,
  377. 'is_live': is_live,
  378. })
  379. return info
  380. class AbemaTVTitleIE(AbemaTVBaseIE):
  381. _VALID_URL = r'https?://abema\.tv/video/title/(?P<id>[^?/]+)'
  382. _PAGE_SIZE = 25
  383. _TESTS = [{
  384. 'url': 'https://abema.tv/video/title/90-1597',
  385. 'info_dict': {
  386. 'id': '90-1597',
  387. 'title': 'シャッフルアイランド',
  388. },
  389. 'playlist_mincount': 2,
  390. }, {
  391. 'url': 'https://abema.tv/video/title/193-132',
  392. 'info_dict': {
  393. 'id': '193-132',
  394. 'title': '真心が届く~僕とスターのオフィス・ラブ!?~',
  395. },
  396. 'playlist_mincount': 16,
  397. }, {
  398. 'url': 'https://abema.tv/video/title/25-102',
  399. 'info_dict': {
  400. 'id': '25-102',
  401. 'title': 'ソードアート・オンライン アリシゼーション',
  402. },
  403. 'playlist_mincount': 24,
  404. }]
  405. def _fetch_page(self, playlist_id, series_version, page):
  406. programs = self._call_api(
  407. f'v1/video/series/{playlist_id}/programs', playlist_id,
  408. note=f'Downloading page {page + 1}',
  409. query={
  410. 'seriesVersion': series_version,
  411. 'offset': str(page * self._PAGE_SIZE),
  412. 'order': 'seq',
  413. 'limit': str(self._PAGE_SIZE),
  414. })
  415. yield from (
  416. self.url_result(f'https://abema.tv/video/episode/{x}')
  417. for x in traverse_obj(programs, ('programs', ..., 'id')))
  418. def _entries(self, playlist_id, series_version):
  419. return OnDemandPagedList(
  420. functools.partial(self._fetch_page, playlist_id, series_version),
  421. self._PAGE_SIZE)
  422. def _real_extract(self, url):
  423. playlist_id = self._match_id(url)
  424. series_info = self._call_api(f'v1/video/series/{playlist_id}', playlist_id)
  425. return self.playlist_result(
  426. self._entries(playlist_id, series_info['version']), playlist_id=playlist_id,
  427. playlist_title=series_info.get('title'),
  428. playlist_description=series_info.get('content'))