SuspiciousLookingOwl / youtubei

Get Youtube data such as videos, playlists, channels, video information & comments, related videos, up next video, and more!
https://suspiciouslookingowl.github.io/youtubei
MIT License
240 stars 50 forks source link

[BUG] Certain videos throw Uncaught Error originating in the Video.js load() method #28

Closed kevinpfeifle closed 3 years ago

kevinpfeifle commented 3 years ago

Describe the bug For certain videos being loaded by the client via the getVideo() method, an error occurs during the load() method of the video due to expected results from the Youtube API being in an unexpected index. As such, the code is searching for an undefined field, because the field is in a different index of the array.

To Reproduce When creating a Client object and calling the getVideo() method with the following value as parameter videoIdOrUrl, "kzBx8TWcrG4", it will attempt to create a new instance of a Video object and call its load() method. During this load() method it will attempt to set this.commentContinuation with a sub property of contents[2].itemSectionRenderer.contents

For this example, the itemSectionRender property is actually in contents[3], and the existing code attempts to access a property of an undefined field, and as such errors out. Ideally, this should be altered such that it searches the indices of the array to find the correct index with the itemSectionRenderer property.

Expected behavior I would expect that any youtube video should be able to run through this process and be loaded correctly. It should search for the itemSelectionRenderer property in whatever index of the array it exists in.

Screenshots Below is a screenshot of the contents array mentioned above. You can see that the itemSectionRenderer property is actually in index 3 instead of index 2. image

Below is a screenshot of the load() method in question. This is in classes/Video.js, and you can see the contents array highlighted image

Below is a screenshot of the load() method which I slightly altered to allow for this scenario to work. By wrapping it in a try/catch and checking index 3 on a failure, I did get this video to work. Ideally, this should be fixed to accommodate the value coming from any index? image

Additional context This is the exact Youtube Video I was testing with to produce this error: https://www.youtube.com/watch?v=kzBx8TWcrG4&ab_channel=-MichaelGhelfi-RPGAudio

I believe this error may have something to do with newer merchandise functionality on youtube videos, based on the "merchandiseShelfRender" being in index 2 of the array in this example. You may even be able to replicate this with other videos with the merchandise shelf.

I found this error because the youtubei module is a dependency of the "discord-music-player" which I am using for a discord bot. I was able to trace the error I was dealing with down to the code above. If you want to fix this, and have any questions feel free to reach out!

Thank you for maintaining this great module :)

SuspiciousLookingOwl commented 3 years ago

I knew hard-coding the index instead of doing .find(x => x.itemSectionRenderer) will eventually comes back to bite me in the butt lol.

I'll fix this soon, thanks for reporting!

kevinpfeifle commented 3 years ago

Lol! We'll all been there before :)

Thanks for the fast response, and again thanks for making this great module!

SuspiciousLookingOwl commented 3 years ago

Fixed on https://github.com/SuspiciousLookingOwl/youtubei/commit/c7e059691a6dfa24d18180fcd7adc338711380a3

Try to upgrade to version 0.0.1-rc.24 😄