Skip to content

Commit

Permalink
Use Spotify for artist images due to inflatablefriends/lastfm#166
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkit committed Jul 2, 2020
1 parent 67d83eb commit 1ae7160
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/generator/ApiConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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; }
}
Expand Down
11 changes: 6 additions & 5 deletions src/generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private static async Task MainAsync(string[] args)
if (args.Length != 2)
{
Console.Error.WriteLine("Usage: generator.exe <config.json> <output.json>");
return;
return;
}

var configPath = Path.GetFullPath(args.ElementAtOrDefault(0));
Expand All @@ -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)
Expand Down
36 changes: 25 additions & 11 deletions src/generator/Tiles/LastfmTileBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<Tile> 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<TileContent>();
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
{
Expand Down
1 change: 1 addition & 0 deletions src/generator/generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Markdown" Version="2.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Octokit" Version="0.48.0" />
<PackageReference Include="SpotifyAPI.Web" Version="6.0.0-beta.7" />
</ItemGroup>

</Project>

0 comments on commit 1ae7160

Please sign in to comment.