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

Only one result in Items. #6

Open
gaycookie opened this issue Aug 28, 2019 · 3 comments
Open

Only one result in Items. #6

gaycookie opened this issue Aug 28, 2019 · 3 comments

Comments

@gaycookie
Copy link

Dear developer!

I'm not sure if I'm doing something wrong here, but I think I'm doing everything according to the documentation.

But when I use

    getInfo(args).then((info) => {
        res.json(info.items);
    })

It only gives me an array with only one item, I tried it with different searched that I was certain of that it would give me more results when I search it on YouTube itself.

Maybe something is up with the API, or was it by intention that you only get one result?

Sincerely Ashley

@TheBITLINK
Copy link
Member

TheBITLINK commented Aug 30, 2019

Hey, sorry for the late reply.

By default the API fetches one item if you provide a search string.

To fetch multiple results, you'd want to prefix the search with ytsearch{n}:, like so:

getInfo('ytsearch10:This is an example', [], true).then((info) => {
    // info.items contains 10 entries
    res.json(info.items);
})

This is how youtube-dl handles searches. Although depending on the use case, it would be better to fetch search results from the YouTube API directly since:

  1. youtube-dl has to extract metadata from every search result, and this is a very slow process (unless you only want URLs)
  2. There's no way to do pagination IIRC, you can only fetch the first n items.

@TheBITLINK
Copy link
Member

Also, the third argument is a boolean that defines when the promise is resolved:

If set to true, the promise will resolve once all items are fetched:

getInfo('ytsearch10:This is an example', [], true).then((info) => {
    console.log(info.items);
})

If set to false or omitted, it will resolve as soon as the first item is available, and partial will be set to true. You must listen to the video and done events for the remaining items:

getInfo('ytsearch10:This is an example').then(info => {
  if (info.partial) {
    info.on('video', v => console.log(v.title))
    info.on('done', () => console.log(info.items)
  }
})

@gaycookie
Copy link
Author

Ah, thank you so much for this explanation!

Problem only is, the project I'm working with requires the user itself to do the api calls.
So when I implement the official YouTube API, they need to add their own API key.

I will make that as an option, IF they insert their own API key, but for the ones that do not want that
I wanted to use this option, so it will still search, only limited options!

I will have a look in what you all gave me, and maybe you can also add this to your project as explanation 😄

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

2 participants