diff --git a/SpotifyAPI.dll b/SpotifyAPI.dll index 19fdcf2f..40a7043e 100644 Binary files a/SpotifyAPI.dll and b/SpotifyAPI.dll differ diff --git a/SpotifyAPI/ExtendedWebClient.cs b/SpotifyAPI/ExtendedWebClient.cs new file mode 100644 index 00000000..e7485210 --- /dev/null +++ b/SpotifyAPI/ExtendedWebClient.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; + +namespace SpotifyAPIv1 +{ + class ExtendedWebClient : WebClient + { + public int Timeout { get; set; } + + protected override WebRequest GetWebRequest(Uri address) + { + WebRequest webRequest = base.GetWebRequest(address); + webRequest.Timeout = Timeout; + return webRequest; + } + } +} diff --git a/SpotifyAPI/RemoteHandler.cs b/SpotifyAPI/RemoteHandler.cs index 6eff3477..96f4a044 100644 --- a/SpotifyAPI/RemoteHandler.cs +++ b/SpotifyAPI/RemoteHandler.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; +using System.Threading.Tasks; +using System.Threading; using Newtonsoft.Json; namespace SpotifyAPIv1 @@ -16,8 +18,7 @@ namespace SpotifyAPIv1 public String host = "127.0.0.1"; - WebClient wc; - SpotifyMusicHandler mh; + ExtendedWebClient wc; internal static RemoteHandler GetInstance() { return instance; @@ -25,8 +26,9 @@ namespace SpotifyAPIv1 internal RemoteHandler() { - - wc = new WebClient(); + + wc = new ExtendedWebClient(); + wc.Timeout = 2000; wc.Proxy = null; wc.Headers.Add("Origin", "https://embed.spotify.com"); wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt"); @@ -81,13 +83,15 @@ namespace SpotifyAPIv1 string a = query("simplecsrf/token.json", false, false, -1); a = a.Replace(@"\", ""); List d = (List)JsonConvert.DeserializeObject(a, typeof(List)); + if (d == null) + return ""; if (d.Count != 1) throw new Exception("CFID couldn't be loaded"); if (d[0].error != null) return ""; return d[0].token; } - internal string query(string request, bool oauth, bool cfid, int wait) + internal String query(string request, bool oauth, bool cfid, int wait) { string parameters = "?&ref=&cors=&_=" + GetTimestamp(); if (request.Contains("?")) @@ -114,13 +118,13 @@ namespace SpotifyAPIv1 string response = ""; try { - //Need to find a better solution... - if(SpotifyAPI.IsSpotifyRunning()) + //Need to find a better solution + if (SpotifyAPI.IsSpotifyRunning()) response = "[ " + wc.DownloadString(a) + " ]"; } catch (Exception z) { - throw; + return ""; } return response; } diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index 0f795160..5c3d135e 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -47,6 +47,9 @@ + + Component + diff --git a/SpotifyAPI/SpotifyEventHandler.cs b/SpotifyAPI/SpotifyEventHandler.cs index 3b4cecbb..8f1c84a9 100644 --- a/SpotifyAPI/SpotifyEventHandler.cs +++ b/SpotifyAPI/SpotifyEventHandler.cs @@ -61,8 +61,16 @@ namespace SpotifyAPIv1 return; } StatusResponse new_response = mh.GetStatusResponse(); - if (!new_response.running && new_response.track == null) + if(new_response == null) + { + timer.Start(); return; + } + if (!new_response.running && new_response.track == null) + { + timer.Start(); + return; + } if (new_response.track != null && response.track != null) { if (new_response.track.GetTrackName() != response.track.GetTrackName() && OnTrackChange != null) diff --git a/SpotifyAPI_Example/Form1.cs b/SpotifyAPI_Example/Form1.cs index cb69410e..8c957c4f 100644 --- a/SpotifyAPI_Example/Form1.cs +++ b/SpotifyAPI_Example/Form1.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using SpotifyAPIv1; +using System.Threading; using SpotifyEventHandler = SpotifyAPIv1.SpotifyEventHandler; @@ -22,11 +23,37 @@ namespace SpotifyAPI_Example { InitializeComponent(); spotify = new SpotifyAPI(); - if(!SpotifyAPI.IsSpotifyRunning()) + if (!SpotifyAPI.IsSpotifyRunning()) + { spotify.RunSpotify(); + Thread.Sleep(4000); + } + if (!SpotifyAPI.IsSpotifyWebHelperRunning()) + { spotify.RunSpotifyWebHelper(); - spotify.Connect(); + Thread.Sleep(4000); + } + + if(!spotify.Connect()) + { + Boolean retry = true; + while(retry) + { + if (MessageBox.Show("SpotifyAPI could'nt load!", "Error", MessageBoxButtons.RetryCancel) == System.Windows.Forms.DialogResult.Retry) + { + if(spotify.Connect()) + retry = false; + else + retry = true; + } + else + { + this.Close(); + return; + } + } + } mh = spotify.GetMusicHandler(); eh = spotify.GetEventHandler(); }