DrKain / scrape-youtube

A lightning fast package to scrape YouTube search results
MIT License
108 stars 30 forks source link

Custom Filter #70

Closed eliklein02 closed 4 months ago

eliklein02 commented 4 months ago

Please make sure you have searched for your question in the closed issues before opening an issue

Hi, you mention in the readme that there are custom filter options, but don't really explain how to use it. Are you able to give a bit more info? And would I be able to use it to limit the searches to come from specific channels?

DrKain commented 4 months ago

It seems like the image in the wiki is broken. You need to fetch the SP value yourself for whatever your target is.

image

youtube.search('some query', { sp: 'EgYIAxABGAM%253D' });
DrKain commented 4 months ago

As an extra note if you specifically want "shorts" there is no filter for this, but you can add "#shorts" to any query. The quotes are required for a strict search.

eliklein02 commented 4 months ago

yeah I saw that, it's more like i want to limit the search to specific channels, or like you said upload date. Can I do more than one? like all videos that are searched should be from this channels sorted by date

DrKain commented 4 months ago

Unfortunately no. This is all YouTube has available for searches. There are other packages out there for getting more specific data from channels or you could use their API.

eliklein02 commented 4 months ago

yeah that was my original plan to use the api, but the goal of my use is to create an app that people can use and the api gets used up in literally four seconds, especially if for every search query I also need to do a second call for the channel info for each video. So this is easier. But it would be cool if I can implement these filter options with their own filtering, instead of me creating my own checks. Like if I only want to display results from DudePerfect, I need to just not display if the videos.channel.id doesnt match their id, but a lot of the 20 results might get dropped like that, whereas if the search filtered it out before I got back the array, then all 20 would match that filter. I hope I'm being articulate :)

DrKain commented 4 months ago

I understand, but this package doesn't do that. The point was to pull YouTube search results as fast as possible without YouTube's fussy API limits. If you'd like you're welcome to create a new issue requesting all these other features, another user might contribute but it's not something I can look into anytime soon. Sorry for the inconvenience.

eliklein02 commented 4 months ago

Nah you don't need to apologize you don't owe me anything :) Thanks it's really a great package.

eliklein02 commented 4 months ago

Interesting, neither

const videos = await youtube.search(q, { sp: 'EhIIBxAD' });

nor

const options = {
            type: 'video',
            request: {
                headers: {
                    Cookie: 'PREF=f2=8000000',
                }
            },
            sp: 'EjUJKA'
        };
        const videos = await youtube.search(q, options);

works. Am I doing something obviously wrong?

eliklein02 commented 4 months ago

and it seems that there are a lot of options i can add into a youtube search url like max results and different sort and filter options, should all of those work with this package?

DrKain commented 4 months ago

EhIIBxAD and EjUJKA do not appear to be valid. What filters are you trying to use? Also you do not need to pass type in the second example, as types use the sp parameter. As for your second message: You can use any combination of filters as long as the sp parameter is valid.

eliklein02 commented 4 months ago

So for the non validity of those sp values I just took them from chatgpt. I'll try again with valid ones. And as for what you said about being able to use any parameters as long as they're valid as sp values, my question was in addition to the sp parameter, it looks like YouTube has other parameters I can pass into the url, like channelId, which would limit the search to that id. So my question is let's say this is my code const videos = await youtube.search(q, { sp: 'whatever', channelId: 'whatever'}); Will this work? Or this const videos = await youtube.search(q, { sp: 'whatever', uploadDate: 'whatever'});

DrKain commented 4 months ago

I'm sorry but I can't give you a definitive answer because I was not aware you could pass a channel ID in the first place. If you would like to use extra URL parameters you can use the params key.

https://github.com/DrKain/scrape-youtube/blob/master/src/interface.ts#L33

eliklein02 commented 4 months ago

const options = { type: 'video', request: { headers: { Cookie: 'PREF=f2=8000000' } }, params: { sp: 'EgYIAxABGAM%253D' } }; const videos = await youtube.search(q, options); this still doesnt seem to be working, I'm assuming I'm doing something wrong. Correct?

DrKain commented 4 months ago

This is getting a bit silly now. The sp value is correct but you're using it incorrectly.

const youtube = require('scrape-youtube');

const search = async (q: string) => {
  const options = { sp: "EgYIAxABGAM%253D" };
  const results = await youtube.search(q, options);

  console.log(results.videos);
};

search("Poets of the Fall");
eliklein02 commented 4 months ago

Oh yikes. I assumed that if I can use extra params that YouTube uses in their url in a separate object called params, then sp values would work there too as they're part of YouTube's search url too. Thanks for your help

DrKain commented 4 months ago

All types (channel, video, playlist, ect) use the sp value. As mentioned in the readme, wiki, the code and even the intellisense comments: the sp is a filter override. Glad you managed to get it working in the end. If you have any other questions please use discussions, unless it's an error then I'd appreciate it if you opened a new issue instead of adding to this.