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

'artist' search parameter + more album properties on LastTrack #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/IF.Lastfm.Core/Api/Commands/Track/SearchCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ internal class SearchCommand : GetAsyncCommandBase<PageResponse<LastTrack>>
{
public string TrackName { get; set; }

public SearchCommand(ILastAuth auth, string trackName)
public string Artist { get; set; }

public SearchCommand(ILastAuth auth, string trackName, string artist = null)
: base(auth)
{
TrackName = trackName;
Artist = artist;
}

public override void SetParameters()
{
Parameters.Add("track", TrackName);
if (Artist != null) Parameters.Add("artist", Artist);

AddPagingParameters();
DisableCaching();
Expand Down
5 changes: 5 additions & 0 deletions src/IF.Lastfm.Core/Api/ITrackApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Task<PageResponse<LastTrack>> SearchAsync(string trackname,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength);

Task<PageResponse<LastTrack>> SearchAsync(string trackname,
string artist,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength);

//Task<LastResponse> AddShoutAsync(string trackname, string artistname, string message);
}
}
7 changes: 5 additions & 2 deletions src/IF.Lastfm.Core/Api/TrackApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ public async Task<LastResponse> UnloveAsync(string trackname, string artistname)
return await command.ExecuteAsync();
}

public async Task<PageResponse<LastTrack>> SearchAsync(string trackname, int page = 1, int itemsPerPage = LastFm.DefaultPageLength)
public async Task<PageResponse<LastTrack>> SearchAsync(string trackname, int page = 1, int itemsPerPage = LastFm.DefaultPageLength) =>
await SearchAsync(trackname, null, page, itemsPerPage);

public async Task<PageResponse<LastTrack>> SearchAsync(string trackname, string artist, int page = 1, int itemsPerPage = LastFm.DefaultPageLength)
{
var command = new SearchCommand(Auth, trackname)
var command = new SearchCommand(Auth, trackname, artist)
{
Page = page,
Count = itemsPerPage,
Expand Down
12 changes: 12 additions & 0 deletions src/IF.Lastfm.Core/Objects/LastAlbum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,17 @@ internal static string GetNameFromJToken(JToken albumToken)

return name;
}

internal static string GetArtistFromJToken(JToken albumToken) =>
albumToken.Value<string>("artist");

internal static Uri GetUrlFromJToken(JToken albumToken) =>
new Uri(albumToken.Value<string>("url"), UriKind.Absolute);

internal static LastImageSet GetImagesFromJToken(JToken albumToken)
{
var images = albumToken.SelectToken("image");
return images == null ? null : LastImageSet.ParseJToken(images);
}
}
}
16 changes: 16 additions & 0 deletions src/IF.Lastfm.Core/Objects/LastTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class LastTrack : ILastfmObject

public string AlbumName { get; set; }

public string AlbumArtistName { get; set; }

public Uri AlbumUrl { get; set; }

public LastImageSet AlbumImages { get; set; }

public int? ListenerCount { get; set; }

public int? PlayCount { get; set; }
Expand Down Expand Up @@ -82,6 +88,13 @@ internal static LastTrack ParseJToken(JToken token)
t.UserPlayCount = userPlayCount;
}

var listenerCountStr = token.Value<string>("listeners");
int listenerCount;
if (int.TryParse(listenerCountStr, out listenerCount))
{
t.ListenerCount = listenerCount;
}

t.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);

var artistToken = token.SelectToken("artist");
Expand All @@ -108,6 +121,9 @@ internal static LastTrack ParseJToken(JToken token)
if (albumToken != null)
{
t.AlbumName = LastAlbum.GetNameFromJToken(albumToken);
t.AlbumArtistName = LastAlbum.GetArtistFromJToken(albumToken);
t.AlbumUrl = LastAlbum.GetUrlFromJToken(albumToken);
t.AlbumImages = LastAlbum.GetImagesFromJToken(albumToken);
}

var tagsToken = token.SelectToken("toptags");
Expand Down