Get artist info command

This commit is contained in:
Rikki Tooley 2013-07-01 23:18:06 +01:00
parent 0b44ed584e
commit 1cb3974f20
10 changed files with 417 additions and 2 deletions

View File

@ -0,0 +1,98 @@
using System;
using System.Threading.Tasks;
using IF.Lastfm.Core.Api.Commands.ArtistApi;
using IF.Lastfm.Core.Api.Helpers;
using IF.Lastfm.Core.Objects;
namespace IF.Lastfm.Core.Api
{
public class ArtistApi : IArtistApi
{
public IAuth Auth { get; private set; }
public ArtistApi(Auth auth)
{
Auth = auth;
}
#region artist.getInfo
public async Task<LastResponse<Artist>> GetArtistInfoAsync(string artist,
string bioLang = LastFm.DefaultLanguageCode,
bool autocorrect = false)
{
var command = new GetArtistInfoCommand(Auth, artist)
{
BioLanguage = bioLang,
Autocorrect = autocorrect
};
return await command.ExecuteAsync();
}
public async Task<LastResponse<Artist>> GetArtistInfoWithMbidAsync(string mbid,
string bioLang = LastFm.DefaultLanguageCode,
bool autocorrect = false)
{
throw new NotImplementedException();
}
#endregion
#region artist.getTopAlbums
public async Task<PageResponse<Album>> GetTopAlbumsForArtistAsync(string artist,
bool autocorrect = false,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength)
{
throw new NotImplementedException();
}
public async Task<PageResponse<Album>> GetTopAlbumsForArtistWithMbidAsync(string mbid,
bool autocorrect = false,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength)
{
throw new NotImplementedException();
}
#endregion
#region artist.getTags
public async Task<PageResponse<Tag>> GetUserTagsForArtistAsync(string artist,
string username,
bool autocorrect = false,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength)
{
throw new NotImplementedException();
}
public async Task<PageResponse<Tag>> GetUserTagsForArtistWithMbidAsync(string mbid,
string username,
bool autocorrect = false,
int page = 1,
int itemsPerPage = LastFm.DefaultPageLength)
{
throw new NotImplementedException();
}
#endregion
#region artist.getTopTags
public async Task<PageResponse<Tag>> GetTopTagsForArtistAsync(string artist, bool autocorrect = false)
{
throw new NotImplementedException();
}
public async Task<PageResponse<Tag>> GetTopTagsForArtistWithMbidAsync(string mbid, bool autocorrect = false)
{
throw new NotImplementedException();
}
#endregion
}
}

View File

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
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.ArtistApi
{
internal class GetArtistInfoCommand : GetAsyncCommandBase<LastResponse<Artist>>
{
public string ArtistName { get; set; }
public string BioLanguage { get; set; }
public bool Autocorrect { get; set; }
public GetArtistInfoCommand(IAuth auth, string artistname)
: base(auth)
{
Method = "artist.getInfo";
ArtistName = artistname;
}
public async override Task<LastResponse<Artist>> ExecuteAsync()
{
var parameters = new Dictionary<string, string>
{
{"artist", ArtistName},
{"autocorrect", Convert.ToInt32(Autocorrect).ToString()}
};
var apiUrl = LastFm.FormatApiUrl(Method, Auth.ApiKey, parameters);
Url = new Uri(apiUrl, UriKind.Absolute);
return await ExecuteInternal();
}
public async override Task<LastResponse<Artist>> HandleResponse(HttpResponseMessage response)
{
string json = await response.Content.ReadAsStringAsync();
LastFmApiError error;
if (LastFm.IsResponseValid(json, out error) && response.IsSuccessStatusCode)
{
var jtoken = JsonConvert.DeserializeObject<JToken>(json);
var artist = Artist.ParseJToken(jtoken.SelectToken("artist"));
return LastResponse<Artist>.CreateSuccessResponse(artist);
}
else
{
return LastResponse<Artist>.CreateErrorResponse(error);
}
}
}
}

View File

@ -40,7 +40,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Api\AlbumApi.cs" />
<Compile Include="Api\ArtistApi.cs" />
<Compile Include="Api\Commands\AlbumApi\GetAbumInfoCommand.cs" />
<Compile Include="Api\Commands\ArtistApi\GetArtistInfoCommand.cs" />
<Compile Include="Api\Commands\GetAsyncCommandBase.cs" />
<Compile Include="Api\Commands\UserApi\GetRecentScrobblesCommand.cs" />
<Compile Include="Api\Commands\UserApi\GetTopAlbumsCommand.cs" />
@ -107,7 +109,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Api\Commands\ArtistApi\" />
<Folder Include="Api\Commands\TrackApi\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

View File

@ -1,9 +1,54 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using IF.Lastfm.Core.Api.Helpers;
using Newtonsoft.Json.Linq;
namespace IF.Lastfm.Core.Objects
{
/// <summary>
/// Todo bio, tour, similar, stats, streamable
/// </summary>
public class Artist
{
#region Properties
public string Name { get; set; }
public string Mbid { get; set; }
public Uri Url { get; set; }
public bool OnTour { get; set; }
public IEnumerable<Tag> Tags { get; set; }
public LastImageCollection Images { get; set; }
#endregion
internal static Artist ParseJToken(JToken token)
{
var a = new Artist();
a.Name = token.Value<string>("name");
a.Mbid = token.Value<string>("mbid");
a.Url = new Uri(token.Value<string>("url"), UriKind.Absolute);
a.OnTour = Convert.ToBoolean(token.Value<int>("ontour"));
var tagsToken = token.SelectToken("tags");
if (tagsToken != null)
{
a.Tags = tagsToken.SelectToken("tag").Children().Select(Tag.ParseJToken);
}
var images = token.SelectToken("image");
if (images != null)
{
var imageCollection = LastImageCollection.ParseJToken(images);
a.Images = imageCollection;
}
return a;
}
internal static string GetNameFromJToken(JToken artistToken)
{
var name = artistToken.Value<string>("name");

View File

@ -102,6 +102,9 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\ArtistApi\GetArtistInfo.xaml.cs">
<DependentUpon>GetArtistInfo.xaml</DependentUpon>
</Compile>
<Compile Include="Pages\UserApi\RecentStations.xaml.cs">
<DependentUpon>RecentStations.xaml</DependentUpon>
</Compile>
@ -112,6 +115,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>AppResources.resx</DependentUpon>
</Compile>
<Compile Include="ViewModels\ArtistApi\GetArtistInfoViewModel.cs" />
<Compile Include="ViewModels\BaseViewModel.cs" />
<Compile Include="Pages\UserApi\History.xaml.cs">
<DependentUpon>History.xaml</DependentUpon>
@ -137,6 +141,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Pages\ArtistApi\GetArtistInfo.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Pages\UserApi\RecentStations.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>

View File

@ -39,6 +39,7 @@
<Button Content="Scrobbling" Click="OnScrobblingLinkClick"/>
<Button Content="History" Click="OnHistoryLinkClick"/>
<Button Content="Recent Stations" Click="OnRecentStationsLinkClick"/>
<Button Content="Get Artist Info" Click="OnGetArtistInfoLinkClick"/>
</StackPanel>
</Grid>

View File

@ -30,5 +30,10 @@ private void OnRecentStationsLinkClick(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Pages/UserApi/RecentStations.xaml", UriKind.Relative));
}
private void OnGetArtistInfoLinkClick(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Pages/ArtistApi/GetArtistInfo.xaml", UriKind.Relative));
}
}
}

View File

@ -0,0 +1,48 @@
<phone:PhoneApplicationPage
x:Class="IF.Lastfm.Demo.Apollo.Pages.ArtistApi.GetArtistInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:behaviors="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:artistApi="clr-namespace:IF.Lastfm.Demo.Apollo.ViewModels.ArtistApi"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True"
d:DataContext="{d:DesignInstance artistApi:GetArtistInfoViewModel, IsDesignTimeCreatable=True}">
<i:Interaction.Behaviors>
<behaviors:MultiApplicationBarBehavior x:Name="MultiApplicationBar">
<behaviors:ApplicationBar>
<behaviors:ApplicationBarIconButton Click="OnDoneClick"
IconUri="/Toolkit.Content/ApplicationBar.Check.png"
Text="done" />
</behaviors:ApplicationBar>
</behaviors:MultiApplicationBarBehavior>
</i:Interaction.Behaviors>
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,17,12,0">
<TextBlock Text="LASTFM-WP DEMO APP" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="artist info" Margin="9,-7,0,12" Style="{StaticResource PhoneTextTitle1Style}"/>
<toolkit:PhoneTextBox Hint="Artist" Text="{Binding ArtistName, Mode=TwoWay}"/>
<toolkit:PhoneTextBox Hint="Track" Text="{Binding Track, Mode=TwoWay}"/>
<toolkit:PhoneTextBox Hint="Album" Text="{Binding Album, Mode=TwoWay}"/>
<toolkit:PhoneTextBox Hint="Album artist" Text="{Binding AlbumArtist, Mode=TwoWay}"/>
</StackPanel>
</Grid>
</phone:PhoneApplicationPage>

View File

@ -0,0 +1,55 @@
using System;
using System.ComponentModel;
using IF.Lastfm.Demo.Apollo.TestPages.ViewModels;
using IF.Lastfm.Demo.Apollo.ViewModels.ArtistApi;
using IF.Lastfm.Demo.Apollo.ViewModels.TrackApi;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace IF.Lastfm.Demo.Apollo.Pages.ArtistApi
{
public partial class GetArtistInfo : PhoneApplicationPage
{
private GetArtistInfoViewModel _viewModel;
public GetArtistInfo()
{
_viewModel = new GetArtistInfoViewModel();
DataContext = _viewModel;
InitializeComponent();
MultiApplicationBar.SelectedIndex = 0;
_viewModel.PropertyChanged += OnViewModelPropertyChanged;
}
private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "InProgress")
{
if (_viewModel.InProgress)
{
SystemTray.ProgressIndicator = new ProgressIndicator
{
IsVisible = _viewModel.InProgress,
IsIndeterminate = _viewModel.InProgress
};
}
else
{
SystemTray.ProgressIndicator = null;
}
}
}
private void OnDoneClick(object sender, EventArgs e)
{
if (!_viewModel.InProgress)
{
_viewModel.GetInfo().AsAsyncAction();
}
}
}
}

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cimbalino.Phone.Toolkit.Services;
using IF.Lastfm.Core.Api;
using IF.Lastfm.Core.Objects;
using IF.Lastfm.Demo.Apollo.TestPages.ViewModels;
namespace IF.Lastfm.Demo.Apollo.ViewModels.ArtistApi
{
public class GetArtistInfoViewModel : BaseViewModel
{
private string _artistName;
private Artist _artist;
private bool _inProgress;
public string ArtistName
{
get { return _artistName; }
set
{
if (value == _artistName)
{
return;
}
_artistName = value;
OnPropertyChanged();
}
}
public Artist Artist
{
get { return _artist; }
set
{
if (Equals(value, _artist))
{
return;
}
_artist = value;
OnPropertyChanged();
}
}
public bool InProgress
{
get { return _inProgress; }
set
{
if (value.Equals(_inProgress))
{
return;
}
_inProgress = value;
OnPropertyChanged();
}
}
public async Task GetInfo()
{
InProgress = true;
var appsettings = new ApplicationSettingsService();
var apikey = appsettings.Get<string>("apikey");
var apisecret = appsettings.Get<string>("apisecret");
var username = appsettings.Get<string>("username");
var pass = appsettings.Get<string>("pass");
var auth = new Auth(apikey, apisecret);
var response = await auth.GetSessionTokenAsync(username, pass);
if (response.Success && auth.HasAuthenticated)
{
var artistApi = new Core.Api.ArtistApi(auth);
var artist = await artistApi.GetArtistInfoAsync(ArtistName);
if (artist.Success)
{
Artist = artist.Content;
}
}
InProgress = false;
}
}
}