pytube: [BUG] 'NoneType' object has no attribute 'span'

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

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 7
  • Comments: 91 (6 by maintainers)

Commits related to this issue

Most upvoted comments

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.

Dear @mahdizarepro in the get_throttling_function_code function in the cipher.py file I have changed name =“iha” to name = “jha” and now it works for me… Until the next change ‘kha’ 😃 do not hesitate to use my repo :

pip install git+https://github.com/rmerzouki/pytube

Mine worked with this, working today fine

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

name="iha" fix did not work for me either. Looks like something might have changed in the past few hours.

None of the fix here are working for me, I’m out of solution right now

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

Dear @mahdizarepro in the get_throttling_function_code function in the cipher.py file I have changed name =“iha” to name = “jha” and now it works for me… Until the next change ‘kha’ 😃 do not hesitate to use my repo :

pip install git+https://github.com/rmerzouki/pytube

replacing ‘iha’ for ‘jha’ have worked for me too

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.

is there any soln other than this (name = “iha” fix has stopped working)

name = “iha” fix has stopped working since today morning (03 JAN 11:05 IST). Worked fine till yesterday

“iha” fix didn’t work for me either 😦

Keeping an eye on this, hopefully it gets figured out soon.

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

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(""))}};

@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😂

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

@rishabh3354 this has nothing to do with this issue.

@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,

@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

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

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

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.

Still doesn’t work 😕

Dear @quantum-hash use this branch https://github.com/glubsy/pytube/tree/nfunc_regex_patch or do this

pip install git+https://github.com/rmerzouki/pytube

It’s not working for me neither. I tried suggested solutions here and non of them worked

Solutions tried:

  1. name = “iha”
  2. glubsy fix: https://github.com/glubsy/pytube/commit/bcae434d1da32538ec7b21d11e271e86d9fafdd1

Error msg:

File "...python3.9/http/client.py", line 614, in _safe_read
    raise IncompleteRead(data, amt-len(data))
http.client.IncompleteRead: IncompleteRead(1666671 bytes read, 7770513 more expected)

Video: https://www.youtube.com/watch?v=8bAzi4zIqK0

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

Latest error is in cypher.py not parser.py

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?

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

@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

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

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

Dear @mahdizarepro in the get_throttling_function_code function in the cipher.py file I have changed name =“iha” to name = “jha” and now it works for me… Until the next change ‘kha’ 😃 do not hesitate to use my repo :

pip install git+https://github.com/rmerzouki/pytube

Thanks, it works

Dear @mahdizarepro in the get_throttling_function_code function in the cipher.py file I have changed name =“iha” to name = “jha” and now it works for me… Until the next change ‘kha’ 😃 do not hesitate to use my repo :

pip install git+https://github.com/rmerzouki/pytube

its work for me thanks

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

thanks! I spent a morning banging my head with this problem

If you people cared to look around before whining like blind moles, you would have noticed there is a pending pull request with the fix to this problem at https://github.com/pytube/pytube/pull/1222

The error only happens in me every time I do “YouTube(url).streams” while “YouTube(url).streaming_data” can return object

name = “iha” seemed to be working fine at the time of previous issue, however, I think now we have got new one.

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

@rishabh3354 try this :

pip install git+https://github.com/rmerzouki/pytube

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.