public void RunMediaSearch(string artist, string track) { SearchType = string.IsNullOrEmpty(track) ? MediaSearchType.Artist : MediaSearchType.Track; WebClient client = new WebClient(); client.DownloadStringCompleted += (s, e) => { try { if (e.Error == null) { byte[] byteArray = Encoding.UTF8.GetBytes(e.Result); MemoryStream stream = new MemoryStream(byteArray); XDocument document = XDocument.Load(stream); XElement root = document.Root; if (root.Attribute("status").Value == "ok") { CreateEntries(root, SearchType); } } } catch (Exception) { //xml parse exceptions. buried intentionally } finally { InvokeSearchComplete(e); } }; client.DownloadStringAsync(new Uri(String.Format(GetRequestTemplate(SearchType), artist, track))); }
protected string GetRequestTemplate(MediaSearchType type) { switch (type) { case MediaSearchType.Artist: var lang = System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName; return ("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist={0}&lang=" + lang + "&autocorrect=1&api_key=dee2df7c96b013246bba7fe491be1f40"); case MediaSearchType.Track: return ("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&track={1}&artist={0}&autocorrect=1&api_key=dee2df7c96b013246bba7fe491be1f40"); default: return("Search Type not provided"); } }
protected void CreateEntries(XElement root, MediaSearchType searchType) { var XMLroot = (searchType == MediaSearchType.Artist) ? root.Element("artist") : root.Element("track"); if (XMLroot != null) { Entry.Url = GetSafeValue(XMLroot.Element("url")); Entry.ImageUrl = GetImageUrl(XMLroot, ImageSize.Small); Entry.LargeImageUrl = GetImageUrl(XMLroot, ImageSize.Large); Entry.ExtraLargeImageUrl = GetImageUrl(XMLroot, ImageSize.ExtraLarge); if (searchType == MediaSearchType.Artist) { Entry.BandName = GetSafeValue(XMLroot.Element("name")); var bio = XMLroot.Element("bio"); Entry.Summary = StripAllEscapeSymbols(Unescape(StripTagsCharArray(bio.Element("summary").Value))); } } }