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

Key Error of 'Assets' when attempting to call 'YouTube()' #777

Closed
vincehartman38 opened this issue Oct 27, 2020 · 14 comments
Closed

Key Error of 'Assets' when attempting to call 'YouTube()' #777

vincehartman38 opened this issue Oct 27, 2020 · 14 comments

Comments

@vincehartman38
Copy link

When doing the following command:

from pytube import YouTube
yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')

I'm seeing the key error 'assets':

KeyError Traceback (most recent call last)
in
----> 1 yt = YouTube('http://youtube.com/watch?v=9bZkp7q19f0')

C:\Program Files\Anaconda3\lib\site-packages\pytube_main_.py in init(self, url, defer_prefetch_init, on_progress_callback, on_complete_callback, proxies)
89
90 if not defer_prefetch_init:
---> 91 self.prefetch()
92 self.descramble()
93

C:\Program Files\Anaconda3\lib\site-packages\pytube_main_.py in prefetch(self)
181 self.vid_info_raw = request.get(self.vid_info_url)
182 if not self.age_restricted:
--> 183 self.js_url = extract.js_url(self.watch_html)
184 self.js = request.get(self.js_url)
185

C:\Program Files\Anaconda3\lib\site-packages\pytube\extract.py in js_url(html)
141 The html contents of the watch page.
142 """
--> 143 base_js = get_ytplayer_config(html)["assets"]["js"]
144 return "https://youtube.com" + base_js
145

KeyError: 'assets'

@AssesBesses
Copy link

Hi,
Since yesterday I have the same, on every version of pytube (pytube, pytube3, pytubex). I thought youtube banned my ip address because I downloaded almost 300GB yesterday and it suddenly stopped working. It looks like youtube changed something yesterday which made pytube not working at all.

I found a problem. I don't know regex, so my solution is primitive but works fully.
There is a js_url() function in the extract.py file.
Delete it completely (or rename it).
Create a new js_url() function, it should look like this:

def js_url(html: str) -> str: start = html.find('src="/s/player/') +5 stop = html[start:].find('base.js') + 7 js_url = html[start:start + stop] return "https://youtube.com" + js_url

Doesn't work with age-restricted videos, but now I know why, I'll post a revised code tomorrow. Today I downloaded over 500 videos without any errors.

@tfdahlin
Copy link
Collaborator

@vincehartman38 @AssesBesses this problem was fixed in the most recent update to the repository, so you will need to reinstall pytube in order to fix it.

To install from this repository, I recommend you first uninstall pytube with pip uninstall pytube, then re-install it with python -m pip install git+https://github.com/nficano/pytube.

@FeedFall8
Copy link

FeedFall8 commented Oct 28, 2020

Hello! im still encountering the same issue(yes im on the latest version of pytube)
you may have some further revisions/fixing to do @tfdahlin

after running the test:
>>> from pytube import YouTube
>>> yt = YouTube('https://www.youtube.com/watch?v=SOFCQ2bfmHw')
i immediately received and error:

site-packages\pytube\extract.py", line 143, in js_url base_js = get_ytplayer_config(html)["assets"]["js"] KeyError: 'assets'

@tfdahlin
Copy link
Collaborator

@FeedFall8 are you sure you've installed it correctly? that error is coming from code that is no longer in the codebase

@AssesBesses
Copy link

@FeedFall8 are you sure you've installed it correctly? that error is coming from code that is no longer in the codebase

I can confirm that this version works very well.

@urna
Copy link

urna commented Oct 28, 2020

@FeedFall8 are you sure you've installed it correctly? that error is coming from code that is no longer in the codebase

I can confirm that this version works very well.

confirm too

@FeedFall8
Copy link

@tfdahlin yes im using latest, the error still occurs though, should i use python 3.7 rather than 3.8 for pytube?

@arpitdiwan
Copy link

@FeedFall8 are you sure you've installed it correctly? that error is coming from code that is no longer in the codebase

It will not work if you install the using the pip. I tried reinstalling "python -m pip install git+https://github.com/nficano/pytube" in command line but nothing happened. I copied the fix in the codebase and paste in the whole code in the extract.py and it works fine. @tfdahlin I don't know why pip install doesn't work for me but your fix does. thanks.

@arpitdiwan
Copy link

@tfdahlin yes im using latest, the error still occurs though, should i use python 3.7 rather than 3.8 for pytube?

Try copy all the code from the codebase and paste it to your pytube/extract.py this will work.

@FeedFall8
Copy link

ah ok thanks

@tfdahlin
Copy link
Collaborator

@FeedFall8 there are a few possible reasons that I can think of that you might be running into an issue with your installation.

  1. Are you installing requirements from a file (e.g. using pip install -r requirements.txt)? If so, you would need to update that file to use this git repository, instead of simply having pytube in there.
  2. Have you at any point installed a different version of pytube? For example, some people in the past have recommended installing pytube3, or pytubex, and these installations can cause conflicts because of how the libraries get installed. If you have, be sure to pip uninstall all of those libraries. You can run pip uninstall pytube pytube3 pytubex, then run python -m pip install git+https://github.com/nficano/pytube to reinstall the most recent version of this repository
  3. Are you using a virtual environment? It's possible you don't have the correct virtual environment enabled.
  4. Are you sure you're using the right version of pip? If you have both python2 and python3 installed on your system, you may need to use pip3 instead of pip to install the package, as pip will be associated with python2 and pip3 will be associated with python3.

@tfdahlin
Copy link
Collaborator

While @arpitdiwan's solution will work as a short term solution, it will most likely be better to correctly install the package rather than rely on modifying a bad package install.

@RONNCC
Copy link
Collaborator

RONNCC commented Oct 31, 2020

Believe this should be fixed now in most recent version @vincehartman38 -- please reopen issue if not so.

Thanks!

@RONNCC RONNCC closed this as completed Oct 31, 2020
@vincehartman38
Copy link
Author

We confirmed this now works. Thanks so much everyone for responding so quickly!

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

No branches or pull requests

7 participants