Skip to content

Commit

Permalink
[jsinterp] Support if statements
Browse files Browse the repository at this point in the history
Closes #6131
  • Loading branch information
pukkandan committed Feb 1, 2023
1 parent 83c4970 commit 8b008d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/test_jsinterp.py
Expand Up @@ -155,6 +155,38 @@ def test_call(self):
self.assertEqual(jsi.call_function('z'), 5)
self.assertEqual(jsi.call_function('y'), 2)

def test_if(self):
jsi = JSInterpreter('''
function x() {
let a = 9;
if (0==0) {a++}
return a
}''')
self.assertEqual(jsi.call_function('x'), 10)

jsi = JSInterpreter('''
function x() {
if (0==0) {return 10}
}''')
self.assertEqual(jsi.call_function('x'), 10)

jsi = JSInterpreter('''
function x() {
if (0!=0) {return 1}
else {return 10}
}''')
self.assertEqual(jsi.call_function('x'), 10)

""" # Unsupported
jsi = JSInterpreter('''
function x() {
if (0!=0) {return 1}
else if (1==0) {return 2}
else {return 10}
}''')
self.assertEqual(jsi.call_function('x'), 10)
"""

def test_for_loop(self):
jsi = JSInterpreter('''
function x() { a=0; for (i=0; i-10; i++) {a++} return a }
Expand Down
4 changes: 4 additions & 0 deletions test/test_youtube_signature.py
Expand Up @@ -134,6 +134,10 @@
'https://www.youtube.com/s/player/7a062b77/player_ias.vflset/en_US/base.js',
'NRcE3y3mVtm_cV-W', 'VbsCYUATvqlt5w',
),
(
'https://www.youtube.com/s/player/dac945fd/player_ias.vflset/en_US/base.js',
'o8BkRxXhuYsBCWi6RplPdP', '3Lx32v_hmzTm6A',
),
]


Expand Down
15 changes: 15 additions & 0 deletions yt_dlp/jsinterp.py
Expand Up @@ -403,10 +403,25 @@ def dict_item(key, val):

m = re.match(r'''(?x)
(?P<try>try)\s*\{|
(?P<if>if)\s*\(|
(?P<switch>switch)\s*\(|
(?P<for>for)\s*\(
''', expr)
md = m.groupdict() if m else {}
if md.get('if'):
cndn, expr = self._separate_at_paren(expr[m.end() - 1:])
if_expr, expr = self._separate_at_paren(expr.lstrip())
# TODO: "else if" is not handled
else_expr = None
m = re.match(r'else\s*{', expr)
if m:
else_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
cndn = _js_ternary(self.interpret_expression(cndn, local_vars, allow_recursion))
ret, should_abort = self.interpret_statement(
if_expr if cndn else else_expr, local_vars, allow_recursion)
if should_abort:
return ret, True

if md.get('try'):
try_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
err = None
Expand Down

6 comments on commit 8b008d6

@anunasica
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here https://www.youtube.com/watch?v=J4X-nk-uZ2w
a video that requires "phantomjs" to avoid the error message.

@gamer191
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anunasica can you please copy-paste yt-dlp’s output, after attempting the download with —verbose

@anunasica
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C:\Users\ >y -v https://www.youtube.com/watch?v=J4X-nk-uZ2w
[debug] Command-line config: ['-v', 'https://www.youtube.com/watch?v=J4X-nk
-uZ2w']
[debug] Encodings: locale cp1252, fs utf-8, pref cp1252, out utf-8 (No VT),
error utf-8 (No VT), screen utf-8 (No VT)
[debug] yt-dlp version 2023.01.06 [6becd25] (win_x86_exe)
[debug] Python 3.7.9 (CPython x86 32bit) - Windows-Vista-6.0.6002-SP2 (Open
SSL 1.1.1g 21 Apr 2020)
[debug] exe versions: ffmpeg N-94698-g83e0b71-Reino (fdk), ffprobe N-94698-
g83e0b71-Reino, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.16.0, brotli-1.0.9, certifi-2022.1
2.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-10.4
[debug] Proxy map: {}
[debug] Loaded 1760 extractors
[youtube] Extracting URL: https://www.youtube.com/watch?v=J4X-nk-uZ2w
[youtube] J4X-nk-uZ2w: Downloading webpage
[youtube] J4X-nk-uZ2w: Downloading android player API JSON
[debug] Loading youtube-nsig.97ea7458 from cache
WARNING: [youtube] J4X-nk-uZ2w: nsig extraction failed: You may experience
throttling for some formats
Install PhantomJS to workaround the issue. Please download it from
https://phantomjs.org/download.html
n = Pni9Xua2jW5PP6 ; player = https://www.youtube.com/s/player/97e
a7458/player_ias.vflset/en_US/base.js
[debug] [youtube] Signature function returned an exception; please report t
his issue on https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the
appropriate issue template. Confirm you are on the latest version using yt
-dlp -U
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, v
codec:vp9.2, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), s
ource, vcodec:vp9.2(10), channels, acodec, lang, proto, filesize, fs_approx
, tbr, vbr, abr, asr, vext, aext, hasaud, id
[debug] Default format spec: bestvideo*+bestaudio/best
[info] J4X-nk-uZ2w: Downloading 1 format(s): 137+251
[debug] Invoking http downloader on "https://rr2---sn-8vq54voxpu-8jxe.googl
evideo.com/videoplayback?expire=1675756797&ei=nbDhY_noGISSgQfgiZbgAg&ip=93.
65.106.14&id=o-AIbB2NJ01cIvRKCcZhC-z5Io2gHw-nVAy3hlh--tWuCR&itag=137&source
=youtube&requiressl=yes&mh=1F&mm=31%2C29&mn=sn-8vq54voxpu-8jxe%2Csn-8vq54vo
xpu-hm2s&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=24&initcwndbps=1676250&spc=H
3gIhlRsCTzHve6swqQ7uJITm1mJdK0&vprv=1&svpuc=1&mime=video%2Fmp4&gir=yes&clen
=4278526&dur=56.374&lmt=1670772397693089&mt=1675735039&fvip=6&keepalive=yes
&fexp=24007246&c=ANDROID&txp=2216224&sparams=expire%2Cei%2Cip%2Cid%2Citag%2
Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&s
ig=AOq0QJ8wRgIhAOWKX9UEiCKwsTxt9-r6tGTOiD6ioCxf5AeAoyjS8KJNAiEA7sDP3Vgh35kK
FHvL-zio9ndJst11MKxbeTACz1JwUgk%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2C
pcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAJMQ_lLx4knSWjwRF4ERV2rc_BVPehM
SaAeRC1ME5YtsAiBOQJ2VhRFgRnPPidkzFR0bIjX4r9xbrLdJ8KXktLw5CQ%3D%3D"
[debug] File locking is not supported. Proceeding without locking
[download] Destination: How to Search in Scanned PDF Documents (2019) [J4X-
nk-uZ2w].f137.mp4
[download] 100% of 4.08MiB in 00:00:05 at 796.46KiB/s
[debug] Invoking http downloader on "https://rr2---sn-8vq54voxpu-8jxe.googl
evideo.com/videoplayback?expire=1675756797&ei=nbDhY_noGISSgQfgiZbgAg&ip=93.
65.106.14&id=o-AIbB2NJ01cIvRKCcZhC-z5Io2gHw-nVAy3hlh--tWuCR&itag=251&source
=youtube&requiressl=yes&mh=1F&mm=31%2C29&mn=sn-8vq54voxpu-8jxe%2Csn-8vq54vo
xpu-hm2s&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=24&initcwndbps=1676250&spc=H
3gIhlRsCTzHve6swqQ7uJITm1mJdK0&vprv=1&svpuc=1&mime=audio%2Fwebm&gir=yes&cle
n=1145795&dur=56.401&lmt=1570116560351961&mt=1675735039&fvip=6&keepalive=ye
s&fexp=24007246&c=ANDROID&txp=2211222&sparams=expire%2Cei%2Cip%2Cid%2Citag%
2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&
sig=AOq0QJ8wRQIgJaJeESTbXiHD6BQxKWQps_t1mDj3wcykGL-JSM4jUsICIQC4h0JqlmSwx-F
RNWNTpTA0MUMp7GFCMpFT2id0R2cacw%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi
%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRQIhAJMQ_lLx4knSWjwRF4ERV2rc_BVP
ehMSaAeRC1ME5YtsAiBOQJ2VhRFgRnPPidkzFR0bIjX4r9xbrLdJ8KXktLw5CQ%3D%3D"
[download] Destination: How to Search in Scanned PDF Documents (2019) [J4X-
nk-uZ2w].f251.webm
[download] 100% of 1.09MiB in 00:00:01 at 771.15KiB/s
[Merger] Merging formats into "How to Search in Scanned PDF Documents (2019
) [J4X-nk-uZ2w].mkv"
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:How
to Search in Scanned PDF Documents (2019) [J4X-nk-uZ2w].f137.mp4" -i "file
:How to Search in Scanned PDF Documents (2019) [J4X-nk-uZ2w].f251.webm" -c
copy -map "0:v:0" -map "1:a:0" -movflags "+faststart" "file:How to Search i
n Scanned PDF Documents (2019) [J4X-nk-uZ2w].temp.mkv"
Deleting original file How to Search in Scanned PDF Documents (2019) [J4X-n
k-uZ2w].f137.mp4 (pass -k to keep)
Deleting original file How to Search in Scanned PDF Documents (2019) [J4X-n
k-uZ2w].f251.webm (pass -k to keep)

C:\Users\ >

@anunasica
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with phantom:

C:\Users\ >y -v https://www.youtube.com/watch?v=J4X-nk-uZ2w
[debug] Command-line config: ['-v', 'https://www.youtube.com/watch?v=J4X-nk
-uZ2w']
[debug] Encodings: locale cp1252, fs utf-8, pref cp1252, out utf-8 (No VT),
error utf-8 (No VT), screen utf-8 (No VT)
[debug] yt-dlp version 2023.01.06 [6becd25] (win_x86_exe)
[debug] Python 3.7.9 (CPython x86 32bit) - Windows-Vista-6.0.6002-SP2 (Open
SSL 1.1.1g 21 Apr 2020)
[debug] exe versions: ffmpeg N-94698-g83e0b71-Reino (fdk), ffprobe N-94698-
g83e0b71-Reino, phantomjs 2.1.1, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.16.0, brotli-1.0.9, certifi-2022.1
2.07, mutagen-1.46.0, sqlite3-2.6.0, websockets-10.4
[debug] Proxy map: {}
[debug] Loaded 1760 extractors
[youtube] Extracting URL: https://www.youtube.com/watch?v=J4X-nk-uZ2w
[youtube] J4X-nk-uZ2w: Downloading webpage
[youtube] J4X-nk-uZ2w: Downloading android player API JSON
[youtube] J4X-nk-uZ2w: Downloading player f565d246
[debug] Saving youtube-nsig.f565d246 to cache
WARNING: [youtube] J4X-nk-uZ2w: Native nsig extraction failed: Trying with
PhantomJS
n = DC2__Pq2la0W4T5-4je ; player = https://www.youtube.com/s/playe
r/f565d246/player_ias.vflset/en_US/base.js
[debug] [youtube] Signature function returned an exception; please report t
his issue on https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the
appropriate issue template. Confirm you are on the latest version using yt
-dlp -U
[youtube] J4X-nk-uZ2w: Executing signature code
[debug] [youtube] PhantomJS command line: phantomjs "--ssl-protocol=any" "C
:\Users\AppData\Local\Temp\tmpjc8oqb71"
[debug] [youtube] Decrypted nsig DC2__Pq2la0W4T5-4je => 6m7fhJQ11aJtEQ
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, v
codec:vp9.2, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), s
ource, vcodec:vp9.2(10), channels, acodec, lang, proto, filesize, fs_approx
, tbr, vbr, abr, asr, vext, aext, hasaud, id
[debug] Default format spec: bestvideo*+bestaudio/best
[info] J4X-nk-uZ2w: Downloading 1 format(s): 137+251
[debug] Invoking http downloader on "https://rr2---sn-8vq54voxpu-8jxe.googl
evideo.com/videoplayback?expire=1675757017&ei=ebHhY-ugJpby-gbE2L6gCQ&ip=93.
65.106.14&id=o-ACbmJVLKEDB0kxrrtxj7WfMQOpAHN6Ysk5KmPJbzqN5i&itag=137&source
=youtube&requiressl=yes&mh=1F&mm=31%2C29&mn=sn-8vq54voxpu-8jxe%2Csn-8vq54vo
xpu-hm2s&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=24&pcm2=yes&initcwndbps=1636
250&spc=H3gIhuhg-0u6o2hESP_NmBmDdQU4JMc&vprv=1&svpuc=1&mime=video%2Fmp4&gir
=yes&clen=4278526&dur=56.374&lmt=1670772397693089&mt=1675735280&fvip=6&keep
alive=yes&fexp=24007246&c=ANDROID&txp=2216224&sparams=expire%2Cei%2Cip%2Cid
%2Citag%2Csource%2Crequiressl%2Cpcm2%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Ccl
en%2Cdur%2Clmt&sig=AOq0QJ8wRAIgNdj2xQbs0GCNygxbsHj_S1ZOwvhBOzD5C2ZfE03dqIYC
IBPbs6Hkg8WiE8QvFVhKU94BVPGEdxTIa7Pq_WFvxBDV&lsparams=mh%2Cmm%2Cmn%2Cms%2Cm
v%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAJVI7Dnxf5fSuFZznkyHT
-WpPS9t82x9UBFAVI1HuEynAiEAnKIBPQNdVc8qcx38RmdgpZz4N_FXyvRmHI9vzExQUZU%3D"
[debug] File locking is not supported. Proceeding without locking
[download] Destination: How to Search in Scanned PDF Documents (2019) [J4X-
nk-uZ2w].f137.mp4
[download] 100% of 4.08MiB in 00:00:05 at 788.94KiB/s
[debug] Invoking http downloader on "https://rr2---sn-8vq54voxpu-8jxe.googl
evideo.com/videoplayback?expire=1675757017&ei=ebHhY-ugJpby-gbE2L6gCQ&ip=93.
65.106.14&id=o-ACbmJVLKEDB0kxrrtxj7WfMQOpAHN6Ysk5KmPJbzqN5i&itag=251&source
=youtube&requiressl=yes&mh=1F&mm=31%2C29&mn=sn-8vq54voxpu-8jxe%2Csn-8vq54vo
xpu-hm2s&ms=au%2Crdu&mv=m&mvi=2&pcm2cms=yes&pl=24&pcm2=yes&initcwndbps=1636
250&spc=H3gIhuhg-0u6o2hESP_NmBmDdQU4JMc&vprv=1&svpuc=1&mime=audio%2Fwebm&gi
r=yes&clen=1145795&dur=56.401&lmt=1570116560351961&mt=1675735280&fvip=6&kee
palive=yes&fexp=24007246&c=ANDROID&txp=2211222&sparams=expire%2Cei%2Cip%2Ci
d%2Citag%2Csource%2Crequiressl%2Cpcm2%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cc
len%2Cdur%2Clmt&sig=AOq0QJ8wRAIgZupTygzGrPXR6ywrU2NJCiCAe7KtrsWwF68H1SK7XOw
CIDwP32KsosG6EV-Ml-fS5eQFbx7rsJjOZvKmGkhlJjrH&lsparams=mh%2Cmm%2Cmn%2Cms%2C
mv%2Cmvi%2Cpcm2cms%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAJVI7Dnxf5fSuFZznkyH
T-WpPS9t82x9UBFAVI1HuEynAiEAnKIBPQNdVc8qcx38RmdgpZz4N_FXyvRmHI9vzExQUZU%3D"

[download] Destination: How to Search in Scanned PDF Documents (2019) [J4X-
nk-uZ2w].f251.webm
[download] 100% of 1.09MiB in 00:00:01 at 774.89KiB/s
[Merger] Merging formats into "How to Search in Scanned PDF Documents (2019
) [J4X-nk-uZ2w].mkv"
[debug] ffmpeg command line: ffmpeg -y -loglevel "repeat+info" -i "file:How
to Search in Scanned PDF Documents (2019) [J4X-nk-uZ2w].f137.mp4" -i "file
:How to Search in Scanned PDF Documents (2019) [J4X-nk-uZ2w].f251.webm" -c
copy -map "0:v:0" -map "1:a:0" -movflags "+faststart" "file:How to Search i
n Scanned PDF Documents (2019) [J4X-nk-uZ2w].temp.mkv"
Deleting original file How to Search in Scanned PDF Documents (2019) [J4X-n
k-uZ2w].f251.webm (pass -k to keep)
Deleting original file How to Search in Scanned PDF Documents (2019) [J4X-n
k-uZ2w].f137.mp4 (pass -k to keep)

C:\Users\ >

@anunasica
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Playback is ok with both downloads.

@gamer191
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anunasica thanks, I've added a comment about this to #6131

Please sign in to comment.