Fixxed GetCFID (null-object check) | Implemented ExtendedWebClient with Timeout var | Example App updated | Fixxed Event Timer stopping randomly

This commit is contained in:
Johnny @PC 2014-02-17 12:58:15 +01:00
parent 198e602598
commit 76f3a8716b
6 changed files with 74 additions and 11 deletions

Binary file not shown.

View File

@ -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;
}
}
}

View File

@ -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<CFID> d = (List<CFID>)JsonConvert.DeserializeObject(a, typeof(List<CFID>));
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;
}

View File

@ -47,6 +47,9 @@
<ItemGroup>
<Compile Include="CFID.cs" />
<Compile Include="Enum.cs" />
<Compile Include="ExtendedWebClient.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SpotifyEventHandler.cs" />
<Compile Include="Events.cs" />
<Compile Include="SpotifyMusicHandler.cs" />

View File

@ -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)

View File

@ -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();
}