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.Linq;
using System.Text; using System.Text;
using System.Net; using System.Net;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace SpotifyAPIv1 namespace SpotifyAPIv1
@ -16,8 +18,7 @@ namespace SpotifyAPIv1
public String host = "127.0.0.1"; public String host = "127.0.0.1";
WebClient wc; ExtendedWebClient wc;
SpotifyMusicHandler mh;
internal static RemoteHandler GetInstance() internal static RemoteHandler GetInstance()
{ {
return instance; return instance;
@ -26,7 +27,8 @@ namespace SpotifyAPIv1
internal RemoteHandler() internal RemoteHandler()
{ {
wc = new WebClient(); wc = new ExtendedWebClient();
wc.Timeout = 2000;
wc.Proxy = null; wc.Proxy = null;
wc.Headers.Add("Origin", "https://embed.spotify.com"); wc.Headers.Add("Origin", "https://embed.spotify.com");
wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt"); 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); string a = query("simplecsrf/token.json", false, false, -1);
a = a.Replace(@"\", ""); a = a.Replace(@"\", "");
List<CFID> d = (List<CFID>)JsonConvert.DeserializeObject(a, typeof(List<CFID>)); List<CFID> d = (List<CFID>)JsonConvert.DeserializeObject(a, typeof(List<CFID>));
if (d == null)
return "";
if (d.Count != 1) if (d.Count != 1)
throw new Exception("CFID couldn't be loaded"); throw new Exception("CFID couldn't be loaded");
if (d[0].error != null) if (d[0].error != null)
return ""; return "";
return d[0].token; 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(); string parameters = "?&ref=&cors=&_=" + GetTimestamp();
if (request.Contains("?")) if (request.Contains("?"))
@ -114,13 +118,13 @@ namespace SpotifyAPIv1
string response = ""; string response = "";
try try
{ {
//Need to find a better solution... //Need to find a better solution
if(SpotifyAPI.IsSpotifyRunning()) if (SpotifyAPI.IsSpotifyRunning())
response = "[ " + wc.DownloadString(a) + " ]"; response = "[ " + wc.DownloadString(a) + " ]";
} }
catch (Exception z) catch (Exception z)
{ {
throw; return "";
} }
return response; return response;
} }

View File

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

View File

@ -61,8 +61,16 @@ namespace SpotifyAPIv1
return; return;
} }
StatusResponse new_response = mh.GetStatusResponse(); StatusResponse new_response = mh.GetStatusResponse();
if (!new_response.running && new_response.track == null) if(new_response == null)
{
timer.Start();
return; return;
}
if (!new_response.running && new_response.track == null)
{
timer.Start();
return;
}
if (new_response.track != null && response.track != null) if (new_response.track != null && response.track != null)
{ {
if (new_response.track.GetTrackName() != response.track.GetTrackName() && OnTrackChange != 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.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using SpotifyAPIv1; using SpotifyAPIv1;
using System.Threading;
using SpotifyEventHandler = SpotifyAPIv1.SpotifyEventHandler; using SpotifyEventHandler = SpotifyAPIv1.SpotifyEventHandler;
@ -22,11 +23,37 @@ namespace SpotifyAPI_Example
{ {
InitializeComponent(); InitializeComponent();
spotify = new SpotifyAPI(); spotify = new SpotifyAPI();
if(!SpotifyAPI.IsSpotifyRunning()) if (!SpotifyAPI.IsSpotifyRunning())
{
spotify.RunSpotify(); spotify.RunSpotify();
Thread.Sleep(4000);
}
if (!SpotifyAPI.IsSpotifyWebHelperRunning()) if (!SpotifyAPI.IsSpotifyWebHelperRunning())
{
spotify.RunSpotifyWebHelper(); 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(); mh = spotify.GetMusicHandler();
eh = spotify.GetEventHandler(); eh = spotify.GetEventHandler();
} }