forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteHandler.cs
More file actions
202 lines (177 loc) · 6.08 KB
/
RemoteHandler.cs
File metadata and controls
202 lines (177 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using Newtonsoft.Json;
namespace SpotifyAPI.SpotifyLocalAPI
{
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";
private class ExtendedWebClientInstance : ExtendedWebClient
{
internal ExtendedWebClientInstance()
{
Timeout = 2000;
Proxy = null;
Headers.Add("Origin", "https://embed.spotify.com");
Headers.Add("Referer", "https://embed.spotify.com/?uri=spotify:track:5Zp4SWOpbuOdnsxLqwgutt");
}
}
internal static RemoteHandler GetInstance()
{
// Is this supposed to return the current instance?
return instance;
}
internal RemoteHandler()
{
}
internal Boolean Init()
{
oauthKey = GetOAuthKey();
cfidKey = GetCFID();
if (cfidKey == "")
return false;
return true;
}
internal async void SendPauseRequest()
{
await QueryAsync("remote/pause.json?pause=true", true, true, -1);
}
internal async void SendPlayRequest()
{
await QueryAsync("remote/pause.json?pause=false", true, true, -1);
}
internal async void SendPlayRequest(String url)
{
await QueryAsync("remote/play.json?uri=" + url, true, true, -1);
}
internal async void SendQueueRequest(String url)
{
await QueryAsync("remote/play.json?uri=" + url + "?action=queue", true, true, -1);
}
internal StatusResponse Update()
{
String response = query("remote/status.json", true, true, -1);
if (response == "")
{
return null;
}
response = response.Replace("\\n", "");
byte[] bytes = Encoding.Default.GetBytes(response);
response = Encoding.UTF8.GetString(bytes);
List<StatusResponse> raw = (List<StatusResponse>)JsonConvert.DeserializeObject(response, typeof(List<StatusResponse>));
return raw[0];
}
internal String GetOAuthKey()
{
String raw = "";
using (WebClient wc = new WebClient())
{
wc.Proxy = null;
raw = wc.DownloadString("http://open.spotify.com/token");
}
Dictionary<String, object> lol = (Dictionary<String, object>)JsonConvert.DeserializeObject<Dictionary<String, object>>(raw);
return (String)lol["t"];
}
internal string GetCFID()
{
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)
{
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
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 response = "";
try
{
//Need to find a better solution
using (var wc = new ExtendedWebClientInstance())
{
if (SpotifyLocalAPIClass.IsSpotifyRunning())
response = "[ " + wc.DownloadString(a) + " ]";
}
}
catch (Exception z)
{
Console.WriteLine(z.Message);
return "";
}
return response;
}
internal async Task<string> QueryAsync(string request, bool oauth, bool cfid, int wait)
{
string parameters = "?&ref=&cors=&_=" + GetTimestamp();
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 response = "";
try
{
//Need to find a better solution
using (var wc = new ExtendedWebClientInstance())
{
if (SpotifyLocalAPIClass.IsSpotifyRunning())
response = "[ " + await wc.DownloadStringTaskAsync(new Uri(a)) + " ]";
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "";
}
return response;
}
internal int GetTimestamp()
{
return Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds);
}
}
}