Quantcast
Channel: toastify Discussions Rss Feed
Viewing all articles
Browse latest Browse all 224

New Post: Tutorial - Grabbing Album Art directly from Spotify

$
0
0
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

Viewing all articles
Browse latest Browse all 224

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>