inflatablefriends / lastfm

Portable .Net library for Last.fm
Other
100 stars 61 forks source link

How do you use track.getInfo properly in your wrapper #126

Closed veso266 closed 6 years ago

veso266 commented 6 years ago

Hi I was trying your wrapper and it works realy well but now I am trying to get an info about a track name in the lastFM api you would use track.getinfo https://www.last.fm/api/show/track.getInfo

but not sure how would I do that in your wrapper I tried to get an Artist name for title Believe

            var client = new LastfmClient("MY_API_KEY", "MY_API_SECRET");
            var response = await client.Track.SearchAsync("Believe", page: 5, itemsPerPage: 100);

            LastTrack visions = response.Content;
            Console.WriteLine(visions.Name);

but it complains

Error   CS0266  Cannot implicitly convert type 'System.Collections.Generic.IReadOnlyList<IF.Lastfm.Core.Objects.LastTrack>' to 'IF.Lastfm.Core.Objects.LastTrack'. An explicit conversion exists (are you missing a cast?)

Hopefully you can help me a bit with this

Thanks for Anwsering and Best Regards

SHOEGAZEssb commented 6 years ago

Content can be multiple LastTracks, therefore you have to iterate it to get single LastTracks

rikkit commented 6 years ago

Hi @veso266

What you want to do is call the track.getInfo API, like so

LastfmClient client = new LastfmClient("MY_API_KEY", "MY_API_SECRET");
LastResponse<LastTrack> track = await client.Track.GetInfoAsync("Believe", "Cher");

What your code is actually doing is calling the track.search API (I've added the types to make things clear)

LastfmClient client = new LastfmClient("MY_API_KEY", "MY_API_SECRET");
PageResponse<LastTrack> track = await client.Track.SearchAsync("Believe", page: 5, itemsPerPage: 100);

IReadOnlyList<LastTrack> searchResults = response.Content;

Let me know if that helps. Cheers

veso266 commented 6 years ago

Thanks for anwsering almost there

using IF.Lastfm;
using IF.Lastfm.Core;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Json;
using IF.Lastfm.Core.Helpers;
using IF.Lastfm.Core.Objects;
using IF.Lastfm.Core.Scrobblers;

        public static async void lastFM (string title)
        {
            var client = new LastfmClient("MY_API_KEY", "MY_API_SECRET");
            LastResponse<LastTrack> track = await client.Track.GetInfoAsync("Believe", "Cher");

            IReadOnlyList<LastTrack> searchResults = track.Content;
        }
CS0246  The type or namespace name 'LastResponse<LastTrack>' could not be found (are you missing a using directive or an assembly reference?)

not sure why LastResponse doesn't exist

rikkit commented 6 years ago

You also need

using IF.Lastfm.Core.Api.Helpers;

Your code won't compile btw - track.Content won't be IReadOnlyList<LastTrack>, because you're calling the GetInfoAsync which returns a LastResponse. track.Content will be a LastTrack.

veso266 commented 6 years ago

oops forget that I also needed that one (didn't knew existed :smiley: )

            var client = new LastfmClient("apikey" "secret");
            PageResponse<LastTrack> track = await client.Track.SearchAsync("Skyfall", page: 1, itemsPerPage: 10);

            IReadOnlyList<LastTrack> searchResults = track.Content;
            var ArtistName = searchResults[0].ArtistName;

works like it should now, but it is a bit slow (do you know if its possible to make it faster?) Thanks

rikkit commented 6 years ago

Hi @veso266 from the code you've given me I can't immediately see any performance problems. I'd use a tool like Fiddler to check exactly how much time is spent waiting for the Last.fm API vs how long is spent in your code.

PS - you posted your api key and api secret in your comment - you should avoid posting these online.

Ta