Tutorial - Grabbing Album Art directly from Spotify

May 16, 2014 at 8:37 PM
Edited May 16, 2014 at 8:45 PM
Since i've gotten alot of requests about this, I decided to write a tutorial for the people who don't know how to do this.

This method also works alot better then the default version, and does not require an API key.
Note: Some songs with special characters will not properly load due to XML hating anything else then normal characters, But still, this is rare, you can expect about 99% success (unless you only listen to french songs or something..)

First of all, I will be explaining how this method works.
Once the artist and the song name have been grabbed, these will be parsed into the search of spotify's metadata API, From this XML, the first track will be fetched so we have the corresponding spotify ID. Once we have this spotify ID, we will be able to use another spotify API to grab the info (including the album artwork link).

It might be the .net framework version of your source is to low for some of the functions. If so, upgrade your .net framework version in the project properties to one that supports it.

In Toast.xaml.cs:

Add these imports:


Find this piece of code in the CheckTitle() function:
try
                        {
                            p.TrackChanged(part1, part2);
                        }
                        catch (Exception)
                        {
                            //For now we swallow any plugin errors.
                        }
                    }
                }

                try
                {
Then replace this piece of code
 System.Diagnostics.Debug.WriteLine("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=" + part1 + "&track=" + part2);
                    XPathDocument doc = new XPathDocument("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=" + part1 + "&track=" + part2);

                    XPathNavigator navigator = doc.CreateNavigator();
                    XPathNodeIterator nodeImage = navigator.Select("/lfm/track/album/image[@size='medium']");

                    if (nodeImage.MoveNext())
                    {
                        XPathNavigator node = nodeImage.Current;
                        coverUrl = node.InnerXml;
                    }
                    else
                        coverUrl = "SpotifyToastifyLogo.png";
                }
                catch (Exception)
                {
                    coverUrl = "SpotifyToastifyLogo.png";
                }
With this new code that fetches directly from Spotify:
String URLString = HtmlEncode("http://ws.spotify.com/search/1/track?q=" + System.Web.HttpUtility.UrlEncode(part2) + " - " + System.Web.HttpUtility.UrlEncode(part1));

                    string xmlStr = String.Empty;
                    using (var wc = new WebClient())
                    {
                        try
                        {
                            xmlStr += wc.DownloadString(URLString);
                        }
                        catch (Exception) { } 
                    }

                    var xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(xmlStr);


                    var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsmgr.AddNamespace("spotify", "http://www.spotify.com/ns/music/1");

                    string spotifyurl = xmlDoc.SelectSingleNode("//spotify:track/@href", nsmgr).Value;


                    String embedString = "https://embed.spotify.com/oembed/?url=" + spotifyurl + "&format=xml";

                    string exmlStr = String.Empty;
                    using (var wc = new WebClient())
                    {
                        wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
                        try
                        {
                            exmlStr = wc.DownloadString(embedString);
                        }
                        catch (Exception) { }
                    }
                    var exmlDoc = new XmlDocument();
                    exmlDoc.LoadXml(exmlStr);

                    coverUrl = exmlDoc.SelectSingleNode("//thumbnail_url").InnerXml;

                }
                catch (Exception ex)
                {
                    coverUrl = "SpotifyToastifyLogo.png";
                }
        
And that's about it. If there is a big demand for my windows 8 styled version, which also includes Artist/Title trimming, then i might make another post about it.

Windows 8 Styled Toast:
Image
May 16, 2014 at 8:39 PM
Edited May 16, 2014 at 8:40 PM
Somehow codeplex hates +, So you'd have to replace &#43 with a +...

Edit: Nevermind, it just fixed itself.
May 17, 2014 at 12:32 AM
Edited May 17, 2014 at 12:32 AM
This probably sounds really stupid, but i'm new to this so please forgive me. Where is the Toast.xaml.cs file. Do I need a special version of Toastify and if so how do I install that?
May 17, 2014 at 8:03 AM
You need the source code....
May 17, 2014 at 2:27 PM
I worked that much out, but how do I then use the edited source code with toastify? I apologise as I am new to this.
May 17, 2014 at 2:28 PM
you compile.... You should really have basic programming knowledge before doing this..
May 17, 2014 at 2:31 PM
I'll just post my windows 8 edit here.. replace toastify.exe with this one and reset your settings to default..
http://www.mediafire.com/download/qmk8lokzgg56e8v/Toastify.rar
May 17, 2014 at 2:33 PM
Ok thank you very much
May 21, 2014 at 2:05 AM
Edited May 21, 2014 at 2:06 AM
Hi epicmiro,

Seeing as you already posted your W8 version of Toastify, I simply copied and pasted your uploaded Toastify.exe file into the program's directory. Unfortunately, while the Windows 8 style has been implemented, the album art fetching still doesn't work. I used .NET Reflector to analyze the .exe file and found that the code in the CheckTitle() function was replaced properly, with the code you included in your original post. At this point, I don't know what to do next as it seems to me the album art fetching should be working. Any advice on this?
May 21, 2014 at 7:38 AM
Edited May 21, 2014 at 9:38 AM
I think it recently broke... It still gets the correct URL for the image but the loading seems to mess up.. Trying to figure out whats wrong

EDIT: There is probally something wrong with the loading yeah... If i try to open the url AFTER it tries to load it's an empty page, but if i open it before it tries to load it displays correctly o.o

EDIT2: I am completely clueless now.. It get's the correct URL but.. the cloudfront server seems unstable or something.. Sometimes the image loads in browser.. Sometimes its an empty page, and the other time i get bad gateway errors.
May 22, 2014 at 4:06 PM
It should have fixed itself.. If not, please reply again.
Coordinator
May 30, 2014 at 7:53 PM
This is now checked in. If it becomes unreliable I'll put back the Audio Scrobbler code as an option. Thanks for the code! Feel free to issue pull / join requests in the future :)