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

Add user.getTopTracks and add Username parameter to artist.getInfo #171

Open
wants to merge 12 commits into
base: next
Choose a base branch
from
9 changes: 4 additions & 5 deletions src/IF.Lastfm.Core/Api/ArtistApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net.Http;
using System.Net.Http;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using System.Threading.Tasks;
Expand All @@ -15,13 +15,12 @@ public ArtistApi(ILastAuth auth, HttpClient httpClient = null)
Auth = auth;
}



public async Task<LastResponse<LastArtist>> GetInfoAsync(string artist, string bioLang = LastFm.DefaultLanguageCode, bool autocorrect = false)
public async Task<LastResponse<LastArtist>> GetInfoAsync(string artist, string username = "", string bioLang = LastFm.DefaultLanguageCode, bool autocorrect = false)
{
var command = new GetInfoCommand(Auth)
{
ArtistName = artist,
Username = username,
BioLanguage = bioLang,
Autocorrect = autocorrect,
HttpClient = HttpClient
Expand Down Expand Up @@ -174,4 +173,4 @@ public async Task<PageResponse<LastArtist>> SearchAsync(string artistname, int p
return await command.ExecuteAsync();
}
}
}
}
13 changes: 10 additions & 3 deletions src/IF.Lastfm.Core/Api/Commands/Artist/GetInfoCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
Expand All @@ -20,6 +20,8 @@ internal class GetInfoCommand : GetAsyncCommandBase<LastResponse<LastArtist>>

public bool Autocorrect { get; set; }

public string Username { get; set; }

public GetInfoCommand(ILastAuth auth) : base(auth) { }

/// <summary>
Expand All @@ -35,12 +37,17 @@ public override void SetParameters()
{
Parameters.Add("artist", ArtistName);
}

if (BioLanguage != null)
{
Parameters.Add("lang", BioLanguage);
}


if (!string.IsNullOrWhiteSpace(Username))
{
Parameters.Add("username", Username);
}

Parameters.Add("autocorrect", Convert.ToInt32(Autocorrect).ToString());

DisableCaching();
Expand Down
51 changes: 51 additions & 0 deletions src/IF.Lastfm.Core/Api/Commands/User/GetTopTracksCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace IF.Lastfm.Core.Api.Commands.User
{
[ApiMethodName("user.getTopTracks")]
internal class GetTopTracksCommand : GetAsyncCommandBase<PageResponse<LastTrack>>
{
public string Username { get; private set; }
public LastStatsTimeSpan Period { get; set; }

public GetTopTracksCommand(ILastAuth auth, string username) : base(auth)
{
Username = username;
}

public override void SetParameters()
{
Parameters.Add("user", Username);
Parameters.Add("period", Period.ToString());

AddPagingParameters();
DisableCaching();
}

public override async Task<PageResponse<LastTrack>> HandleResponse(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();

LastResponseStatus status;
if (LastFm.IsResponseValid(json, out status) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json).SelectToken("toptracks");
var itemsToken = jtoken.SelectToken("track");
var attrToken = jtoken.SelectToken("@attr");

return PageResponse<LastTrack>.CreateSuccessResponse(itemsToken, attrToken, LastTrack.ParseJToken, LastPageResultsType.Attr);
}
else
{
return LastResponse.CreateErrorResponse<PageResponse<LastTrack>>(status);
}
}
}
}
4 changes: 2 additions & 2 deletions src/IF.Lastfm.Core/Api/IArtistApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;

Expand All @@ -8,7 +8,7 @@ public interface IArtistApi
{
ILastAuth Auth { get; }

Task<LastResponse<LastArtist>> GetInfoAsync(string artist, string bioLang = LastFm.DefaultLanguageCode,
Task<LastResponse<LastArtist>> GetInfoAsync(string artist, string username = "", string bioLang = LastFm.DefaultLanguageCode,
bool autocorrect = false);

Task<LastResponse<LastArtist>> GetInfoByMbidAsync(string mbid, string bioLang = LastFm.DefaultLanguageCode,
Expand Down
10 changes: 8 additions & 2 deletions src/IF.Lastfm.Core/Api/IUserApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
Expand All @@ -24,10 +24,16 @@ Task<PageResponse<LastArtist>> GetTopArtists(string username,
int pagenumber = 0,
int count = LastFm.DefaultPageLength);

Task<PageResponse<LastTag>> GetTopTags(string username,
int count = LastFm.DefaultPageLength);

Task<PageResponse<LastTrack>> GetRecentScrobbles(string username, DateTimeOffset? from = null,
DateTimeOffset? to = null, bool extendedResponse = false, int pagenumber = LastFm.DefaultPage,
int count = LastFm.DefaultPageLength);

Task<PageResponse<LastTrack>> GetTopTracks(string username, LastStatsTimeSpan period = LastStatsTimeSpan.Week, int pagenumber = LastFm.DefaultPage,
int count = LastFm.DefaultPageLength);

Task<PageResponse<LastStation>> GetRecentStations(string username,
int pagenumber,
int count = LastFm.DefaultPageLength);
Expand All @@ -50,4 +56,4 @@ Task<PageResponse<LastShout>> GetShoutsAsync(string username,

Task<PageResponse<LastAlbum>> GetWeeklyAlbumChartAsync(string username, double? to = null, double? from = null);
}
}
}
17 changes: 15 additions & 2 deletions src/IF.Lastfm.Core/Api/UserApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Enums;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
using System;
Expand Down Expand Up @@ -54,6 +54,19 @@ public async Task<PageResponse<LastArtist>> GetTopArtists(string username, LastS
return await command.ExecuteAsync();
}

public async Task<PageResponse<LastTrack>> GetTopTracks(string username, LastStatsTimeSpan period = LastStatsTimeSpan.Week, int pageNumber = LastFm.DefaultPage, int count = LastFm.DefaultPageLength)
{
var command = new GetTopTracksCommand(Auth, username)
{
Page = pageNumber,
Count = count,
Period = period,
HttpClient = HttpClient
};

return await command.ExecuteAsync();
}

/// <summary>
/// Gets a list of recent scrobbled tracks for this user in reverse date order.
/// </summary>
Expand Down Expand Up @@ -181,4 +194,4 @@ public async Task<PageResponse<LastAlbum>> GetWeeklyAlbumChartAsync(string usern
return await command.ExecuteAsync();
}
}
}
}
36 changes: 19 additions & 17 deletions src/IF.Lastfm.Core/Objects/LastStats.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;

namespace IF.Lastfm.Core.Objects
{
public class LastStats : ILastfmObject
{
#region Properties
public class LastStats : ILastfmObject
{
#region Properties

public int Listeners { get; set; }
public int Plays { get; set; }
public int Listeners { get; set; }
public int Plays { get; set; }
public int? UserPlayCount { get; set; }

#endregion
#endregion

internal static LastStats ParseJToken(JToken token)
{
var stats = new LastStats
{
Listeners = token.Value<int>("listeners"),
Plays = token.Value<int>("plays")
};
internal static LastStats ParseJToken(JToken token)
{
var stats = new LastStats
{
Listeners = token.Value<int>("listeners"),
Plays = token.Value<int>("playcount"),
UserPlayCount = token.Value<int>("userplaycount")
};

return stats;
}
}
return stats;
}
}
}