private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType) { using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout()) { try { // Authorization uses POST instead of GET bool usePostMethodInsteadOfGet = false; string postParameters = string.Empty; // Modify HTTP headers based on what's being contacted switch (spotifyAddressContactType) { case SpotifyAddressContactType.Authorization: usePostMethodInsteadOfGet = true; postParameters = "grant_type=client_credentials"; jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.API: jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); break; default: break; } // Let's be respectful and identify ourself jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); jsonWebClient.Encoding = Encoding.UTF8; string downloadedJson = string.Empty; if (usePostMethodInsteadOfGet) { downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters); } else { downloadedJson = jsonWebClient.DownloadString(jsonAddress); } if (!string.IsNullOrEmpty(downloadedJson)) { return(downloadedJson); } else { return(string.Empty); } } catch (WebException) { return(string.Empty); } } }
private void DownloadSpotifyAlbumArtwork(string albumId) { string artworkDirectory = @Application.StartupPath + @"\SpotifyArtwork"; string artworkImagePath = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}.jpg", artworkDirectory, albumId); if (!Directory.Exists(artworkDirectory)) { Directory.CreateDirectory(artworkDirectory); } FileInfo fileInfo = new FileInfo(artworkImagePath); if (fileInfo.Exists && fileInfo.Length > 0) { fileInfo.CopyTo(this.DefaultArtworkFilePath, true); } else { this.SaveBlankImage(); using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"; var downloadedJson = webClient.DownloadString(string.Format(CultureInfo.InvariantCulture, "https://embed.spotify.com/oembed/?url=spotify:album:{0}", albumId)); if (!string.IsNullOrEmpty(downloadedJson)) { dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson); string imageUrl = jsonSummary.thumbnail_url.ToString().Replace("cover", string.Format(CultureInfo.InvariantCulture, "{0}", (int)Globals.ArtworkResolution)); if (Globals.KeepSpotifyAlbumArtwork) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(new Uri(imageUrl), artworkImagePath, artworkImagePath); } else { webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath); } this.SavedBlankImage = false; } } catch (WebException) { this.SaveBlankImage(); } } } }
private void DownloadSpotifyAlbumArtwork(dynamic jsonSummary) { string albumId = jsonSummary.id; string artworkDirectory = @Application.StartupPath + @"\SpotifyArtwork"; string artworkImagePath = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}.jpg", artworkDirectory, albumId); if (!Directory.Exists(artworkDirectory)) { Directory.CreateDirectory(artworkDirectory); } FileInfo fileInfo = new FileInfo(artworkImagePath); if (fileInfo.Exists && fileInfo.Length > 0) { fileInfo.CopyTo(this.DefaultArtworkFilePath, true); this.SavedBlankImage = false; } else { using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { Uri imageUrl = SelectAlbumArtworkSizeToDownload(jsonSummary); webClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); if (Globals.KeepSpotifyAlbumArtwork) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(imageUrl, artworkImagePath, artworkImagePath); } else { webClient.DownloadFileAsync(imageUrl, this.DefaultArtworkFilePath); } this.SavedBlankImage = false; } catch (WebException) { this.SaveBlankImage(); } } } }
private void SpotifyPlayerControl(SpotifyPlayerControlType controlType) { try { using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { string urlAddress = "https://api.spotify.com/v1/me/player/"; string methodType = string.Empty; switch (controlType) { case SpotifyPlayerControlType.Play: urlAddress += "play"; methodType = "PUT"; break; case SpotifyPlayerControlType.Pause: urlAddress += "pause"; methodType = "PUT"; break; case SpotifyPlayerControlType.NextTrack: urlAddress += "next"; methodType = "POST"; break; case SpotifyPlayerControlType.PreviousTrack: urlAddress += "previous"; methodType = "POST"; break; default: break; } webClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); webClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); webClient.Encoding = Encoding.UTF8; webClient.UploadString(urlAddress, methodType, string.Empty); } } catch { // If you send a request to pause the track, or play the track, when it is already paused or playing // it will send a 403 Forbidden. This will silently ignore the exception. } }
private void DownloadGPMDPAlbumArtwork(string albumArtAddress) { using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { Uri imageUrl = new Uri(albumArtAddress); webClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); webClient.DownloadFileAsync(imageUrl, this.DefaultArtworkFilePath); this.SavedBlankImage = false; } catch (WebException) { this.SaveBlankImage(); } } }
private string DownloadJson(string jsonAddress, SpotifyAddressContactType spotifyAddressContactType) { using (WebClientWithShortTimeout jsonWebClient = new WebClientWithShortTimeout()) { try { // Authorization uses POST instead of GET bool usePostMethodInsteadOfGet = false; string postParameters = string.Empty; // Modify HTTP headers based on what's being contacted switch (spotifyAddressContactType) { case SpotifyAddressContactType.Authorization: usePostMethodInsteadOfGet = true; postParameters = string.Format( CultureInfo.InvariantCulture, "grant_type=authorization_code&code={0}&redirect_uri={1}", this.authorizationCode, this.callbackAddress); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.AuthorizationRefresh: usePostMethodInsteadOfGet = true; postParameters = string.Format( CultureInfo.InvariantCulture, "grant_type=refresh_token&refresh_token={0}", this.refreshToken); jsonWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Basic {0}", ApplicationKeys.Spotify)); break; case SpotifyAddressContactType.API: jsonWebClient.Headers.Add("Authorization", string.Format(CultureInfo.InvariantCulture, "Bearer {0}", this.authorizationToken)); break; default: break; } // Let's be respectful and identify ourself jsonWebClient.Headers.Add("User-Agent", "Snip/" + AssemblyInformation.AssemblyVersion); jsonWebClient.Encoding = Encoding.UTF8; string downloadedJson = string.Empty; if (usePostMethodInsteadOfGet) { downloadedJson = jsonWebClient.UploadString(jsonAddress, "POST", postParameters); } else { downloadedJson = jsonWebClient.DownloadString(jsonAddress); } if (!string.IsNullOrEmpty(downloadedJson)) { return(downloadedJson); } else { return(string.Empty); } } catch (WebException webException) { WebResponse webResponse = webException.Response; WebHeaderCollection webHeaderCollection = webResponse.Headers; for (int i = 0; i < webHeaderCollection.Count; i++) { if (webHeaderCollection.GetKey(i).ToUpperInvariant() == "RETRY-AFTER") { // Set the timer to the retry seconds. Plus 1 for safety. this.updateSpotifyTrackInformation.Enabled = false; this.updateSpotifyTrackInformation.Interval = (Double.Parse(webHeaderCollection.Get(i) + 1, CultureInfo.InvariantCulture)) * 1000; this.updateSpotifyTrackInformation.Enabled = true; } } return(string.Empty); } } }
private void DownloadSpotifyAlbumArtwork(string albumId, int albumArtworkResolution, string savePath = null) { using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { var json = webClient.DownloadString(string.Format(CultureInfo.InvariantCulture, "https://embed.spotify.com/oembed/?url=spotify:album:{0}", albumId)); dynamic jsonSummary = SimpleJson.DeserializeObject(json); string imageUrl = jsonSummary.thumbnail_url.ToString().Replace("cover", string.Format(CultureInfo.InvariantCulture, "{0}", albumArtworkResolution)); if (savePath == null) { webClient.DownloadFileAsync(new Uri(imageUrl), this.defaultArtworkFile); } else { this.SaveBlankImage(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(new Uri(imageUrl), savePath, savePath); } } catch (WebException) { this.SaveBlankImage(); } } }
private void DownloadSpotifyAlbumArtwork(string albumId, int albumArtworkResolution, string savePath = null) { this.SaveBlankImage(); using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"; var json = webClient.DownloadString(string.Format(CultureInfo.InvariantCulture, "https://embed.spotify.com/oembed/?url=spotify:album:{0}", albumId)); if (!string.IsNullOrEmpty(json)) { dynamic jsonSummary = SimpleJson.DeserializeObject(json); string imageUrl = jsonSummary.thumbnail_url.ToString().Replace("cover", string.Format(CultureInfo.InvariantCulture, "{0}", albumArtworkResolution)); this.SaveBlankImage(); if (savePath == null) { webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath); } else { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(new Uri(imageUrl), savePath, savePath); } } } catch (WebException) { this.SaveBlankImage(); } } }
private void DownloadSpotifyAlbumArtwork(dynamic jsonSummary) { string albumId = jsonSummary.id.ToString(); string artworkDirectory = @Application.StartupPath + @"\SpotifyArtwork"; string artworkImagePath = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}.jpg", artworkDirectory, albumId); if (!Directory.Exists(artworkDirectory)) { Directory.CreateDirectory(artworkDirectory); } FileInfo fileInfo = new FileInfo(artworkImagePath); if (fileInfo.Exists && fileInfo.Length > 0) { fileInfo.CopyTo(this.DefaultArtworkFilePath, true); } else { this.SaveBlankImage(); using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { // This assumes that the Spotify image array will always have three results (which in all of my tests it has so far) string imageUrl = string.Empty; switch (Globals.ArtworkResolution) { case Globals.AlbumArtworkResolution.Large: imageUrl = jsonSummary.images[0].url.ToString(); break; case Globals.AlbumArtworkResolution.Medium: imageUrl = jsonSummary.images[1].url.ToString(); break; case Globals.AlbumArtworkResolution.Tiny: imageUrl = jsonSummary.images[2].url.ToString(); break; default: imageUrl = jsonSummary.images[0].url.ToString(); break; } webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"; if (Globals.KeepSpotifyAlbumArtwork) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(new Uri(imageUrl), artworkImagePath, artworkImagePath); } else { webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath); } this.SavedBlankImage = false; } catch (WebException) { this.SaveBlankImage(); } } } }
private void DownloadSpotifyAlbumArtwork(string albumId) { string artworkDirectory = @Application.StartupPath + @"\SpotifyArtwork"; string artworkImagePath = string.Format(CultureInfo.InvariantCulture, @"{0}\{1}.jpg", artworkDirectory, albumId); if (!Directory.Exists(artworkDirectory)) { Directory.CreateDirectory(artworkDirectory); } FileInfo fileInfo = new FileInfo(artworkImagePath); if (fileInfo.Exists && fileInfo.Length > 0) { fileInfo.CopyTo(this.DefaultArtworkFilePath, true); } else { this.SaveBlankImage(); using (WebClientWithShortTimeout webClient = new WebClientWithShortTimeout()) { try { webClient.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"; var downloadedJson = webClient.DownloadString(string.Format(CultureInfo.InvariantCulture, "https://embed.spotify.com/oembed/?url=spotify:album:{0}", albumId)); if (!string.IsNullOrEmpty(downloadedJson)) { dynamic jsonSummary = SimpleJson.DeserializeObject(downloadedJson); string imageUrl = jsonSummary.thumbnail_url.ToString().Replace("cover", string.Format(CultureInfo.InvariantCulture, "{0}", (int)Globals.ArtworkResolution)); if (Globals.KeepSpotifyAlbumArtwork) { webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadSpotifyFileCompleted); webClient.DownloadFileAsync(new Uri(imageUrl), artworkImagePath, artworkImagePath); } else { webClient.DownloadFileAsync(new Uri(imageUrl), this.DefaultArtworkFilePath); } } } catch (WebException) { this.SaveBlankImage(); } } } }