twlite / youtube-sr

A dead-simple youtube metadata scraper
https://npmjs.com/package/youtube-sr
MIT License
115 stars 15 forks source link

TypeError: Cannot read properties of undefined (reading "videoOwnerRenderer") #32

Closed choco-green closed 2 years ago

choco-green commented 3 years ago
TypeError: Cannot read properties of undefined (reading 'videoOwnerRenderer')
    at Function.Util.getVideo (PROJECT_LOCATION\node_modules\youtube-sr\build\Util.js:1:13984)
    at Function.<anonymous> (PROJECT_LOCATION\node_modules\youtube-sr\build\main.js:1:5363)
    at step (PROJECT_LOCATION\node_modules\youtube-sr\build\main.js:1:1854)
    at Object.next (PROJECT_LOCATION\node_modules\youtube-sr\build\main.js:1:1127)
    at fulfilled (PROJECT_LOCATION\node_modules\youtube-sr\build\main.js:1:528)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

The error that I am getting is very similar to the current open issue 26

I believe this can be easily reproducible with this specific video YouTube.getVideo("https://www.youtube.com/watch?v=kTJczUoc26U") it simply drops the type error without doing anything with what it returns.

I noticed that the subscribers are null where the subscriber on the actual video is not, and it is this line of code that seems to break the program, but I'm not too sure why it breaks the code, perhaps a ? missing on videoOwnerRenderer as @Ayeven suggested: (Possible fix : subscribers: info.owner.videoOwnerRenderer.subscriberCountText.simpleText?.replace(" subscribers", "") change to subscribers: info.owner.videoOwnerRenderer?.subscriberCountText?.simpleText ?? " ",)

One more thing, I think the error will pop up for all url, icon and subscriber, because perhaps info.owner is undefined (?), I'm not quite sure

Full code below (typescript)

async function getVideo(messageContent: string, message: Message, serverQueue: ServerQueue, overallQueue: Map<string, ServerQueue>) {
    await YouTube.getVideo(messageContent)
        .then(async video => {
            serverQueue.songs.push({
                title: video.title,
                url: `https://www.youtube.com/watch?v=${video.id}`,
                thumbnail: video.thumbnail.url
            });

            if (serverQueue.connection)
                message.channel.send(`${video.title} has been added to the queue!`, { files: [video.thumbnail.url] });

            if (!serverQueue.connection) {
                try {
                    const connection = await message.member.voice.channel.join();
                    serverQueue.connection = connection;
                    playSong(message, serverQueue.songs[0], serverQueue, overallQueue);
                } catch (err) {
                    console.log(err);
                    overallQueue.delete(message.guild.id);
                    return message.channel.send(err);
                }
            }
        })
        .catch(err => console.error(err));
}

Can be reduced to this, while error is still persistantly showing

async function getVideo() {
    await YouTube.getVideo("https://www.youtube.com/watch?v=kTJczUoc26U")
        .catch(console.error);
}

The object which was returned

Video {
  id: 'kTJczUoc26U',
  title: 'The Kid LAROI, Justin Bieber - STAY (Official Video)',
  description: null,
  durationFormatted: '2:38',
  duration: 158000,
  uploadedAt: '2 months ago',
  views: 208567211,
  thumbnail: Thumbnail {
    id: 'kTJczUoc26U',
    width: 480,
    height: 270,
    url: 'https://i.ytimg.com/vi/kTJczUoc26U/hqdefault.jpg?sqp=-oaymwEjCOADEI4CSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLA909zFxhhQ3SS5RNLZlHzX_Ex7sw'
  },
  channel: Channel {
    name: 'The Kid LAROI.',
    verified: true,
    id: 'UC6G2vAJrt407lwiynW116Eg',
    url: 'https://www.youtube.com/channel/UC6G2vAJrt407lwiynW116Eg',
    icon: {
      url: 'https://yt3.ggpht.com/kz1Lb_TY9CtYJjObgL-1xN3tabgoHOPrrZCVqq-11kVgogJGxRlK0eFf6G6IIG7PPQ-xIWkhLw=s88-c-k-c0x00ffffff-no-rj',
      width: 68,
      height: 68
    },
    subscribers: null
  },
  likes: 0,
  dislikes: 0,
  live: false,
  private: false,
  tags: []
}

Lastly, this is my first time ever posting an Issue on Github, please tell me if I have done anything wrong or I missed anything in the issue, thanks!