private void CheckTitle() { string currentTitle = Spotify.GetCurrentTrack(); if (!string.IsNullOrEmpty(currentTitle) && currentTitle != previousTitle) { string part1, part2; // set the previous title asap so that the next timer call to this function will // fail fast (setting it at the end may cause multiple web requests) previousTitle = currentTitle; if (SplitTitle(currentTitle, out part1, out part2)) { this.Dispatcher.Invoke((Action) delegate { Title1.Text = part2; Title2.Text = part1; }, System.Windows.Threading.DispatcherPriority.Normal); foreach (var p in this.Plugins) { try { p.TrackChanged(part1, part2); } catch (Exception) { //For now we swallow any plugin errors. } } } try { //Use the iTunes API to get the cover art String url = "https://itunes.apple.com/search?term=" + part2 + "&attribute=songTerm&entity=album"; var json = new WebClient().DownloadString(url); iTunesResult deserializedJson = JsonConvert.DeserializeObject <iTunesResult>(json); //Filter tracks by artist List <iTunesTrack> resultsForArtist = getResultsForArtist(deserializedJson, part1); //Get the oldest track (filters out all the "best of" albums, etc.) iTunesTrack oldestTrack = getOldestTitle(resultsForArtist); coverUrl = oldestTrack.artworkUrl100; } catch (Exception) { coverUrl = "SpotifyToastifyLogo.png"; } this.Dispatcher.Invoke((Action) delegate { FadeIn(); }, System.Windows.Threading.DispatcherPriority.Normal); } }
private iTunesTrack getOldestTitle(List <iTunesTrack> resultList) { iTunesTrack oldestTrack = null; DateTime oldestTrackDate = DateTime.Now; foreach (iTunesTrack result in resultList) { DateTime trackDate = Convert.ToDateTime(result.releaseDate); if (DateTime.Compare(trackDate, oldestTrackDate) < 0) { oldestTrackDate = trackDate; oldestTrack = result; } } return(oldestTrack); }