SuspiciousLookingOwl / scrape-yt

Simple lib to scrape information from youtube such as search results, video information, related videos, playlist information and up next video
https://www.npmjs.com/package/scrape-yt
MIT License
9 stars 3 forks source link

Search breaks on keywords with "typo" [BUG] #17

Closed 3chospirits closed 4 years ago

3chospirits commented 4 years ago

Describe the bug Search works for most keywords, but when the search terms trigger youtube's "showing results for xxxxxxx Search instead fo xxxxxxx", the search() function has a

(node:8684) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 
'videoId' of undefined
at Object.parseSearch (C:\Users\xxxxx\Projects\Music\node_modules\scrape- 
yt\dist\common\parser.js:142:30)

To Reproduce Use search function with any "typoed" keywords as search query. search(keywords) Some example keyword combos that result in error:

Expected behavior Should return normal search query results like normal without errors

Screenshots N/A

Additional context N/A

SuspiciousLookingOwl commented 4 years ago

I can't reproduce this, what version are you using?

3chospirits commented 4 years ago

Ahh, sorry I missed this in the reproduce steps. Adding {type:"video"} to parameters breaks it. This code errors:

let {search} = require("scrape-yt")
search("helllo", {type:"video"}).then(r=>console.log(r));
(node:8684) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 
'videoId' of undefined
at Object.parseSearch (C:\Users\xxxxx\Projects\Music\node_modules\scrape- 
yt\dist\common\parser.js:142:30)

If I run it without the type option it does not have any issues:

let {search} = require("scrape-yt")
search("helllo").then(r=>console.log(r));
[
  {
    id: 'YQHsXMglC9A',
    title: 'Adele - Hello',
    duration: 367,
    thumbnail: 'https://i.ytimg.com/vi/YQHsXMglC9A/hqdefault.jpg?sqp=-oaymwEjCNACELwBSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLABnmZFMUz4mRFtZtiMekk8VLKZyw',
    channel: {
      id: 'UCsRM0YB_dabtEPGPTKo-gcw',
      name: 'Adele',
      url: 'https://www.youtube.com/channel/UCsRM0YB_dabtEPGPTKo-gcw'
    },
    uploadDate: '4 years ago',
    viewCount: 2681828126,
    type: 'video'
  }
. . .
3chospirits commented 4 years ago

Also, I am using 1.4.0

SuspiciousLookingOwl commented 4 years ago

Ah okay, it's indeed throwing an error with {type:"video"}, I will look into this.

3chospirits commented 4 years ago

Thank you!! Please lmk if you find a fix!

3chospirits commented 4 years ago

Not sure if this is the proper way to resolve this issue, but i found that adding this in line 140 of parser.js removes the error:

if (data === undefined) continue;
 if (searchType === "video") {
                data = data.videoRenderer;
                if (data === undefined) continue;
                result = {
. . .
3chospirits commented 4 years ago

The issue itself was actually related to ads on youtube. Adding this:

if (searchType === "video") {
                if (data.videoRenderer === undefined) console.log(data);

gave me this output:

{
  searchPyvRenderer: {
    ads: [ [Object] ],
    trackingParams: 'CIsBEJMmGAAiEwjh3aWuxePqAhWVMAMKHQAgAvE='
  }
}

its the annoying youtube ads lol

SuspiciousLookingOwl commented 4 years ago

Try updating to 1.4.1, should fix it.