public static UnifyTitles ( string title ) : string | ||
title | string | |
Результат | string |
// TODO: Re-write this to download the artwork link supplied in the primary JSON file instead of using the old embedded web link. private void HandleSpotifyAlbumArtwork(string songTitle) { string albumId = string.Empty; try { if (!string.IsNullOrEmpty(this.json)) { dynamic jsonSummary = SimpleJson.DeserializeObject(json); if (jsonSummary != null) { jsonSummary = SimpleJson.DeserializeObject(jsonSummary.tracks["items"].ToString()); foreach (dynamic jsonTrack in jsonSummary) { string modifiedTitle = TextHandler.UnifyTitles(songTitle); string foundTitle = TextHandler.UnifyTitles(jsonTrack.name.ToString()); if (foundTitle == modifiedTitle) { dynamic jsonAlbum = SimpleJson.DeserializeObject(jsonTrack["album"].ToString()); albumId = jsonAlbum.uri.ToString(); break; } } if (!string.IsNullOrEmpty(albumId)) { albumId = albumId.Substring(albumId.LastIndexOf(':') + 1); this.DownloadSpotifyAlbumArtwork(albumId); } } } } catch (FileNotFoundException) { this.SaveBlankImage(); } }
private void DownloadJson(string spotifyTitle) { // Prevent redownloading JSON if it's already attempting to if (!this.downloadingJson) { this.downloadingJson = true; using (WebClient jsonWebClient = new WebClient()) { try { // There are certain characters that can cause issues with Spotify's search spotifyTitle = TextHandler.UnifyTitles(spotifyTitle); jsonWebClient.Encoding = System.Text.Encoding.UTF8; jsonWebClient.Headers.Add(string.Format("Authorization: Bearer {0}", this.token)); var downloadedJson = jsonWebClient.DownloadString( string.Format( CultureInfo.InvariantCulture, "https://api.spotify.com/v1/search?q={0}&type=track", HttpUtility.UrlEncode(spotifyTitle))); if (!string.IsNullOrEmpty(downloadedJson)) { this.json = downloadedJson; } } catch (WebException) { this.json = string.Empty; this.SaveBlankImage(); } } this.downloadingJson = false; } }