Spotify.NET/SpotifyAPI/RemoteHandler.cs

128 lines
3.8 KiB
C#
Raw Normal View History

2014-01-07 15:26:03 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
namespace SpotifyAPIv1
{
class RemoteHandler
{
private static RemoteHandler instance = new RemoteHandler();
public String oauthKey {get;private set;}
public String cfidKey {get;private set;}
public String host = "127.0.0.1";
WebClient wc;
2014-01-08 22:22:54 +00:00
MusicHandler mh;
internal static RemoteHandler GetInstance()
2014-01-07 15:26:03 +00:00
{
return instance;
}
2014-01-10 07:09:14 +00:00
internal RemoteHandler()
2014-01-07 15:26:03 +00:00
{
2014-01-10 07:09:14 +00:00
2014-01-07 15:26:03 +00:00
wc = new WebClient();
2014-01-10 07:09:14 +00:00
wc.Proxy = null;
2014-01-07 15:26:03 +00:00
wc.Headers.Add("Origin", "https://embed.spotify.com");
wc.Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
}
2014-01-08 22:22:54 +00:00
internal void Init()
2014-01-07 15:26:03 +00:00
{
oauthKey = GetOAuthKey();
cfidKey = GetCFID();
}
2014-01-10 07:09:14 +00:00
internal void SendPauseRequest()
{
recv("remote/pause.json?pause=true", true, true, -1);
}
internal void SetVolumeRequest()
{
Console.WriteLine(recv("remote/info.json", true, true, -1));
}
internal void SendPlayRequest()
{
recv("remote/pause.json?pause=false", true, true, -1);
}
2014-01-08 22:22:54 +00:00
internal StatusResponse Update()
{
String response = recv("remote/status.json", true, true, -1);
if(response == "")
{
return Update();
}
response = response.Replace("\\n", "");
List<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response,typeof(List<StatusResponse>));
return raw[0];
}
2014-01-07 15:26:03 +00:00
private String GetOAuthKey()
{
String raw = "";
2014-01-10 07:09:14 +00:00
using(WebClient wc = new WebClient())
2014-01-07 15:26:03 +00:00
{
2014-01-10 07:09:14 +00:00
wc.Proxy = null;
raw = wc.DownloadString("http://open.spotify.com/token");
2014-01-07 15:26:03 +00:00
}
2014-01-10 07:09:14 +00:00
Dictionary<String, object> lol = (Dictionary<String, object>)JsonConvert.DeserializeObject<Dictionary<String, object>>(raw);
return (String)lol["t"];
2014-01-07 15:26:03 +00:00
}
private String GetCFID()
{
string a = recv("simplecsrf/token.json", false, false, -1);
List<CFID> d = (List<CFID>)JsonConvert.DeserializeObject(a, typeof(List<CFID>));
if (d.Count != 1)
throw new Exception("CFID konnte nicht geladen werden.");
return d[0].token;
}
private string recv(string request, bool oauth, bool cfid, int wait)
{
2014-01-10 07:09:14 +00:00
string parameters = "?&ref=&cors=&_=" + TimeStamp;
2014-01-07 15:26:03 +00:00
if (request.Contains("?"))
{
parameters = parameters.Substring(1);
}
if (oauth)
{
parameters += "&oauth=" + oauthKey;
}
if (cfid)
{
parameters += "&csrf=" + cfidKey;
}
if (wait != -1)
{
parameters += "&returnafter=" + wait;
parameters += "&returnon=login%2Clogout%2Cplay%2Cpause%2Cerror%2Cap";
}
string a = "http://" + host + ":4380/" + request + parameters;
string derp = "";
try
{
derp = wc.DownloadString(a);
derp = "[ " + derp + " ]";
}
catch (Exception z)
{
}
return derp;
}
2014-01-10 07:09:14 +00:00
private int TimeStamp
{
get
{
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
}
}
2014-01-07 15:26:03 +00:00
}
}