-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
416 lines (362 loc) · 17.3 KB
/
Program.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
404
405
406
407
408
409
410
411
412
413
414
415
416
using System.Configuration;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using Swan.Logging;
using static SpotifyAPI.Web.Scopes;
namespace SpotifyPlayerControl
{
public class Program
{
private static readonly string? clientId = ConfigurationManager.AppSettings["ClientID"];
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static EmbedIOAuthServer _server;
private static string CredentialsPath { get; set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static void Exiting() => Console.CursorVisible = true;
public static async Task<int> Main(string[] args)
{
#if DEBUG
//
// Testing things out...
//
//args = new[] { "!start" };
//args = new[] { "!pause" };
//args = new[] { "!skip" };
//args = new[] { "!prev" };
//args = new[] { "!play", "https://open.spotify.com/track/14BIjb7JQJzUPBE7PxLV25?si=69f799ac9b154e82" };
//args = new[] { "!play", "https%3A%2F%2Fopen.spotify.com%2Ftrack%2F1huvTbEYtgltjQRXzrNKGi" };
//args = new[] { "!play", "Peace Orchestra - Double Drums" };
//args = new[] { "!play", "Peace+Orchestra+-+Double+Drums" };
//args = new[] { "!play", "Where is my mind?" };
//args = new[] { "!song" };
//args = new[] { "!next" };
//args = new[] { "!recent" };
//args = new[] { "!shuffle" }; //restriction violated... no idea what that means, doh!
//args = new[] { "!repeat" }; //restriction violated... no idea what that means, doh!
//args = new[] { "!mute" };
//args = new[] { "!vol", "-50" };
//args = new[] { "!playlist" };
#endif
// initialize path
CredentialsPath = Path.Combine(System.AppContext.BaseDirectory, "credentials.json");
// Disable logging output from web server
// https://github.com/unosquare/embedio/wiki/Cookbook#logging-turn-off-or-customize
//Logger.UnregisterLogger<ConsoleLogger>();
var command = "";
var payload = "";
if (args.Length > 0)
{
command = args[0].ToLower();
}
if (args.Length > 1)
{
payload = args[1];
}
// This is a bug in the SWAN Logging library, need this hack to bring back the cursor
AppDomain.CurrentDomain.ProcessExit += (sender, e) => Exiting();
if (File.Exists(CredentialsPath))
{
await Start(command, payload);
}
else
{
_server = new EmbedIOAuthServer(new Uri("http://localhost:5000/spotifyCallback"), 5000);
await StartAuthentication(command, payload);
}
await Task.Delay(-1);
return 0;
}
private static async Task Start(string command, string payload)
{
var json = await File.ReadAllTextAsync(CredentialsPath);
var token = JsonConvert.DeserializeObject<PKCETokenResponse>(json);
var authenticator = new PKCEAuthenticator(clientId!, token!);
authenticator.TokenRefreshed += (sender, token) => File.WriteAllText(CredentialsPath, JsonConvert.SerializeObject(token));
var config = SpotifyClientConfig.CreateDefault().WithAuthenticator(authenticator);
var spotifyClient = new SpotifyClient(config);
// Get the current user profile
var userProfile = await spotifyClient.UserProfile.Current();
// Check if we have an active player or not
var currentPlayback = await spotifyClient.Player.GetCurrentPlayback();
if (currentPlayback != null && currentPlayback.Device.IsActive)
switch (command)
{
case "!start":
case "!resume":
await spotifyClient.Player.ResumePlayback();
break;
case "!pause":
await spotifyClient.Player.PausePlayback();
break;
case "!skip":
await spotifyClient.Player.SkipNext();
break;
case "!prev":
case "!back":
await spotifyClient.Player.SkipPrevious();
break;
case "!play":
case "!queue":
await AddTrackToQueue(spotifyClient, payload);
break;
case "!song":
case "!music":
await GetCurrentTrackInfo(spotifyClient);
break;
case "!next":
await GetNextTrackInfo(spotifyClient);
break;
case "!recent":
await GetRecentlyPlayed(spotifyClient);
break;
case "!shuffle":
await ToggleShuffle(spotifyClient, currentPlayback);
break;
case "!repeat":
await SwitchRepeat(spotifyClient, currentPlayback);
break;
case "!mute":
await ToggleMute(spotifyClient, currentPlayback);
break;
case "!vol":
case "!volume":
await ChangeVolume(spotifyClient, currentPlayback, payload);
break;
case "!playlist":
await GetCurrentPlaylist(spotifyClient, currentPlayback);
break;
default:
GreetUser(userProfile);
break;
}
else if (command != "") NoActivePlayer(userProfile);
else GreetUser(userProfile);
Environment.Exit(0);
}
private static async Task StartAuthentication(string command, string payload)
{
var (verifier, challenge) = PKCEUtil.GenerateCodes();
await _server.Start();
_server.AuthorizationCodeReceived += async (sender, response) =>
{
await _server.Stop();
PKCETokenResponse token = await new OAuthClient().RequestToken(
new PKCETokenRequest(clientId!, response.Code, _server.BaseUri, verifier)
);
_server.Dispose();
await File.WriteAllTextAsync(CredentialsPath, JsonConvert.SerializeObject(token));
await Start(command, payload);
};
var request = new LoginRequest(_server.BaseUri, clientId!, LoginRequest.ResponseType.Code)
{
CodeChallenge = challenge,
CodeChallengeMethod = "S256",
Scope = new List<string> { UserReadPlaybackState, UserModifyPlaybackState, UserReadCurrentlyPlaying, UserReadPlaybackPosition, UserReadRecentlyPlayed }
};
Uri uri = request.ToUri();
try
{
BrowserUtil.Open(uri);
}
catch (Exception)
{
$"Unable to open URL, manually open: {uri}".Warn();
}
}
private static void GreetUser(PrivateUser userProfile)
{
ConsoleColor currentForeground = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\nMoin {userProfile.DisplayName},\nyour Spotify account with ID {userProfile.Id} is authenticated!");
Console.ForegroundColor = currentForeground;
}
private static void NoActivePlayer(PrivateUser userProfile)
{
ConsoleColor currentForeground = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"\n{userProfile.DisplayName}, no active player found!");
Console.ForegroundColor = currentForeground;
Console.WriteLine("Please play a Spotify song on your client before triggering an action from the CLI and then try again.");
}
private static async Task AddTrackToQueue(SpotifyClient spotifyClient, string songRequest)
{
var sb = new StringBuilder();
songRequest = HttpUtility.UrlDecode(songRequest);
// Spotify URL given with track ID
if (songRequest.StartsWith("https://open.spotify.com/track/"))
{
var uri = new Uri(songRequest);
var trackId = uri.Segments[2];
sb.Append("spotify:track:");
sb.Append(trackId);
var requestedTrack = await spotifyClient.Tracks.Get(trackId);
if (requestedTrack != null && requestedTrack.Artists.Count > 0)
{
var request = new PlayerAddToQueueRequest(sb.ToString());
await spotifyClient.Player.AddToQueue(request);
Console.Write($"'{requestedTrack.Artists[0].Name} - {requestedTrack.Name}'");
}
else
{
Console.Write("404 - Track not found!");
}
}
// Need to search for the song and hope the best
else
{
// Input should be given in the format: "artist name - song title"
var requestParts = songRequest.Split('-');
if (requestParts.Length == 1)
{
sb.Append(songRequest);
}
else if (requestParts.Length == 2)
{
sb.Append("artist:");
sb.Append(requestParts[0].Trim());
sb.Append(" track:");
sb.Append(requestParts[1].Trim());
}
var searchRequest = new SearchRequest(SearchRequest.Types.Track, sb.ToString());
var searchResponse = await spotifyClient.Search.Item(searchRequest);
if (searchResponse.Tracks.Items != null && searchResponse.Tracks.Items.Count > 0)
{
var request = new PlayerAddToQueueRequest(searchResponse.Tracks.Items[0].Uri);
await spotifyClient.Player.AddToQueue(request);
Console.Write($"'{searchResponse.Tracks.Items[0].Artists[0].Name} - {searchResponse.Tracks.Items[0].Name}'");
}
else
{
Console.Write("404 - Track not found!");
}
}
// Unable to remove or reorder things in queue!
// https://community.spotify.com/t5/Spotify-for-Developers/API-Delete-Remove-songs-from-queue/td-p/4956378
// await spotify.Player.SkipNext(); <- not working when more than one item were added to queue...
}
private static async Task GetCurrentTrackInfo(SpotifyClient spotifyClient)
{
var currentlyPlayingRequest = new PlayerCurrentlyPlayingRequest(PlayerCurrentlyPlayingRequest.AdditionalTypes.Track);
var currentlyPlaying = await spotifyClient.Player.GetCurrentlyPlaying(currentlyPlayingRequest);
if (currentlyPlaying != null && currentlyPlaying.IsPlaying)
{
var fullTrack = ((FullTrack)currentlyPlaying.Item);
Console.Write($"'{fullTrack.Artists[0].Name} - {fullTrack.Name}' -> {fullTrack.ExternalUrls["spotify"]}");
}
}
private static async Task GetNextTrackInfo(SpotifyClient spotifyClient)
{
var queueResponse = await spotifyClient.Player.GetQueue();
if (queueResponse != null && queueResponse.Queue.Count > 0)
{
var fullTrack = ((FullTrack)queueResponse.Queue[0]);
Console.Write($"'{fullTrack.Artists[0].Name} - {fullTrack.Name}' -> {fullTrack.ExternalUrls["spotify"]}");
}
}
private static async Task GetRecentlyPlayed(SpotifyClient spotifyClient)
{
var recentlyPlayedItems = await spotifyClient.Player.GetRecentlyPlayed();
if (recentlyPlayedItems == null || recentlyPlayedItems.Items.Count == 0) return;
var fullTrack = recentlyPlayedItems.Items[0].Track;
Console.Write($"'{fullTrack.Artists[0].Name} - {fullTrack.Name}' -> {fullTrack.ExternalUrls["spotify"]}");
}
private static async Task ToggleShuffle(SpotifyClient spotifyClient, CurrentlyPlayingContext currentPlayback)
{
var playerShuffleRequest = new PlayerShuffleRequest(!currentPlayback.ShuffleState);
await spotifyClient.Player.SetShuffle(playerShuffleRequest);
}
private static async Task SwitchRepeat(SpotifyClient spotifyClient, CurrentlyPlayingContext currentPlayback)
{
var currentRepeatState = currentPlayback.RepeatState;
var targetRepeatState = PlayerSetRepeatRequest.State.Off;
switch (currentRepeatState)
{
case "off":
targetRepeatState = PlayerSetRepeatRequest.State.Track;
break;
case "track":
targetRepeatState = PlayerSetRepeatRequest.State.Context;
break;
}
var playerRepeatRequest = new PlayerSetRepeatRequest(targetRepeatState);
await spotifyClient.Player.SetRepeat(playerRepeatRequest);
}
private static async Task ToggleMute(SpotifyClient spotifyClient, CurrentlyPlayingContext currentPlayback)
{
var currentVolume = currentPlayback.Device.VolumePercent;
if (currentVolume.HasValue && currentVolume.Value > 0)
{
await File.WriteAllTextAsync(Path.Combine(System.AppContext.BaseDirectory, "currentVolume.txt"), currentVolume.Value.ToString());
await spotifyClient.Player.SetVolume(new PlayerVolumeRequest(0));
Console.Write("0");
}
if (!currentVolume.HasValue || currentVolume.Value == 0)
{
var volume = await File.ReadAllTextAsync(Path.Combine(System.AppContext.BaseDirectory, "currentVolume.txt"));
await spotifyClient.Player.SetVolume(new PlayerVolumeRequest(int.Parse(volume)));
Console.Write(volume);
}
}
private static async Task ChangeVolume(SpotifyClient spotifyClient, CurrentlyPlayingContext currentPlayback, string payload)
{
var currentVolume = currentPlayback.Device.VolumePercent;
// null guard
if (currentVolume == null) return;
// no int guard
int volumeRequest;
if (!int.TryParse(payload, out volumeRequest)) return;
// already muted guard
if ((!currentVolume.HasValue || currentVolume.Value == 0) && volumeRequest < 1)
{
Console.Write("0");
return;
}
// already max volume guard
if ((currentVolume.HasValue && currentVolume.Value == 100) && volumeRequest > 0)
{
Console.Write("100");
return;
}
var newVolume = currentVolume.Value + volumeRequest;
// set max
if (newVolume > 99)
{
await spotifyClient.Player.SetVolume(new PlayerVolumeRequest(100));
Console.Write("100");
return;
}
// set mute
if (newVolume < 1)
{
await spotifyClient.Player.SetVolume(new PlayerVolumeRequest(0));
Console.Write("0");
return;
}
// change volume
await spotifyClient.Player.SetVolume(new PlayerVolumeRequest(newVolume));
Console.Write(newVolume);
}
private static async Task GetCurrentPlaylist(SpotifyClient spotifyClient, CurrentlyPlayingContext currentPlayback)
{
if (currentPlayback.Context == null || currentPlayback.Context.Type != "playlist")
{
Console.Write("404 - No playlist in use!");
return;
}
var playListId = currentPlayback.Context.Uri.Replace("spotify:playlist:", "");
var currentPlaylist = await spotifyClient.Playlists.Get(playListId);
if (currentPlaylist == null)
{
Console.Write("404 - Playlist not found!");
return;
}
// Bug in SpotifyAPi.Web library? Property "Public" is always false...
//string urlString = !currentPlaylist.Public.Value ? "(private)" : $" -> {currentPlaylist.ExternalUrls["spotify"]}";
string urlString = $" -> {currentPlaylist.ExternalUrls["spotify"]}";
Console.Write($"'{currentPlaylist.Name.Trim()}' by {currentPlaylist.Owner.DisplayName.Trim()} {urlString}");
}
}
}