Compare commits

...

9 Commits

Author SHA1 Message Date
Han Hyeji
62b312feda
Merge 1e4ea76441 into c5098961b0 2024-08-21 22:32:54 -04:00
dirkf
c5098961b0 [Youtube] Rework n function extraction pattern
Now also succeeds with player b12cc44b
2024-08-06 20:59:09 +01:00
dirkf
dbc08fba83 [jsinterp] Improve slice implementation for player b12cc44b
Partly taken from yt-dlp/yt-dlp#10664, thx seproDev
        Fixes #32896
2024-08-06 20:51:38 +01:00
Aiur Adept
71223bff39
[Youtube] Fix nsig extraction for player 20dfca59 (#32891)
* dirkf's patch for nsig extraction
* add generic search per  yt-dlp/yt-dlp/pull/10611 - thx bashonly

---------

Co-authored-by: dirkf <fieldhouse@gmx.net>
2024-08-01 19:18:34 +01:00
dirkf
1e4ea76441
Include NateProgramIE 2023-08-31 16:37:20 +01:00
dirkf
ba06388c8c
Update from yt-dlp extractor 2023-08-31 16:34:09 +01:00
Han Hyeji
8cf64dcbbf [nate] add new site 2021-12-07 19:09:23 +09:00
hyeeji
a0bb1d8c0d Feat:add Site 2021-12-07 13:18:15 +09:00
hyeeji
2b0623b116 Test 2021-12-07 03:11:01 +09:00
6 changed files with 283 additions and 13 deletions

View File

@ -425,6 +425,34 @@ class TestJSInterpreter(unittest.TestCase):
self._test(jsi, [''], args=['', '-'])
self._test(jsi, [], args=['', ''])
def test_slice(self):
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice()}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(5)}', [5, 6, 7, 8])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(99)}', [])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-2)}', [7, 8])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-99)}', [0, 1, 2, 3, 4, 5, 6, 7, 8])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 0)}', [])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, 0)}', [])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(0, 1)}', [0])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(3, 6)}', [3, 4, 5])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(1, -1)}', [1, 2, 3, 4, 5, 6, 7])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-1, 1)}', [])
self._test('function f(){return [0, 1, 2, 3, 4, 5, 6, 7, 8].slice(-3, -1)}', [6, 7])
self._test('function f(){return "012345678".slice()}', '012345678')
self._test('function f(){return "012345678".slice(0)}', '012345678')
self._test('function f(){return "012345678".slice(5)}', '5678')
self._test('function f(){return "012345678".slice(99)}', '')
self._test('function f(){return "012345678".slice(-2)}', '78')
self._test('function f(){return "012345678".slice(-99)}', '012345678')
self._test('function f(){return "012345678".slice(0, 0)}', '')
self._test('function f(){return "012345678".slice(1, 0)}', '')
self._test('function f(){return "012345678".slice(0, 1)}', '0')
self._test('function f(){return "012345678".slice(3, 6)}', '345')
self._test('function f(){return "012345678".slice(1, -1)}', '1234567')
self._test('function f(){return "012345678".slice(-1, 1)}', '')
self._test('function f(){return "012345678".slice(-3, -1)}', '67')
if __name__ == '__main__':
unittest.main()

View File

@ -174,6 +174,14 @@ _NSIG_TESTS = [
'https://www.youtube.com/s/player/5604538d/player_ias.vflset/en_US/base.js',
'7X-he4jjvMx7BCX', 'sViSydX8IHtdWA',
),
(
'https://www.youtube.com/s/player/20dfca59/player_ias.vflset/en_US/base.js',
'-fLCxedkAk4LUTK2', 'O8kfRq1y1eyHGw',
),
(
'https://www.youtube.com/s/player/b12cc44b/player_ias.vflset/en_US/base.js',
'keLa5R2U00sR9SQK', 'N1OGyujjEwMnLw',
),
]

View File

@ -750,6 +750,10 @@ from .nationalgeographic import (
NationalGeographicVideoIE,
NationalGeographicTVIE,
)
from .nate import (
NateIE,
NateProgramIE,
)
from .naver import NaverIE
from .nba import (
NBAWatchEmbedIE,

View File

@ -0,0 +1,194 @@
# coding: utf-8
from __future__ import unicode_literals
import itertools
from .common import InfoExtractor
from ..utils import (
ExtractorError,
int_or_none,
merge_dicts,
T,
traverse_obj,
txt_or_none,
unified_strdate,
url_or_none,
)
class NateBaseIE(InfoExtractor):
_API_BASE = 'https://tv.nate.com/api/v1/'
def _download_webpage_handle(self, url_or_request, video_id, *args, **kwargs):
fatal = kwargs.get('fatal', True)
kwargs['fatal'] = False
res = super(NateBaseIE, self)._download_webpage_handle(
url_or_request, video_id, *args, **kwargs)
if not res:
if fatal:
raise ExtractorError('Failed to download webpage')
return res
status = res[1].getcode()
if 200 <= status < 400:
new_url = res[1].geturl()
if url_or_request != new_url and '/Error.html' in new_url:
raise ExtractorError(
'Download redirected to Error.html: expired?',
expected=True)
else:
msg = 'Failed to download webpage: HTTP code %d' % status
if fatal:
raise ExtractorError(msg)
else:
self.report_warning(msg)
return res
class NateIE(NateBaseIE):
_VALID_URL = r'https?://(?:m\.)?tv\.nate\.com/clip/(?P<id>[0-9]+)'
_TESTS = [{
'url': 'https://tv.nate.com/clip/1848976',
'info_dict': {
'id': '1848976',
'ext': 'mp4',
'title': '[결승 오프닝 타이틀] 2018 LCK 서머 스플릿 결승전 kt Rolster VS Griffin',
'description': 'md5:e1b79a7dcf0d8d586443f11366f50e6f',
'thumbnail': r're:^http?://.*\.jpg$',
'upload_date': '20180908',
'age_limit': 15,
'duration': 73,
'uploader': '2018 LCK 서머 스플릿(롤챔스)',
'channel': '2018 LCK 서머 스플릿(롤챔스)',
'channel_id': '3606',
'uploader_id': '3606',
'tags': 'count:59',
},
'skip': 'Redirect to Error.html',
}, {
'url': 'https://tv.nate.com/clip/4300566',
# 'md5': '02D3CAB3907B60C58043761F8B5BF2B3',
'info_dict': {
'id': '4300566',
'ext': 'mp4',
'title': '[심쿵엔딩] 이준호x이세영, 서로를 기억하며 끌어안는 두 사람!💕, MBC 211204 방송',
'description': 'md5:edf489c54ea2682c7973154b2089aa0e',
'thumbnail': r're:^http?://.*\.jpg$',
'upload_date': '20211204',
'age_limit': 15,
'duration': 201,
'uploader': '옷소매 붉은 끝동',
'channel': '옷소매 붉은 끝동',
'channel_id': '27987',
'uploader_id': '27987',
'tags': 'count:20',
},
'params': {'skip_download': True},
}, {
'url': 'https://tv.nate.com/clip/4764792',
'info_dict': {
'id': '4764792',
'ext': 'mp4',
'title': '흥을 돋우는 가야금 연주와 트롯의 만남⬈ ‘열두줄’♪ TV CHOSUN 230625 방송',
'description': 'md5:85734d3f9daebe4aa4f20cc73bdcc90c',
'upload_date': '20230625',
'uploader_id': '29116',
'uploader': '쇼퀸',
'age_limit': 15,
'thumbnail': r're:^http?://.*\.jpg$',
'duration': 182,
'channel': '쇼퀸',
'channel_id': '29116',
'tags': 'count:25',
},
'params': {'skip_download': True},
}]
_QUALITY = {
'36': 2160,
'35': 1080,
'34': 720,
'33': 480,
'32': 360,
'31': 270,
}
def _real_extract(self, url):
video_id = self._match_id(url)
video_data, urlh = self._download_json_handle(
'{0}clip/{1}'.format(self._API_BASE, video_id), video_id,
fatal=False)
if not video_data:
raise ExtractorError('Empty programme JSON')
title = video_data['clipTitle']
formats = []
for f_url in traverse_obj(video_data, ('smcUriList', Ellipsis, T(url_or_none))):
fmt_id = f_url[-2:]
formats.append({
'format_id': fmt_id,
'url': f_url,
'height': self._QUALITY.get(fmt_id),
'quality': int_or_none(fmt_id),
})
self._sort_formats(formats)
info = traverse_obj(video_data, {
'uploader': ('programTitle', T(txt_or_none)),
'uploader_id': ('programSeq', T(txt_or_none)),
})
for up, ch in (('uploader', 'channel'), ('uploader_id', 'channel_id')):
info[ch] = info.get(up)
return merge_dicts({
'id': video_id,
'title': title,
'formats': formats,
}, info, traverse_obj(video_data, {
'description': ('synopsis', T(txt_or_none)),
'thumbnail': ('contentImg', T(url_or_none)),
'upload_date': (('broadDate', 'regDate'), T(unified_strdate)),
'age_limit': ('targetAge', T(int_or_none)),
'duration': ('playTime', T(int_or_none)),
'tags': ('hashTag', T(lambda s: s.split(',') or None)),
}, get_all=False))
class NateProgramIE(NateBaseIE):
_VALID_URL = r'https?://tv\.nate\.com/program/clips/(?P<id>[0-9]+)'
_TESTS = [{
'url': 'https://tv.nate.com/program/clips/27987',
'playlist_mincount': 191,
'info_dict': {
'id': '27987',
},
}, {
'url': 'https://tv.nate.com/program/clips/3606',
'playlist_mincount': 15,
'info_dict': {
'id': '3606',
},
'skip': 'Redirect to Error.html',
}]
def _entries(self, pl_id):
for page_num in itertools.count(1):
program_data, urlh = self._download_json_handle(
'{0}program/{1}/clip/ranking'.format(self._API_BASE, pl_id),
pl_id, query={'size': 20, 'page': page_num},
note='Downloading page {0}'.format(page_num), fatal=False)
empty = True
for clip_id in traverse_obj(program_data, ('content', Ellipsis, 'clipSeq', T(txt_or_none))):
yield self.url_result(
'https://tv.nate.com/clip/%s' % clip_id,
ie=NateIE.ie_key(), video_id=clip_id)
empty = False
if traverse_obj(program_data, 'last') or (program_data and empty):
break
def _real_extract(self, url):
pl_id = self._match_id(url)
return self.playlist_result(self._entries(pl_id), playlist_id=pl_id)

View File

@ -1659,17 +1659,46 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
def _extract_n_function_name(self, jscode):
func_name, idx = self._search_regex(
# new: (b=String.fromCharCode(110),c=a.get(b))&&c=nfunc[idx](c)
# or: (b="nn"[+a.D],c=a.get(b))&&(c=nfunc[idx](c)s
# old: .get("n"))&&(b=nfunc[idx](b)
# older: .get("n"))&&(b=nfunc(b)
# or: (b="nn"[+a.D],c=a.get(b))&&(c=nfunc[idx](c)
# or: (PL(a),b=a.j.n||null)&&(b=nfunc[idx](b)
# or: (b="nn"[+a.D],vL(a),c=a.j[b]||null)&&(c=narray[idx](c),a.set(b,c),narray.length||nfunc("")
# old: (b=a.get("n"))&&(b=nfunc[idx](b)(?P<c>[a-z])\s*=\s*[a-z]\s*
# older: (b=a.get("n"))&&(b=nfunc(b)
r'''(?x)
(?:\(\s*(?P<b>[a-z])\s*=\s*(?:
String\s*\.\s*fromCharCode\s*\(\s*110\s*\)|
"n+"\[\s*\+?s*[\w$.]+\s*]
)\s*,(?P<c>[a-z])\s*=\s*[a-z]\s*)?
\.\s*get\s*\(\s*(?(b)(?P=b)|"n{1,2}")(?:\s*\)){2}\s*&&\s*\(\s*(?(c)(?P=c)|b)\s*=\s*
(?P<nfunc>[a-zA-Z_$][\w$]*)(?:\s*\[(?P<idx>\d+)\])?\s*\(\s*[\w$]+\s*\)
''', jscode, 'Initial JS player n function name', group=('nfunc', 'idx'))
\((?:[\w$()\s]+,)*?\s* # (
(?P<b>[a-z])\s*=\s* # b=
(?:
(?: # expect ,c=a.get(b) (etc)
String\s*\.\s*fromCharCode\s*\(\s*110\s*\)|
"n+"\[\s*\+?s*[\w$.]+\s*]
)\s*(?:,[\w$()\s]+(?=,))*|
(?P<old>[\w$]+) # a (old[er])
)\s*
(?(old)
# b.get("n")
(?:\.\s*[\w$]+\s*|\[\s*[\w$]+\s*]\s*)*?
(?:\.\s*n|\[\s*"n"\s*]|\.\s*get\s*\(\s*"n"\s*\))
| # ,c=a.get(b)
,\s*(?P<c>[a-z])\s*=\s*[a-z]\s*
(?:\.\s*[\w$]+\s*|\[\s*[\w$]+\s*]\s*)*?
(?:\[\s*(?P=b)\s*]|\.\s*get\s*\(\s*(?P=b)\s*\))
)
# interstitial junk
\s*(?:\|\|\s*null\s*)?(?:\)\s*)?&&\s*(?:\(\s*)?
(?(c)(?P=c)|(?P=b))\s*=\s* # [c|b]=
# nfunc|nfunc[idx]
(?P<nfunc>[a-zA-Z_$][\w$]*)(?:\s*\[(?P<idx>\d+)\])?\s*\(\s*[\w$]+\s*\)
''', jscode, 'Initial JS player n function name', group=('nfunc', 'idx'),
default=(None, None))
# thx bashonly: yt-dlp/yt-dlp/pull/10611
if not func_name:
self.report_warning('Falling back to generic n function search')
return self._search_regex(
r'''(?xs)
(?:(?<=[^\w$])|^) # instead of \b, which ignores $
(?P<name>(?!\d)[a-zA-Z\d_$]+)\s*=\s*function\((?!\d)[a-zA-Z\d_$]+\)
\s*\{(?:(?!};).)+?["']enhanced_except_
''', jscode, 'Initial JS player n function name', group='name')
if not idx:
return func_name

View File

@ -925,9 +925,16 @@ class JSInterpreter(object):
obj.reverse()
return obj
elif member == 'slice':
assertion(isinstance(obj, list), 'must be applied on a list')
assertion(len(argvals) == 1, 'takes exactly one argument')
return obj[argvals[0]:]
assertion(isinstance(obj, (list, compat_str)), 'must be applied on a list or string')
# From [1]:
# .slice() - like [:]
# .slice(n) - like [n:] (not [slice(n)]
# .slice(m, n) - like [m:n] or [slice(m, n)]
# [1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
assertion(len(argvals) <= 2, 'takes between 0 and 2 arguments')
if len(argvals) < 2:
argvals += (None,)
return obj[slice(*argvals)]
elif member == 'splice':
assertion(isinstance(obj, list), 'must be applied on a list')
assertion(argvals, 'takes one or more arguments')