From 8567853b996622f05996788a68b4f6862a34e146 Mon Sep 17 00:00:00 2001 From: David Rudie Date: Sun, 4 Sep 2016 02:49:31 -0500 Subject: [PATCH] Spotify artwork download changes 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. --- Snip/Players/Spotify.cs | 91 +++++++++++++++-------------------------- Snip/Snip.Designer.cs | 14 +++---- Snip/Snip.cs | 16 ++++---- 3 files changed, 47 insertions(+), 74 deletions(-) diff --git a/Snip/Players/Spotify.cs b/Snip/Players/Spotify.cs index 198d928..2b0b59c 100644 --- a/Snip/Players/Spotify.cs +++ b/Snip/Players/Spotify.cs @@ -92,7 +92,7 @@ public override void Update() if (Globals.SaveAlbumArtwork) { - this.HandleSpotifyAlbumArtwork(jsonSummary[mostPopular].name.ToString()); + this.DownloadSpotifyAlbumArtwork(jsonSummary[mostPopular].album); } } else @@ -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); @@ -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) { diff --git a/Snip/Snip.Designer.cs b/Snip/Snip.Designer.cs index b68183a..519e6a0 100644 --- a/Snip/Snip.Designer.cs +++ b/Snip/Snip.Designer.cs @@ -18,7 +18,7 @@ public partial class Snip private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemSaveAlbumArtwork; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemKeepSpotifyAlbumArtwork; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemTiny; - private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemSmall; + // private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemSmall; // Disabling the small size since no small size is provided via Spotify private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemMedium; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemLarge; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemSaveHistory; @@ -78,7 +78,7 @@ private void InitializeComponent() this.toolStripMenuItemSaveAlbumArtwork = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemKeepSpotifyAlbumArtwork = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemTiny = new System.Windows.Forms.ToolStripMenuItem(); - this.toolStripMenuItemSmall = new System.Windows.Forms.ToolStripMenuItem(); + // this.toolStripMenuItemSmall = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemMedium = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemLarge = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemSaveHistory = new System.Windows.Forms.ToolStripMenuItem(); @@ -201,7 +201,7 @@ private void InitializeComponent() // this.toolStripMenuItemKeepSpotifyAlbumArtwork.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemTiny, - this.toolStripMenuItemSmall, + // this.toolStripMenuItemSmall, this.toolStripMenuItemMedium, this.toolStripMenuItemLarge}); this.toolStripMenuItemKeepSpotifyAlbumArtwork.Name = "toolStripMenuItemKeepSpotifyAlbumArtwork"; @@ -220,10 +220,10 @@ private void InitializeComponent() // // toolStripMenuItemSmall // - this.toolStripMenuItemSmall.Name = "toolStripMenuItemSmall"; - this.toolStripMenuItemSmall.Size = new System.Drawing.Size(67, 22); - this.toolStripMenuItemSmall.Text = Globals.ResourceManager.GetString("ImageResolutionSmall"); - this.toolStripMenuItemSmall.Click += new System.EventHandler(this.AlbumArtworkResolutionCheck); + // this.toolStripMenuItemSmall.Name = "toolStripMenuItemSmall"; + // this.toolStripMenuItemSmall.Size = new System.Drawing.Size(67, 22); + // this.toolStripMenuItemSmall.Text = Globals.ResourceManager.GetString("ImageResolutionSmall"); + // this.toolStripMenuItemSmall.Click += new System.EventHandler(this.AlbumArtworkResolutionCheck); // // toolStripMenuItemMedium // diff --git a/Snip/Snip.cs b/Snip/Snip.cs index 3e84cf9..e35fbe9 100644 --- a/Snip/Snip.cs +++ b/Snip/Snip.cs @@ -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(); @@ -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; @@ -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; @@ -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; @@ -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;