pytube: [BUG] get_throttling_function_name could not find match for multiple

Describe the bug Having issues resolving video URLs for a give youtube video. I get the following error pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

To Reproduce Please provide the following information:

  • The video or playlist url that is causing the error. https://www.youtube.com/watch?v=Y4-GSFKZmEg

The code where the problem is occurring.

from pytube import YouTube

url = "https://www.youtube.com/watch?v=Y4-GSFKZmEg"
yt = YouTube(url)
thevid = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
vid = thevid
print("VID", vid)

Expected behavior the output should print the video asset URL where the video can be downloaded from

Output

Traceback (most recent call last):
  File "/Users/par/Dev/mememaker-web/mememaker/scripts/pytube_test.py", line 5, in <module>
    thevid = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/__main__.py", line 296, in streams
    return StreamQuery(self.fmt_streams)
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/__main__.py", line 188, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/cipher.py", line 43, in __init__
    self.throttling_plan = get_throttling_plan(js)
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/cipher.py", line 405, in get_throttling_plan
    raw_code = get_throttling_function_code(js)
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code
    name = re.escape(get_throttling_function_name(js))
  File "/Users/par/.virtualenvs/mememaker/lib/python3.9/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name
    raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple

System information Please provide the following information:

  • Python 3.9.9
  • print(pytube.version) 12.0.0
  • python -m pip install git+https://github.com/pytube/pytube

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 26
  • Comments: 44 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Apparently the JavaScript code containing details of the throttling function name is now of the form:

a.D&&(b=a.get("n"))&&(b=$x[0](b)

And the regex in cipher.py cannot match the same as it searches for variable names of a fixed length (3 in this case).

Changing the regex in function_patterns in cipher.py to:

r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*'
r'\([a-z]\s*=\s*([a-zA-Z0-9$]{2,3})(\[\d+\])?\([a-z]\)'

and changing line 288 of cipher.py to:

nfunc=re.escape(function_match.group(1))),

to escape the ‘$’ symbol now present in the ‘nfunc’ value seems to resolve the errors.

@manish-kumar-iisc Neither of the PRs containing the fixes for this problem have been merged as of yet, so you’ll have to either make the changes manually or install one of the forks containing the fix:

python -m pip install git+https://github.com/kinshuk-h/pytube

you guys on github are the best! saving us, lazy devs, so much work haha

@mpena22

Line 288 was changed from

nfunc=function_match.group(1)),

to

nfunc=re.escape(function_match.group(1))),

Why it’s taking so much time to fix this critical bug?

I just started experiencing the same issue, while it was working fine up until a few hours ago. I confirmed the issue by making a basic script on a completely different system and attempting to download a video that was previously successful. It appears this is something that has happened from time to time when something changes on the YouTube side and the regex has to be updated in cipher.py for the get_throttling_function_name.

https://github.com/pytube/pytube/issues/1107

when is pytube gonna release this patch?

getting the same issue using v12.0.0

@kinshuk-h Thank you! Changing these two pieces of code saved my evening.

Снимок экрана 2022-04-15 в 19 26 12 Снимок экрана 2022-04-15 в 19 26 57

Thank you @kinshuk-h 😃