Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 'NoneType' object has no attribute 'span' #1218

Closed
hunterhawley opened this issue Feb 1, 2022 · 91 comments · Fixed by #1222
Closed

[BUG] 'NoneType' object has no attribute 'span' #1218

hunterhawley opened this issue Feb 1, 2022 · 91 comments · Fixed by #1222
Labels

Comments

@hunterhawley
Copy link

Before creating an issue

Please confirm that you are on the latest version of pytube by installing from the source.
You can do this by running python -m pip install git+https://github.com/pytube/pytube.
Sometimes, the pypi library repository is not up to date, and your issue may have been fixed already!

Describe the bug
I have been running PyTube at different points today with success and fast downloads, but as of about 30 minutes ago I started to get this when I would run it:

'NoneType' object has no attribute 'span'

To Reproduce
I have tried on these two links, both of which were working earlier today:
-https://www.youtube.com/watch?v=twtGL8WbllM&ab_channel=HarritonRams
-https://www.youtube.com/watch?v=mA_3LkF_Cdo&ab_channel=IndianaSRN

Here is the code sample where it is being used:

def download_youtube_video(row, game_id): filename = game_id + ".mp4" try: YouTube(row[0]).streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename=filename) upload_successful = True except Exception as e: print(e) print("HEY, LOOK HERE") print("I'VE GOT SOME BAD NEWS, SADLY") print("YOU'RE GOING TO HAVE TO DOWNLOAD AND UPLOAD THIS ONE MANUALLY") print(row[0]) print(game_id) print("I TRIED TWO DIFFERENT YOUTUBE DOWNLOADER PACKAGES, BLAME THOSE F**** OVER AT GOOGLE FOR THIS")

Expected behavior
I expected the video to be downloaded in the same directory as my python script as it was earlier today.

Output
Traceback (most recent call last): File "test.py", line 3, in <module> YouTube("https://www.youtube.com/watch?v=mA_3LkF_Cdo&ab_channel=IndianaSRN").streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename="hello.mp4") File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/__main__.py", line 292, in streams return StreamQuery(self.fmt_streams) File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/__main__.py", line 177, in fmt_streams extract.apply_signature(stream_manifest, self.vid_info, self.js) File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/extract.py", line 409, in apply_signature cipher = Cipher(js=js) File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/cipher.py", line 43, in __init__ self.throttling_plan = get_throttling_plan(js) File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/cipher.py", line 387, in get_throttling_plan raw_code = get_throttling_function_code(js) File "/Users/hunterhawley/Library/Python/3.7/lib/python/site-packages/pytube/cipher.py", line 301, in get_throttling_function_code code_lines_list = find_object_from_startpoint(js, match.span()[1]).split('\n') AttributeError: 'NoneType' object has no attribute 'span'

System information
Please provide the following information:

  • Python version: Python 3.7.1rc1
  • Pytube version: Unsure as the provided way to find this didn't work
  • Command used to install pytube: First was python -m pip install pytube but before posting it was python -m pip install git+https://github.com/pytube/pytube
@hunterhawley
Copy link
Author

hunterhawley commented Feb 1, 2022

I'm not sure why all of my code is formatted so badly but happy to make edits if I can figure out how to fix it

@AndyPythonCode
Copy link

Same error

AttributeError: 'NoneType' object has no attribute 'span'

@UnknownCrafts
Copy link

There are pull requests that fix this issue but idk why they are not merged yet.

@smartsingh
Copy link

There are pull requests that fix this issue but idk why they are not merged yet.

I've tried manually applying the fix suggested in the original bug filing, but the issue is persisting. I'm not sure if the issue occurring today is related to the earlier issue that PRs have been created for.

@benbaker76
Copy link

Latest error is in cypher.py not parser.py

@HarrisDePerceptron
Copy link

@headkaze yes. error in cypher.py . tryed using
pip install git+https://github.com/pytube/pytube
and
pip install git+https://github.com/mytja/pytube

@ghost
Copy link

ghost commented Feb 1, 2022

Latest error is in cypher.py not parser.py

the regex search is failing, maybe youtube has changed the javascript.

@benbaker76
Copy link

the regex search is failing, maybe youtube has changed the javascript.

Yes, the regex is searching for a throttling function Bpa\[0\]=function\(\w\) but new function looks like this &&(b=Bpa[0](b),a.set("n",b),Bpa.length||iha(""))}};

@czarnoff
Copy link

czarnoff commented Feb 1, 2022

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

@ghost
Copy link

ghost commented Feb 1, 2022

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is a var that points to iha. If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

works...
i tried and it worked perfectly

@paavampayyan
Copy link

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is a var that points to iha. If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

thanks broooo

richmengsix added a commit to richmengsix/pytube that referenced this issue Feb 1, 2022
@sandeepkumar1101
Copy link

i am also facing Same error

AttributeError: 'NoneType' object has no attribute 'span'

@nabilkrs
Copy link

nabilkrs commented Feb 1, 2022

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

thank you bro it works!!!!!

@goodop
Copy link

goodop commented Feb 1, 2022

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

Good Work!
TQ

@IntenseSoftwares
Copy link

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

It works you should upload it

@BraynStorm
Copy link

BraynStorm commented Feb 1, 2022

This doesn't work for me tho. This is what I get when I use name="iha"

Traceback (most recent call last):
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "D:\Stuff\Projects\assnouncer\assnouncer.py", line 255, in on_message
    await queue_song(q)
  File "D:\Stuff\Projects\assnouncer\assnouncer.py", line 111, in queue_song
    await play_queue()
  File "D:\Stuff\Projects\assnouncer\assnouncer.py", line 132, in play_queue
    stream = yt.streams.get_audio_only()
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 292, in streams
    return StreamQuery(self.fmt_streams)
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 184, in fmt_streams
    extract.apply_signature(stream_manifest, self.vid_info, self.js)
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
    cipher = Cipher(js=js)
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 29, in __init__
    self.transform_plan: List[str] = get_transform_plan(js)
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 198, in get_transform_plan
    return regex_search(pattern, js, group=1).split(";")
  File "C:\Users\Braynstorm\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\helpers.py", line 129, in regex_search
    raise RegexMatchError(caller="regex_search", pattern=pattern)
pytube.exceptions.RegexMatchError: regex_search: could not find match for iha=function\(\w\){[a-z=\.\(\"\)]*;(.*);(?:.+)}

@rishabh3354
Copy link

is pytube is working for anyone, Please share pytube patched branch so that we will be able to install through pip

@oleksandr-shvab
Copy link

I created a fork with the earlier proposed solution if anyone need it.
https://github.com/oleksandr-shvab/pytube

@rishabh3354
Copy link

rishabh3354 commented Feb 1, 2022

I created a fork with the earlier proposed solution if anyone need it. https://github.com/oleksandr-shvab/pytube

Not working

ERROR: Cannot unpack file /tmp/pip-unpack-oah_1k8u/pytube (downloaded from /tmp/pip-req-build-apkmcmlb, content-type: text/html; charset=utf-8); cannot detect archive format
ERROR: Cannot determine archive format of /tmp/pip-req-build-apkmcmlb

@oleksandr-shvab
Copy link

oleksandr-shvab commented Feb 1, 2022

I created a fork with the earlier proposed solution if anyone need it. https://github.com/oleksandr-shvab/pytube

Not working

ERROR: Cannot unpack file /tmp/pip-unpack-oah_1k8u/pytube (downloaded from /tmp/pip-req-build-apkmcmlb, content-type: text/html; charset=utf-8); cannot detect archive format ERROR: Cannot determine archive format of /tmp/pip-req-build-apkmcmlb

Strange. It's working for me. I used command pipenv install -e git+https://github.com/oleksandr-shvab/pytube#egg=pytube and then rebuild the container.

@rishabh3354
Copy link

I created a fork with the earlier proposed solution if anyone need it. https://github.com/oleksandr-shvab/pytube

Not working
ERROR: Cannot unpack file /tmp/pip-unpack-oah_1k8u/pytube (downloaded from /tmp/pip-req-build-apkmcmlb, content-type: text/html; charset=utf-8); cannot detect archive format ERROR: Cannot determine archive format of /tmp/pip-req-build-apkmcmlb

Strange. It's working for me. I used command pipenv install -e git+https://github.com/oleksandr-shvab/pytube#egg=pytube and then rebuild the container.

and this video after using your code doesnot work
https://www.youtube.com/watch?v=twtGL8WbllM

@glubsy
Copy link
Contributor

glubsy commented Feb 1, 2022

@hunterhawley please fix your formatting.

There was a change in youtube's javascript. yt-dlp has already been fixed here is this is indeed the same issue.

@2412rock
Copy link

2412rock commented Feb 1, 2022

I created a fork with the earlier proposed solution if anyone need it. https://github.com/oleksandr-shvab/pytube

Not working
ERROR: Cannot unpack file /tmp/pip-unpack-oah_1k8u/pytube (downloaded from /tmp/pip-req-build-apkmcmlb, content-type: text/html; charset=utf-8); cannot detect archive format ERROR: Cannot determine archive format of /tmp/pip-req-build-apkmcmlb

Strange. It's working for me. I used command pipenv install -e git+https://github.com/oleksandr-shvab/pytube#egg=pytube and then rebuild the container.

This worked

glubsy added a commit to glubsy/livestream_saver that referenced this issue Feb 1, 2022
* pytube/pytube#1218 (comment)
Should take care of missing regex match.
@RexMan04
Copy link

RexMan04 commented Feb 1, 2022

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

It worked! Thanks so much!

@shivpatil007
Copy link

I think the javascipt changed. If you search for Bpa, you don't find a function. Instead you get

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thus Bpa is an array with the function "iha" in location zero . If you use iha for the name, you get a function. I kludged line 293 of cipher.py to go from

name = re.escape(get_throttling_function_name(js))

to

name = "iha"

And I now its working.

I'm not sure how to correctly change the code yet, but if I figure out a proper patch I'll post it.

Its working bro !!!!Thanx

@rishabh3354
Copy link

Mine worked with this, working today fine
pip uninstall pytube pip install git+https://github.com/glubsy/pytube@nfunc_regex_patch

Thanks it also works for me

This fix is not working for this video https://www.youtube.com/watch?v=twtGL8WbllM

@glubsy
Copy link
Contributor

glubsy commented Feb 8, 2022

@rishabh3354 it works. Show us the error you get? I'm not getting any error.

@Benjamin-Yan
Copy link

Mine worked with this, working today fine

pip uninstall pytube pip install git+https://github.com/glubsy/pytube@nfunc_regex_patch

Thanks! It works for me!!!

@rishabh3354
Copy link

rishabh3354 commented Feb 10, 2022

pip install git+https://github.com/glubsy/pytube@nfunc_regex_patch

Error is when i try to download
Error: 'content-length'
video link: https://www.youtube.com/watch?v=twtGL8WbllM
Screenshot from 2022-02-10 23-52-32

@glubsy
Copy link
Contributor

glubsy commented Feb 10, 2022

@rishabh3354 this has nothing to do with this issue.

@rishabh3354
Copy link

rishabh3354 commented Feb 11, 2022

@rishabh3354 this has nothing to do with this issue.

Bro I am not able to download . https://github.com/glubsy/pytube@nfunc_regex_patch
Sorry to say but this patch is Not working ..

@rmerzouki
Copy link

@rishabh3354 this has nothing to do with this issue.

Bro I am not able to download . https://github.com/glubsy/pytube@nfunc_regex_patch Sorry to say but this patch is Not working ..

@rishabh3354 this patch works perfectly well, I just tested it now - BTW, thank you @glubsy - I use it directly in my repo https://github.com/rmerzouki/pytube.

@rishabh3354
Copy link

rishabh3354 commented Feb 11, 2022

@glubsy @rmerzouki

It is failing for Some streams objects

HOW TO REPLICATE

video link https://www.youtube.com/watch?v=twtGL8WbllM

this stream obj is failing which in no.8 in my case (itag="160").

<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400b" progressive="False" type="video">

YouTube('https://www.youtube.com/watch?v=twtGL8WbllM').streams[8].download("/home/warlord/testing", "Basketball.mp4")

ERROR OUTPUT

Error: Traceback (most recent call last):
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/streams.py", line 146, in filesize
    self._filesize = request.filesize(self.url)
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/request.py", line 200, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'
python-BaseException

Process finished with exit code 1

@rishabh3354
Copy link

rishabh3354 commented Feb 11, 2022

@glubsy @rmerzouki

It is failing for Some streams objects

HOW TO REPLICATE

video link https://www.youtube.com/watch?v=twtGL8WbllM

this stream obj is failing which in no.8 in my case (itag="160").

<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400b" progressive="False" type="video">

YouTube('https://www.youtube.com/watch?v=twtGL8WbllM').streams[8].download("/home/warlord/testing", "Basketball.mp4")

ERROR OUTPUT

Error: Traceback (most recent call last):
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/streams.py", line 146, in filesize
    self._filesize = request.filesize(self.url)
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/request.py", line 200, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'
python-BaseException

Process finished with exit code 1

@glubsy no doubt, here we got full of immature programmers who claims to be superb😂

@ifahadone
Copy link

ifahadone commented Feb 13, 2022

@glubsy @rmerzouki
It is failing for Some streams objects
HOW TO REPLICATE
video link https://www.youtube.com/watch?v=twtGL8WbllM
this stream obj is failing which in no.8 in my case (itag="160").
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400b" progressive="False" type="video">
YouTube('https://www.youtube.com/watch?v=twtGL8WbllM').streams[8].download("/home/warlord/testing", "Basketball.mp4")
ERROR OUTPUT

Error: Traceback (most recent call last):
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/streams.py", line 146, in filesize
    self._filesize = request.filesize(self.url)
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/request.py", line 200, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'
python-BaseException

Process finished with exit code 1

@glubsy no doubt, here we got full of immature programmers who claims to be superb😂

the specific itag you are trying to use has no filesize value, therefore you are getting KeyError: 'content-length'.

i suppose, this will get the job done

video.streams.filter(progressive=True).order_by('resolution').asc()

and please be respectful to others,

@rishabh3354
Copy link

@glubsy @rmerzouki
It is failing for Some streams objects
HOW TO REPLICATE
video link https://www.youtube.com/watch?v=twtGL8WbllM
this stream obj is failing which in no.8 in my case (itag="160").
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400b" progressive="False" type="video">
YouTube('https://www.youtube.com/watch?v=twtGL8WbllM').streams[8].download("/home/warlord/testing", "Basketball.mp4")
ERROR OUTPUT

Error: Traceback (most recent call last):
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/streams.py", line 146, in filesize
    self._filesize = request.filesize(self.url)
  File "/home/warlord/projects/env/lib/python3.8/site-packages/pytube/request.py", line 200, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'
python-BaseException

Process finished with exit code 1

@glubsy no doubt, here we got full of immature programmers who claims to be superbjoy

the specific itag you are trying to use has no filesize value, therefore you are getting KeyError: 'content-length'.

you have proved yourself, i am not expecting answers from you btw

RONNCC pushed a commit that referenced this issue Feb 13, 2022
* Fix regex to find throttle function name

The javascript now stores the throttling function name in an array.
Fix #1218

* Fix array parsing

Strip whitespaces around symbol names for future-proofing.

The variable name might be "b" right now, but it could change in the
future.
@rishabh3354
Copy link

this is not fixed, dont close it bro.. dont make us fool. lol

@eliorc
Copy link

eliorc commented Feb 14, 2022

Any expectation on when we are going to see this released in PyPI?

@RONNCC
Copy link
Collaborator

RONNCC commented Feb 14, 2022

Hey @rishabh3354 please don't be rude to other members of the repo - there is a ban functionality.

@eliorc Hey want to confirm the question -- is the fix working but not released in PyPI?

@eliorc
Copy link

eliorc commented Feb 14, 2022

Yes @RONNCC , I've installed from the latest on master and it works fine for me

@RONNCC
Copy link
Collaborator

RONNCC commented Feb 14, 2022

@eliorc thanks for update -- just released on PyPi

@MtnDewBoi
Copy link

MtnDewBoi commented Feb 15, 2022

The
name = "iha"
fix isn't working for me, have there been any other methods?

Yes, I have tried a fresh pip install of the release from a few hours ago.

@vcpavao
Copy link

vcpavao commented Feb 15, 2022

Just did a fresh pip, pytube, pydub reinstall and this issue has gone away.

@VLomonovskis
Copy link

VLomonovskis commented Feb 17, 2022

var Bpa=[iha];g.k=Jz.prototype;g.k.GC=function(a){this.segments.push(a)};

Thank you! It did help, especially explanation. Name "iha" did not work for me, but "yha" worked well. I believe it is case by case (that why originally it was determined by function). So, just print "js" variable to see its content before name and search for "Spa" in the text, you will find name of function you need. In my case it was "Spa=[yha]". Then, just set "name" variable to this value, in my case "name = 'yha'".
Idealy, we need to fix "get_throttling_function_name" so it will find correct function name.

@RONNCC
Copy link
Collaborator

RONNCC commented Feb 26, 2022

@VLomonovskis could you submit a patch :-)

@el07694
Copy link

el07694 commented Mar 16, 2022

And you will bang your head again as soon as the javascript gets recompiled. Better use this branch instead https://github.com/glubsy/pytube/tree/nfunc_regex_patch

Changing :

  • File cipher.py, line 268 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$]{3})(\[\d+\])?\([a-z]\)',
  • File cipher.py, lines 275 -> 277 to
            logger.debug("finished regex search, matched: %s", pattern)
            if len(function_match.groups()) == 1:
                return function_match.group(1)
            idx = function_match.group(2)
            if idx:
                idx = idx.strip("[]")
                array = re.search(
                    r'var {nfunc}\s*=\s*(\[.+?\]);'.format(
                        nfunc=function_match.group(1)),
                    js
                )
                if array:
                    array = array.group(1).strip("[]").split(",")
                    array = [x.strip() for x in array]
                    return array[int(idx)]

Thanks, helped me fix the error!
Hopes that it will last.

Thanks!

@ghost
Copy link

ghost commented Mar 27, 2022

Hello guys,
Pytube fixes bugs, so write:

pip install --upgrade pytube

in your CMD

@ShifterinoSama
Copy link

Hello guys!
I am still having problems with:

Exception has occurred: AttributeError (note: full exception trace is shown but execution is paused at: load)
'NoneType' object has no attribute 'span'

I tried everything you mentioned here but nothing seems to work for me.

My code worked perfectly a few months ago, but now I am getting an error
Here is my code:
https://github.com/ShifterinoSama/YoutubeDownloader-Converter/blob/main/downloader_en.pyw

The error appears on lines 73, 78, 81

Does anyone know how to fix this?

PS: Sorry for my bad English and bad coding, I am a self-learner.

pip 22.0.4
pytube 12.0.0

@shameerariff
Copy link

``> Hello guys, Pytube fixes bugs, so write:

pip install --upgrade pytube

in your CMD

I still have the problem

`pytube https://www.youtube.com/watch?v=EbHxWU52ZH0
Loading video...
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/pytube/main.py", line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "/usr/local/lib/python3.9/dist-packages/pytube/extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 43, in init
self.throttling_plan = get_throttling_plan(js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "/usr/local/lib/python3.9/dist-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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/bin/pytube", line 8, in
sys.exit(main())
File "/usr/local/lib/python3.9/dist-packages/pytube/cli.py", line 53, in main
_perform_args_on_youtube(youtube, args)
File "/usr/local/lib/python3.9/dist-packages/pytube/cli.py", line 60, in _perform_args_on_youtube
download_highest_resolution_progressive(
File "/usr/local/lib/python3.9/dist-packages/pytube/cli.py", line 474, in download_highest_resolution_progressive
stream = youtube.streams.get_highest_resolution()
File "/usr/local/lib/python3.9/dist-packages/pytube/main.py", line 296, in streams
return StreamQuery(self.fmt_streams)
File "/usr/local/lib/python3.9/dist-packages/pytube/main.py", line 188, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "/usr/local/lib/python3.9/dist-packages/pytube/extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 43, in init
self.throttling_plan = get_throttling_plan(js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "/usr/local/lib/python3.9/dist-packages/pytube/cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "/usr/local/lib/python3.9/dist-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
`

@RONNCC
Copy link
Collaborator

RONNCC commented Apr 15, 2022

hey @shameerariff -- would you mind submitting a patch to fix the above ;)

@shameerariff
Copy link

Hi @RONNCC It was already applied in the git as follows

def get_throttling_function_name(js: str) -> str:
"""Extract the name of the function that computes the throttling parameter.

:param str js:
    The contents of the base.js asset file.
:rtype: str
:returns:
    The name of the function used to compute the throttling parameter.
"""
function_patterns = [
    # https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-865985377
    # https://github.com/yt-dlp/yt-dlp/commit/48416bc4a8f1d5ff07d5977659cb8ece7640dcd8
    # var Bpa = [iha];
    # ...
    # a.C && (b = a.get("n")) && (b = Bpa[0](b), a.set("n", b),
    # Bpa.length || iha("")) }};
    # In the above case, `iha` is the relevant function name
    **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$]{3})(\[\d+\])?\([a-z]\)',**

]
logger.debug('Finding throttling function name')
for pattern in function_patterns:
    regex = re.compile(pattern)
    function_match = regex.search(js)
    if function_match:
        **logger.debug("finished regex search, matched: %s", pattern)
        if len(function_match.groups()) == 1:
            return function_match.group(1)
        idx = function_match.group(2)
        if idx:
            idx = idx.strip("[]")
            array = re.search(
                r'var {nfunc}\s*=\s*(\[.+?\]);'.format(
                    nfunc=function_match.group(1)),
                js
            )
            if array:
                array = array.group(1).strip("[]").split(",")
                array = [x.strip() for x in array]
                return array[int(idx)]**

raise RegexMatchError(
    caller="get_throttling_function_name", pattern="multiple"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.