Skip to content

Commit

Permalink
Spotify artwork download changes
Browse files Browse the repository at this point in the history
Previously several different JSON files were downloaded. One for the
track information, and then one for getting the embedded page to
download the artwork from.

The main JSON file has its own image links within it that can be used
which prevents another JSON file from being downloaded (and removes a
whole function).

While the embedded site offered four file sizes the main JSON file only
offers three. Because of this I've removed the "Small" option from
downloading.

The artwork provided will still match whichever result the search came
back with so it may still be inaccurate from what you're actually
playing. However, it may fix #100 and the blank artwork problem.
  • Loading branch information
David Rudie committed Sep 4, 2016
1 parent d87d847 commit 8567853
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 74 deletions.
91 changes: 32 additions & 59 deletions Snip/Players/Spotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override void Update()

if (Globals.SaveAlbumArtwork)
{
this.HandleSpotifyAlbumArtwork(jsonSummary[mostPopular].name.ToString());
this.DownloadSpotifyAlbumArtwork(jsonSummary[mostPopular].album);
}
}
else
Expand Down Expand Up @@ -229,51 +229,10 @@ private static int SelectTrackByPopularity(dynamic jsonSummary)
return keyWithHighestPopularity;
}

// TODO: Re-write this to download the artwork link supplied in the primary JSON file instead of using the old embedded web link.
private void HandleSpotifyAlbumArtwork(string songTitle)
private void DownloadSpotifyAlbumArtwork(dynamic jsonSummary)
{
string albumId = string.Empty;
string albumId = jsonSummary.id.ToString();

try
{
if (!string.IsNullOrEmpty(this.json))
{
dynamic jsonSummary = SimpleJson.DeserializeObject(json);

if (jsonSummary != null)
{
jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString());

foreach (dynamic jsonTrack in jsonSummary)
{
string modifiedTitle = TextHandler.UnifyTitles(songTitle);
string foundTitle = TextHandler.UnifyTitles(jsonTrack.name.ToString());

if (foundTitle == modifiedTitle)
{
dynamic jsonAlbum = SimpleJson.DeserializeObject(jsonTrack["album"].ToString());
albumId = jsonAlbum.uri.ToString();

break;
}
}

if (!string.IsNullOrEmpty(albumId))
{
albumId = albumId.Substring(albumId.LastIndexOf(':') + 1);
this.DownloadSpotifyAlbumArtwork(albumId);
}
}
}
}
catch (FileNotFoundException)
{
this.SaveBlankImage();
}
}

private void DownloadSpotifyAlbumArtwork(string albumId)
{
string artworkDirectory = @Application.StartupPath + @"\SpotifyArtwork";
string artworkImagePath = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}.jpg", artworkDirectory, albumId);

Expand All @@ -296,27 +255,41 @@ private void DownloadSpotifyAlbumArtwork(string albumId)
{
try
{
webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
var downloadedJson = webClient.DownloadString(string.Format(CultureInfo.InvariantCulture, "https://embed.spotify.com/oembed/?url=spotify:album:{0}", albumId));
// This assumes that the Spotify image array will always have three results (which in all of my tests it has so far)
string imageUrl = string.Empty;

if (!string.IsNullOrEmpty(downloadedJson))
switch (Globals.ArtworkResolution)
{
dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson);
case Globals.AlbumArtworkResolution.Large:
imageUrl = jsonSummary.images[0].url.ToString();
break;

string imageUrl = jsonSummary.thumbnail_url.ToString().Replace("cover", string.Format(CultureInfo.InvariantCulture, "{0}", (int)Globals.ArtworkResolution));
case Globals.AlbumArtworkResolution.Medium:
imageUrl = jsonSummary.images[1].url.ToString();
break;

if (Globals.KeepSpotifyAlbumArtwork)
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted);
webClient.DownloadFileAsync(new Uri(imageUrl), artworkImagePath, artworkImagePath);
}
else
{
webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath);
}
case Globals.AlbumArtworkResolution.Tiny:
imageUrl = jsonSummary.images[2].url.ToString();
break;

default:
imageUrl = jsonSummary.images[0].url.ToString();
break;
}

webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";

this.SavedBlankImage = false;
if (Globals.KeepSpotifyAlbumArtwork)
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted);
webClient.DownloadFileAsync(new Uri(imageUrl), artworkImagePath, artworkImagePath);
}
else
{
webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath);
}

this.SavedBlankImage = false;
}
catch (WebException)
{
Expand Down
14 changes: 7 additions & 7 deletions Snip/Snip.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Snip/Snip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ private void AlbumArtworkResolutionCheck(object sender, EventArgs e)
{
this.ToggleArtworkTiny();
}
else if (sender == this.toolStripMenuItemSmall)
{
this.ToggleArtworkSmall();
}
// else if (sender == this.toolStripMenuItemSmall)
// {
// this.ToggleArtworkSmall();
// }
else if (sender == this.toolStripMenuItemMedium)
{
this.ToggleArtworkMedium();
Expand All @@ -467,7 +467,7 @@ private void AlbumArtworkResolutionCheck(object sender, EventArgs e)
private void ToggleArtworkTiny()
{
this.toolStripMenuItemTiny.Checked = true;
this.toolStripMenuItemSmall.Checked = false;
// this.toolStripMenuItemSmall.Checked = false;
this.toolStripMenuItemMedium.Checked = false;
this.toolStripMenuItemLarge.Checked = false;

Expand All @@ -477,7 +477,7 @@ private void ToggleArtworkTiny()
private void ToggleArtworkSmall()
{
this.toolStripMenuItemTiny.Checked = false;
this.toolStripMenuItemSmall.Checked = true;
// this.toolStripMenuItemSmall.Checked = true;
this.toolStripMenuItemMedium.Checked = false;
this.toolStripMenuItemLarge.Checked = false;

Expand All @@ -487,7 +487,7 @@ private void ToggleArtworkSmall()
private void ToggleArtworkMedium()
{
this.toolStripMenuItemTiny.Checked = false;
this.toolStripMenuItemSmall.Checked = false;
// this.toolStripMenuItemSmall.Checked = false;
this.toolStripMenuItemMedium.Checked = true;
this.toolStripMenuItemLarge.Checked = false;

Expand All @@ -497,7 +497,7 @@ private void ToggleArtworkMedium()
private void ToggleArtworkLarge()
{
this.toolStripMenuItemTiny.Checked = false;
this.toolStripMenuItemSmall.Checked = false;
// this.toolStripMenuItemSmall.Checked = false;
this.toolStripMenuItemMedium.Checked = false;
this.toolStripMenuItemLarge.Checked = true;

Expand Down

0 comments on commit 8567853

Please sign in to comment.