miceiken

Untitled

Apr 1st, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1.                     var match = Regex.Match(word, @"http:\/\/open.spotify.com\/track\/([\w]+)");
  2.                     if (match.Success)
  3.                     {
  4.                         if (linksPerLine.Contains(word))
  5.                             continue;
  6.  
  7.                         linksPerLine.Add(word);
  8.  
  9.                         var httpRequest = (HttpWebRequest)WebRequest.Create(string.Format("http://ws.spotify.com/lookup/1/?uri={0}", word));
  10.                         httpRequest.Method = "GET";
  11.                         httpRequest.KeepAlive = false;
  12.                         httpRequest.ContentType = "text/xml";
  13.  
  14.                         using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
  15.                         {
  16.                             var enc = System.Text.Encoding.UTF8;
  17.                             using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), enc))
  18.                             {
  19.                                 var response = streamReader.ReadToEnd();
  20.                                 var spotifyXML = new XmlDocument();
  21.                                 spotifyXML.LoadXml(response);
  22.  
  23.                                 var trackRoot = spotifyXML.SelectSingleNode("track");
  24.  
  25.                                 var name = trackRoot.SelectSingleNode("name").InnerText;
  26.                                 var artist = trackRoot.SelectSingleNode("artist").SelectSingleNode("name").InnerText;
  27.                                 var album = trackRoot.SelectSingleNode("album").SelectSingleNode("name").InnerText;
  28.                                 var length = trackRoot.SelectSingleNode("length").InnerText;
  29.                                 var popularity = Math.Round(double.Parse(trackRoot.SelectSingleNode("popularity").InnerText) * 100, 0).ToString() + "%";
  30.  
  31.                                 var ircResponse = string.Format("{0}: {1} - {2} ({3}} | {4} | {5} popular", word, name, artist, album, length, popularity);
  32.                             }
  33.                         }
  34.                     }
Advertisement
Add Comment
Please, Sign In to add comment