4

I have been this error when running my Pytube script:

     signature = cipher.get_signature(js, stream['s'])
     KeyError: 's'

My code is like this:

    url = 'https://www.youtube.com/watch?v='
    train_List = []
    i = 0
    while i < len(my_list):
        if len(my_list[i]) > 6:
            urls = url + my_list[i]
            train_List.append(urls)
            yt=YouTube(train_List[i])
            t=yt.streams.filter(only_audio=True).all()
            t[0].download('/pathtofolder')
            i+=1

I also tried:

    t=yt.streams.filter(file_extension='mp4').all()

I altered the cipher.py and helper.py files per the recommendation here: https://github.com/nficano/pytube/issues/353#issuecomment-455116197

but it hasn't fixed the problem. After making the alterations, I got the error noted above.

Next, I ran "pip install pytube --upgrade" per some other recommendations. Still getting the KeyError after it downloads a few audio files.

I've also implemented this in mixins.py, per the github issues:

    if ('signature=' in url) or ('&sig=' in url) or ('&lsig=' in url):

but now it's hanging after 3 uploads.

Does anyone have a fix for this?

topplethepat
  • 419
  • 4
  • 18

6 Answers6

1

I fixed this issue (for me, at least) by changing line 49 in mixins.py to this:

signature = cipher.get_signature(js, stream['url'])

instead of

signature = cipher.get_signature(js, stream['s'])

and then changing lines 55-63 to

logger.debug(
    'finished descrambling signature for itag=%s\n%s',
    stream['itag'], pprint.pformat(
        {
            'url': stream['url'],
            'signature': signature,
        }, indent=2,
    ),
)
PepperLola
  • 11
  • 4
0

Have the same problem. If I try a second time it works most of the time...

url = 'https://www.youtube.com/watch?v=gQrkvZeE3Uc'
yt = YouTube(url)
yt.streams.filter(progressive=True, subtype='mp4').order_by('resolution').desc().last().download()

Thanks for any help

0

You have to fix the cipher.py file in lib/python3.6/site-packages/pytube:

In cipher.py, change pattern starting at line 38 to this:

pattern = [
    r'(["\'])signature\1\s*,\s*(?P<sig>[a-zA-Z0-9$]+)\(',
    r'\.sig\|\|(?P<sig>[a-zA-Z0-9$]+)\(',
    r'yt\.akamaized\.net/\)\s*\|\|\s*.*?\s*c\s*&&\s*d\.set\([^,]+\s*,\s*(?:encodeURIComponent\s*\()?(?P<sig>[a-zA-Z0-9$]+)\(',
    r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*(?:encodeURIComponent\s*\()?\s*(?P<sig>[a-zA-Z0-9$]+)\(',
    r'\bc\s*&&\s*d\.set\([^,]+\s*,\s*\([^)]*\)\s*\(\s*(?P<sig>[a-zA-Z0-9$]+)\(',
]
mr. M
  • 31
  • 5
  • thanks.This seems to be helping, however after downloading about 6 this way I got a VideoUnavailable error, and currently am trying to implement some error handling. After that works I'll be able to know if this regex change will fix it. – topplethepat Jun 03 '19 at 20:15
  • 1
    Well, what I did I create a while loop till the video is downloaded... That worked for me :) – mr. M Jun 04 '19 at 20:36
0

One of the ways to solve it: pip install git+https://github.com/nficano/pytube.git

Other than that, check this thread

techkuz
  • 2,241
  • 3
  • 17
  • 45
0

I did pip uninstall pytube.

Checked my python version with python --version, turned out to be the Anaconda 3.6.8 version.

So I did pip3 install pytube, pip3 being for Python 3 I believe.

It works no problems now.

mLstudent33
  • 669
  • 2
  • 7
  • 17
0

had same issue, based on other answers that installed it from sources, just checked my current pytube version, which was 9.5.0 and see that the last released is 9.5.2, so I just run pip install --upgrade pytube and worked perfectly

Rodrigo Laguna
  • 929
  • 1
  • 12
  • 29