IcySnex / YouTubeMusicAPI

YouTube Music Web API Wrapper for C#
GNU General Public License v3.0
3 stars 0 forks source link

Option to fetch similar content for a provided ID #2

Closed martinle068 closed 2 months ago

martinle068 commented 2 months ago

Hello, YouTube API depricated the option to fetch similar content for a provided ID a few months ago. Do you think it would be possible to implement this sort of a feature or is it impossible?

Respectfully, Martin

IcySnex commented 2 months ago

Hello, I've looked at this suggestion and its sure possible but there are a few different ways you could achieve such a thing.

The most simple way you can achieve this without any update of the API wrapper would be using the "radio" provided by search results. If you search for a query all song/video/album/community playlist results return a "radio" with a playlist Id which then can be used to get all the radios similar songs.

But if you want to get similar content from any video via the Id this wouldn´t work. Instead you would have to send a request to YouTube Musics "next"-endpoint. This will return various song info just like the GetSongVideoInfoAsync() function but unlike this function it will also return additional info like the "radio" playlist which then can be used again for fetching all its songs. (As I went through the response, I have noticed this endpoint would be a way better approach than the current GetSongVideoInfoAsync() implementation. I definitely should update this sooner or later.)

But this will only return similar songs/videos. If you want actual related content you would have to send a request to YouTube Musics "browse"-endpoint. This will not only show related songs/videos but also suggested playlists, similar artists and more albums from the creator. But this will still require sending a request to the "next"-endpoint beforehand because this endpoint only accepts browse Ids and only the "next"-endpoint returns such an Id. This means it could be a little bit slow if speed is important.

Btw you can also check all of this out on the actual YouTube Music page when opening any song/video. On the right side there will be the tabs "Up next" & "Related".

Let me know what approach you think would be best. Kind regards.

martinle068 commented 2 months ago

I think similar Songs to a given ID would be fine.

Could you please provide an example how to use it, the "radios", specifically how to fetch songs using the playlist id? I think it is not described in the readme file.

Respectfully, Martin

IcySnex commented 2 months ago

Hello,

Getting similar songs via the Id would require the "next"-endpoint implemented. I will update YouTubeMusicAPI in the next few days to do that.

To fetch the songs of a community playlist (and radio) you will have to call the GetCommunityPlaylistInfoAsync() function. This will return a CommunityPlaylistInfo which also contains an array of CommunityPlaylistSongInfo's. If a song isn't available anymore (which happens quite often on YouTube) the Id of this CommunityPlaylistSongInfo will be null.

Also please note that this function needs the browse Id of the playlist (and not the actual playlist Id). You can obtain the browse Id by first calling GetCommunityPlaylistBrowseId().

You can see this in action in one of my programs: https://github.com/IcySnex/Musify/blob/main/Plugins/Musify.PlatformSupport.YouTube/Internal/YouTubeMusicWrapper.cs#L187

if you have any other question, feel free to ask me! Kind regards.

martinle068 commented 2 months ago

Maybe I got it wrong, is it only possible to fetch songs from community playlist and not those ones that are created for PlaylistID of a radio. When I searched one of the PlaylistIDs on ytmusic, I got one of those random generated playlists from youtube. Would it be possible to fetch these playlists or am I just missing something?

Sincerely, Martin

IcySnex commented 2 months ago

I dont think I quite understand. When you search for one of the "Playlist Ids" on YouTube Music? Those random generated playlists are called radios (,if I understand you correctly) and you can get a radio playlist based on each single song/video.

Could you please explain it a little bit further? Thank you.

martinle068 commented 2 months ago

Hello, sure I can. You are right, I was talking about the radios. When I fetch a Song and get the property Song.Radio.PlaylistId, then put it in the ?list= parameter in the YT music url, it displays the radio for a given song. I thought that it would be possible to fetch the radio using the SearchCommunityPlaylistAsync() method as you mentioned earlier, with the browseId of course, but when searched it says "Not Found". Is this a wrong way of doing it or is it just not possible? Currently I'm using the official YT api to fetch a "radio" for a given PlaylistId, but the content is slightly different from the one from YT music, currently it appears to be from YouTube. Please let me know if there is another way.

Thank you. Martin

IcySnex commented 2 months ago

Thanks for explaining.

Yes now I get the issue. During my testing I have never actually tried to fetch the info of a radio playlist, my bad.

This 404 Not Found error is caused because YouTube Musics "browse"-endpoint used for getting the playlists info doesn't support auto-generated playlists. But this can be worked around using the "next"-endpoint.

I have fixed this in Version 1.3.0. You can now get similar content of any video via the id like this:

SongVideoInfo songVideo = await client.GetSongVideoInfoAsync(videoId);

string playlistBrowseId = client.GetCommunityPlaylistBrowseId(songVideo.Radio.PlaylistId);
CommunityPlaylistInfo playlist = await client.GetCommunityPlaylistInfoAsync(playlistBrowseId);

foreach (CommunityPlaylistSongInfo playlistSong in playlist.Songs)
    Console.WriteLine(playlistSong.Name);

I hope this works as expected now. Thanks for letting me know about this bug. Kind regards.

martinle068 commented 2 months ago

Thank you, I appreciate your quick response, it works great now. I have another question regarding this tho, when reaching the end of the radio, is it possible to fetch more songs or is the count fixed? Currently it is 50 songs.

IcySnex commented 2 months ago

It looks like it's limited to 50 on the original YouTube Music "Next Up" page as well. I haven't seen any continuation tokens in the response either.

But since all songs are similar and from the same genre anyways you could just get the radio for the last song in the playlist and continue your list. But please note that there could be duplicates, which you could ignore or you would have to filter them out. I would only recommend this if you really need more songs.

martinle068 commented 2 months ago

Yes, I had the same idea and already tried it. It looks fine, not many duplicated. I also think that after 50 songs a few duplicates wouldn't matter.

Thank you.