/
RemotelessController.cs
403 lines (349 loc) · 13.2 KB
/
RemotelessController.cs
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace Remoteless
{
/// <summary>
/// Class to hold information about a playlist
/// </summary>
public class Playlist
{
public string Name { get; set; }
public string SpotifyURL { get; set; }
public List<Song> Songs { get; set; }
}
/// <summary>
/// Class to hold information about a song in a playlist, artist and track info
/// </summary>
public class Song
{
public SongInfo Album { get; set; }
public SongInfo Artist { get; set; }
public SongInfo Track { get; set; }
}
/// <summary>
/// Class to hold information about a artist or a track
/// </summary>
public class SongInfo
{
public string Name { get; set; }
public string SpotifyURL { get; set; }
}
/// <summary>
/// Class to hold information of Spotify client state
/// </summary>
public class State
{
public bool Alive { get; set; }
public bool Visible { get; set; }
public bool Playing { get; set; }
public bool Activated { get; set; }
public int Volume { get; set; }
public int SongPos { get; set; }
public bool CovertArt { get; set; }
public int CovertID { get; set; }
public string Artist { get; set; }
public string Track { get; set; }
}
/// <summary>
/// Main class to hold functionality for Remoteless
/// </summary>
public class Controller
{
/// <summary>
/// URL to the HTTP interface for Remoteless
/// </summary>
private static string URL = "http://localhost:14387";
/// <summary>
/// Function to get the result from a HTTP GET from example Remoteless HTTP interface
/// </summary>
/// <param name="url">URL to where to get information</param>
/// <returns>String with data from example Remoteless HTTP interface</returns>
private static string GetSite(string url)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.UTF8.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
return sb.ToString();
}
/// <summary>
/// Function to get a list of playlists and the playlists songs
/// </summary>
/// <returns>List of playlists</returns>
public static List<Playlist> GetPlaylistWithSongs()
{
return GetPlaylistWithSongs(0);
}
/// <summary>
/// Function to get a list of playlists and the playlists songs
/// </summary>
/// <param name="from">Number the list should start from</param>
/// <returns>List of playlists</returns>
public static List<Playlist> GetPlaylistWithSongs(int startFrom)
{
string data = GetSite(URL + "/playlists?from=" + startFrom);
string[] separator = { "\n" };
string[] splitedData = data.Split(separator, StringSplitOptions.RemoveEmptyEntries);
string[] separator2 = { "|" };
string[] separator3 = { "=" };
List<Playlist> playlists = new List<Playlist>();
Playlist lastPlaylist = new Playlist();
Song lastSongInfo = new Song();
int from = 0;
int to = 0;
int remaining = 0;
for (int i = 0; i < splitedData.Length; i++)
{
if (splitedData[i].Contains("from") || splitedData[i].Contains("to") || splitedData[i].Contains("remaining"))
{
string[] rowData = splitedData[i].Split(separator3, StringSplitOptions.RemoveEmptyEntries);
switch (rowData[0].ToLower())
{
case "from":
from = Convert.ToInt32(rowData[1]);
break;
case "to":
to = Convert.ToInt32(rowData[1]);
break;
case "remaining":
remaining = Convert.ToInt32(rowData[1]);
break;
default:
break;
}
}
else
{
string[] rowData = splitedData[i].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
switch (rowData[0].ToLower())
{
case "playlist":
if ((lastPlaylist.Name != string.Empty) && (lastPlaylist.Songs != null) && (lastPlaylist.Songs.Count > 0))
{
playlists.Add(lastPlaylist);
}
lastPlaylist = new Playlist();
lastPlaylist.SpotifyURL = rowData[2];
lastPlaylist.Name = rowData[3];
lastPlaylist.Songs = new List<Song>();
break;
case "track":
lastSongInfo = new Song();
lastSongInfo.Track = new SongInfo()
{
SpotifyURL = rowData[2],
Name = rowData[3]
};
break;
case "artist":
lastSongInfo.Artist = new SongInfo()
{
SpotifyURL = rowData[2],
Name = rowData[3]
};
lastPlaylist.Songs.Add(lastSongInfo);
break;
case "from":
from = Convert.ToInt32(rowData[1]);
break;
case "to":
to = Convert.ToInt32(rowData[1]);
break;
case "remaining":
remaining = Convert.ToInt32(rowData[1]);
break;
default:
break;
}
}
}
if ((remaining > 0) && (to != from))
{
playlists.AddRange(GetPlaylistWithSongs(to));
}
return playlists;
}
/// <summary>
/// Get a state object
/// </summary>
/// <returns>State object with information about the client</returns>
public static State GetState()
{
string data = GetSite(URL + "/state");
State state = new State();
string[] separator = { "\n" };
string[] separator2 = { "=" };
string[] splitedData = data.Split(separator, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < splitedData.Length; i++)
{
string[] splitedRowData = splitedData[i].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
if (splitedRowData.Length > 0)
{
switch (splitedRowData[0])
{
case "alive":
state.Alive = Convert.ToBoolean(splitedRowData[1]);
break;
case "visible":
state.Visible = Convert.ToBoolean(splitedRowData[1]);
break;
case "playing":
state.Playing = Convert.ToBoolean(splitedRowData[1]);
break;
case "activated":
state.Activated = Convert.ToBoolean(splitedRowData[1]);
break;
case "volume":
state.Volume = Convert.ToInt32(splitedRowData[1]);
break;
case "songpos":
state.SongPos = Convert.ToInt32(splitedRowData[1]);
break;
case "coverart":
state.CovertArt = Convert.ToBoolean(splitedRowData[1]);
break;
case "artist":
state.Artist = splitedRowData[1];
break;
case "track":
state.Track = splitedRowData[1];
break;
}
}
}
return state;
}
public static List<Song> Search(string searchValue)
{
List<Song> songList = new List<Song>();
Song lastSong = null;
string data = GetSite(URL + "/search?cc=NO&query=" + searchValue);
string[] separator = { "\n" };
string[] splitedData = data.Split(separator, StringSplitOptions.RemoveEmptyEntries);
string[] separator2 = { "|" };
for (int i = 0; i < splitedData.Length; i++)
{
string[] splitedDataRow = splitedData[i].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
if (splitedDataRow[1] == "0")
{
if (lastSong != null)
{
songList.Add(lastSong);
}
lastSong = new Song();
}
switch (splitedDataRow[0])
{
case "artist":
lastSong.Artist = new SongInfo()
{
Name = splitedDataRow[3],
SpotifyURL = splitedDataRow[2]
};
break;
case "track":
lastSong.Track = new SongInfo()
{
Name = splitedDataRow[3],
SpotifyURL = splitedDataRow[2]
};
break;
case "album":
lastSong.Album = new SongInfo()
{
Name = splitedDataRow[3],
SpotifyURL = splitedDataRow[2]
};
break;
}
}
return songList;
}
/// <summary>
/// Start Spotify
/// </summary>
public static void Start()
{
string data = GetSite(URL + "/action/start");
}
/// <summary>
/// Play previous track
/// </summary>
public static void Prev()
{
string data = GetSite(URL + "/action/prev");
}
/// <summary>
/// Play or pause the track
/// </summary>
public static void PlayPause()
{
string data = GetSite(URL + "/action/playpause");
}
/// <summary>
/// Play next track
/// </summary>
public static void Next()
{
string data = GetSite(URL + "/action/next");
}
/// <summary>
/// Put volume up
/// </summary>
public static void VolUp()
{
string data = GetSite(URL + "/action/volup");
}
/// <summary>
/// Put volume down
/// </summary>
public static void VolDown()
{
string data = GetSite(URL + "/action/voldown");
}
/// <summary>
/// Set volume
/// </summary>
/// <param name="vol">% of volume to set it to</param>
public static void VolSet(int vol)
{
string data = GetSite(URL + "/action/volset?vol=" + vol);
}
/// <summary>
/// Play a playlist, artist or track
/// </summary>
/// <param name="url"></param>
public static void Play(string url)
{
string data = GetSite(URL + "/action/play?uri=spotify:" + url);
}
}
}