From 1ae71608cb1c526c7a48ce59459f668667259b44 Mon Sep 17 00:00:00 2001 From: Rikki Tooley Date: Thu, 2 Jul 2020 19:07:46 +0100 Subject: [PATCH] Use Spotify for artist images due to inflatablefriends/lastfm#166 --- src/generator/ApiConfigManager.cs | 9 ++++++ src/generator/Program.cs | 11 ++++---- src/generator/Tiles/LastfmTileBuilder.cs | 36 ++++++++++++++++-------- src/generator/generator.csproj | 1 + 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/generator/ApiConfigManager.cs b/src/generator/ApiConfigManager.cs index d33a8da..9880d82 100644 --- a/src/generator/ApiConfigManager.cs +++ b/src/generator/ApiConfigManager.cs @@ -45,6 +45,12 @@ public class LastfmApiConfig public int Count { get; set; } = 5; } + public class SpotifyApiConfig + { + [JsonProperty("key")] + public string Key { get; set; } + } + public class ApiConfig { [JsonProperty("github")] @@ -53,6 +59,9 @@ public class ApiConfig [JsonProperty("lastfm")] public LastfmApiConfig Lastfm { get; set; } + [JsonProperty("spotify")] + public SpotifyApiConfig Spotify { get; set; } + [JsonProperty("twitter")] public TwitterApiConfig Twitter { get; set; } } diff --git a/src/generator/Program.cs b/src/generator/Program.cs index ba005a5..7ef9320 100644 --- a/src/generator/Program.cs +++ b/src/generator/Program.cs @@ -30,7 +30,7 @@ private static async Task MainAsync(string[] args) if (args.Length != 2) { Console.Error.WriteLine("Usage: generator.exe "); - return; + return; } var configPath = Path.GetFullPath(args.ElementAtOrDefault(0)); @@ -41,14 +41,15 @@ private static async Task MainAsync(string[] args) var httpClient = new HttpClient(); - var tileData = new { + var tileData = new + { github = await new GithubTileBuilder(apiConfig.GitHub).GetTileAsync(), - lastfm = await new LastfmTileBuilder(apiConfig.Lastfm, httpClient).GetTileAsync(), + lastfm = await new LastfmTileBuilder(apiConfig.Lastfm, apiConfig.Spotify, httpClient).GetTileAsync(), twitter = await new TwitterTileBuilder(apiConfig.Twitter, httpClient).GetTileAsync(), }; - await File.WriteAllTextAsync(outputPath, JsonConvert.SerializeObject(tileData)); - Console.WriteLine("...complete"); + await File.WriteAllTextAsync(outputPath, JsonConvert.SerializeObject(tileData)); + Console.WriteLine("...complete"); } private static ApiConfig GetConfigFromFile(string configPath) diff --git a/src/generator/Tiles/LastfmTileBuilder.cs b/src/generator/Tiles/LastfmTileBuilder.cs index c72f45e..d70e542 100644 --- a/src/generator/Tiles/LastfmTileBuilder.cs +++ b/src/generator/Tiles/LastfmTileBuilder.cs @@ -1,34 +1,48 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using IF.Lastfm.Core.Api; +using Newtonsoft.Json; +using SpotifyAPI.Web; +using static SpotifyAPI.Web.SearchRequest; namespace generator.Tiles { public class LastfmTileBuilder : TileBuilder { private readonly LastfmClient _lastfmClient; - private readonly LastfmApiConfig _config; + private readonly LastfmApiConfig _lastfmConfig; + private readonly SpotifyApiConfig _spotifyConfig; + private readonly SpotifyClient _spotifyClient; - public LastfmTileBuilder(LastfmApiConfig config, HttpClient httpClient) + public LastfmTileBuilder(LastfmApiConfig lastfmConfig, SpotifyApiConfig spotifyConfig, HttpClient httpClient) { - _config = config; - _lastfmClient = new LastfmClient(config.Key, config.Secret, httpClient); + _lastfmConfig = lastfmConfig; + _spotifyConfig = spotifyConfig; + _lastfmClient = new LastfmClient(lastfmConfig.Key, lastfmConfig.Secret, httpClient); + _spotifyClient = new SpotifyClient(_spotifyConfig.Key); } protected override async Task BuildAsync() { - var artists = await _lastfmClient.User.GetTopArtists(_config.Username, _config.Period, 1, _config.Count); + var artists = await _lastfmClient.User.GetTopArtists(_lastfmConfig.Username, _lastfmConfig.Period, 1, _lastfmConfig.Count); - var data = artists.Select(a => new TileContent + var data = new List(); + foreach (var artist in artists) { - Name = a.Name, - Overlay = true, - Body = String.Join(",", a.Tags.Select(tag => tag.Name.ToLower())), - }); + var searchResult = await _spotifyClient.Search.Item(new SearchRequest(Types.Artist, artist.Name)); + var spotifyArtist = searchResult.Artists.Items.FirstOrDefault(); + var url = spotifyArtist.Images.OrderByDescending(x => x.Width).FirstOrDefault()?.Url; - Console.WriteLine(String.Join("\n", artists.SelectMany(x => x.MainImage))); + data.Add(new TileContent + { + Name = artist.Name, + Overlay = true, + Image = url != null ? new Uri(url) : null, + }); + } return new Tile { diff --git a/src/generator/generator.csproj b/src/generator/generator.csproj index a5c3a05..5904102 100644 --- a/src/generator/generator.csproj +++ b/src/generator/generator.csproj @@ -11,6 +11,7 @@ +