-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSpotifyPlugin.cs
660 lines (554 loc) · 24.7 KB
/
SpotifyPlugin.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using Flow.Launcher.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SpotifyAPI.Web;
namespace Flow.Launcher.Plugin.SpotifyPremium
{
public class SpotifyPlugin : IAsyncPlugin
{
private PluginInitContext _context;
private SpotifyPluginClient _client;
private readonly Dictionary<string, Func<string, List<Result>>> _terms = new(StringComparer.InvariantCultureIgnoreCase);
private readonly Dictionary<string, Func<string, Task<List<Result>>>> _expensiveTerms = new(StringComparer.InvariantCultureIgnoreCase);
private const string SpotifyIcon = "icon.png";
private string currentUserId; //Required for playlist querying
private bool optimizeclientUsage = true; //Flag to limit client calls to X ms after a keystroke
//Set to 'false' to stop optimizing client calls
private DateTime lastQueryTime; //Record the time on every query
//Almost every keypress counts as a new query
private string currentQuery;
private const int OptimizeClientKeyDelay = 200; //Time to wait before issuing an expensive query
private int cachedVolume = -1;
private SemaphoreSlim authSemaphore = new SemaphoreSlim(1, 1);
//Wait for delay before querying
//Specify expensive search terms for optimizing client usage
private readonly string[] expensiveSearchTerms =
{
"artist", "album", "track", "playlist", "queue"
};
public Task InitAsync(PluginInitContext context)
{
_context = context;
lastQueryTime = DateTime.UtcNow;
// initialize data, passing it the plugin directory
Task.Run(() => _client = new SpotifyPluginClient(context.API, _context.CurrentPluginMetadata.PluginDirectory));
_expensiveTerms.Add("artist", SearchArtist);
_expensiveTerms.Add("album", SearchAlbum);
_expensiveTerms.Add("track", SearchTrack);
_expensiveTerms.Add("playlist", SearchPlaylist);
_expensiveTerms.Add("device", GetDevices);
_expensiveTerms.Add("queue", QueueSearch);
_expensiveTerms.Add("like", SearchLikeTrack);
_terms.Add("next", PlayNext);
_terms.Add("last", PlayLast);
_terms.Add("pause", Pause);
_terms.Add("play", Play);
_terms.Add("mute", ToggleMute);
_terms.Add("vol", SetVolume);
_terms.Add("volume", SetVolume);
_terms.Add("shuffle", ToggleShuffle);
_terms.Add("repeat", ToggleRepeat);
_terms.Add("unlike", UnlikeCurrentSong);
//view query count and average query duration
_terms.Add("diag", q =>
SingleResultInList(
$"Query Count: {context.CurrentPluginMetadata.QueryCount}",
$"Avg. Query Time: {context.CurrentPluginMetadata.AvgQueryTime}ms",
action: null));
_terms.Add("reconnect", q =>
SingleResultInList(
"Reconnect",
"Force a reconnection and remove the refresh token",
action: ReconnectAction(_client, false)));
return Task.CompletedTask;
}
private List<Result> Play(string arg) =>
SingleResultInList("Play", $"Resume: {_client.CurrentPlaybackName}", action: _client.Play);
private List<Result> Pause(string arg = null) =>
SingleResultInList("Pause", $"Pause: {_client.CurrentPlaybackName}", action: _client.Pause);
private List<Result> PlayNext(string arg) =>
SingleResultInList("Next", $"Skip: {_client.CurrentPlaybackName}", action: _client.Skip);
private List<Result> PlayLast(string arg) =>
SingleResultInList("Last", "Skip Backwards", action: _client.SkipBack);
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
//Save the raw query for requery API call. Refreshes current playing info
currentQuery = query.RawQuery;
if (!_client.RefreshTokenAvailable())
{
return SingleResultInList(
"Require Authentication", "Select to authorize",
action: ReconnectAction(_client),
hideAfterAction: false);
}
if (!_client.ApiConnected)
{
await ReconnectAsync();
}
if (!await _client.CheckTokenValidityAsync())
{
await ReconnectAsync();
}
if (!await _client.UserHasSpotifyPremium())
{
return SingleResultInList(
"Current Spotify account is not premium!",
"Switch to premium account, then select this to use new login",
action: ReconnectAction(_client, false),
hideAfterAction: false);
}
if (token.IsCancellationRequested)
return null;
try
{
List<Result> results;
// display status if no parameters are added
if (string.IsNullOrWhiteSpace(query.Search))
{
return await GetPlaying();
}
//Run the query if it is not an expensive search term
if (_terms.ContainsKey(query.FirstSearch))
{
return _terms[query.FirstSearch].Invoke(query.SecondToEndSearch);
}
//If query is expensive, AND if optimize client Usage is flagged
// return null if query is updated within set number ms
// this limits the client calls made
// if you type a 10 character query quickly enough, only the last keypress searches the Spotify client
if (optimizeclientUsage)
{
await Task.Delay(OptimizeClientKeyDelay, token);
if (token.IsCancellationRequested)
return null;
}
if (_expensiveTerms.ContainsKey(query.FirstSearch))
{
results = await _expensiveTerms[query.FirstSearch].Invoke(query.SecondToEndSearch);
return results;
}
return await SearchAllAsync(query.Search);
}
catch (Exception e)
{
Console.WriteLine(e);
return SingleResultInList(
"There was an error with your request",
e.GetBaseException().Message);
}
}
private async Task<List<Result>> GetPlaying()
{
var d = await _client.GetActiveDeviceNameAsync();
if (d == null)
{
//Must have an active device to control Spotify
return SingleResultInList(
"No active device", "Select device with `sp device`",
action: () =>
{
_context.API.ChangeQuery($"{_context.CurrentPluginMetadata.ActionKeywords[0]} device");
},
hideAfterAction: false);
}
var playbackContext = _client.PlaybackContext;
var item = playbackContext.Item;
var t = item as FullTrack;
var e = item as FullEpisode;
var status = playbackContext.IsPlaying ? "Now Playing" : "Paused";
var toggleAction = playbackContext.IsPlaying ? "Pause" : "Resume";
// Check if item is a track, episode, or default icon if neither work
var icon = t != null ? _client.GetArtworkAsync(t) :
e != null ? _client.GetArtworkAsync(e) :
null;
return new List<Result>()
{
SingleResultInList(
t?.Name ?? e?.Name ?? "Not Available",
$"{status} | by {string.Join(", ", t.Artists.Select(a => String.Join("", a.Name)))}",
icon != null ? icon.Result : SpotifyIcon).First(),
SingleResultInList(
"Pause / Resume",
$"{toggleAction}: {t.Name}",
action: () =>
{
if (playbackContext.IsPlaying)
{
_client.Pause();
}
else
{
_client.Play();
}
},
hideAfterAction: true).First(),
PlayNext(string.Empty).First(),
PlayLast(string.Empty).First(),
ToggleMute().First(),
ToggleShuffle().First(),
ToggleRepeat().First(),
SetVolume().First()
};
}
private List<Result> ToggleMute(string arg = null)
{
var toggleAction = _client.MuteStatus ? "Unmute" : "Mute";
return SingleResultInList("Toggle Mute", $"{toggleAction}: {_client.CurrentPlaybackName}", action: _client.ToggleMute);
}
private List<Result> ToggleRepeat(string arg = null)
{
var currentRepeatStatus = _client.RepeatStatus;
var nextRepeatStatus = _client.GetNextRepeatAction(currentRepeatStatus);
var toggleAction = nextRepeatStatus switch
{
PlayerSetRepeatRequest.State.Off => "Repeat Off",
PlayerSetRepeatRequest.State.Track => "Repeat Current Track",
PlayerSetRepeatRequest.State.Context => "Repeat Current Playlist",
_ => "Unknown repeat status"
};
return SingleResultInList("Toggle Repeat", $"{toggleAction}: {_client.CurrentPlaybackName}", action: _client.ToggleRepeat);
}
private struct SetVolAction {
public enum VolAction {
DISPLAY,
ABSOLUTE,
DECREASE,
INCREASE
}
public VolAction action;
public int target;
public int current;
// validAction returns false if parsing the actionString fails
// to create a valid volume change operation, or if no
// action needs to be taken
public bool validAction;
public SetVolAction(string actionString, int current) {
this.validAction = false;
this.target = -1;
this.current = current;
if (string.IsNullOrWhiteSpace(actionString)) {
this.action = VolAction.DISPLAY;
return;
}
string intString = actionString;
this.action = VolAction.ABSOLUTE;
if (actionString[0] == '+')
{
this.action = VolAction.INCREASE;
intString = actionString.Substring(1);
}
if (actionString[0] == '-')
{
this.action = VolAction.DECREASE;
intString = actionString.Substring(1);
}
if (int.TryParse(intString, out var amt))
{
switch (this.action)
{
case VolAction.ABSOLUTE:
this.target = amt;
break;
case VolAction.INCREASE:
this.target = this.current + amt;
if (this.target > 100) this.target = 100;
break;
case VolAction.DECREASE:
this.target = this.current - amt;
if (this.target < 0) this.target = 0;
break;
}
if (this.target is >= 0 and <= 100) {
this.validAction = true;
return;
}
}
// If there's no valid action to take, fall back to displaying
// the current volume
this.action = VolAction.DISPLAY;
}
}
private List<Result> SetVolume(string arg = null)
{
cachedVolume = _client.CurrentVolume;
SetVolAction volAction = new SetVolAction(arg, cachedVolume);
if (volAction.validAction)
{
return SingleResultInList(
$"Set Volume to {volAction.target}",
$"Current Volume: {cachedVolume}",
action: () =>
{
_client.SetVolume(volAction.target);
});
}
return SingleResultInList($"Volume", $"Current Volume: {cachedVolume}", action: () => { });
}
private List<Result> ToggleShuffle(string arg = null)
{
var toggleAction = _client.ShuffleStatus ? "Off" : "On";
return SingleResultInList("Toggle Shuffle", $"Turn Shuffle {toggleAction}", action: _client.ToggleShuffle);
}
private List<Result> AddLikeCurrentSong(string arg = null)
{
var currentSong = _client.CurrentPlaybackName;
return SingleResultInList("Like", $"Add '{currentSong}' to liked songs", action: _client.AddLikeCurrentSong);
}
private List<Result> UnlikeCurrentSong(string arg = null)
{
var currentSong = _client.CurrentPlaybackName;
return SingleResultInList("Remove", $"Remove '{currentSong}' from liked songs", action: _client.UnlikeCurrentSong);
}
private async Task<List<Result>> SearchAllAsync(string param)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return SingleResultInList("sp {any search term}", "Perform a full search on albums, tracks, artists, and playlists.");
}
// Retrieve data and return the first 20 results
var searchResults = await _client.SearchAll(param);
var results = searchResults.Select(async x => new Result()
{
Title = x.Title,
SubTitle = x.Subtitle,
IcoPath = await _client.GetArtworkAsync(x),
Action = _ =>
{
_client.Play(x.Uri);
return true;
}
}).ToArray();
await Task.WhenAll(results);
return results.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private Task<List<Result>> SearchTrack(string param) => SearchTrack(param, false);
private async Task<List<Result>> SearchTrack(string param, bool shouldQueue = false)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return SingleResultInList("sp track {track name}", "Search for a single Track to play.");
}
// Retrieve data and return the first 20 results
var searchResults = _client.GetTracks(param).Result;
var results = searchResults.Select(async x => new Result()
{
Title = x.Name,
SubTitle = (shouldQueue ? "Queue track by " : "") +
"Artist: " + string.Join(", ", x.Artists.Select(a => a.Name)),
IcoPath = await _client.GetArtworkAsync(x),
Action = _ =>
{
if (shouldQueue)
_client.Enqueue(x.Uri);
else
_client.Play(x.Uri);
return true;
}
}).ToArray();
await Task.WhenAll(results);
return results.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private async Task<List<Result>> SearchAlbum(string param)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return SingleResultInList("sp album {album name}", "Search for an Album to play.");
}
//Get first page of results
var searchResults = _client.GetAlbums(param).Result;
var results = searchResults.Select(async x => new Result()
{
Title = x.Name,
SubTitle = "by " + string.Join(", ", x.Artists.Select(a => a.Name)),
IcoPath = await _client.GetArtworkAsync(x),
Action = _ =>
{
_client.Play(x.Uri);
return true;
}
}).ToArray();
await Task.WhenAll(results);
return searchResults.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private async Task<List<Result>> SearchArtist(string param)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return SingleResultInList("sp artist {artist name}", "Search for an Artist to play.");
}
//Get first page of results
var searchResults = _client.GetArtists(param).Result;
var results = searchResults.Select(async x => new Result()
{
Title = x.Name,
SubTitle = $"Popularity: {x.Popularity}%",
IcoPath = await _client.GetArtworkAsync(x),
// When selected, open it with the spotify client
Action = _ =>
{
_client.Play(x.Uri);
return true;
}
});
await Task.WhenAll(results);
return searchResults.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private async Task<List<Result>> SearchPlaylist(string param)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
param = "";
}
// Retrieve data and return the first 500 playlists
var searchResults = await _client.GetPlaylists(param);
var results = searchResults.Select(async x => new Result()
{
Title = x.Name,
SubTitle = x.Type,
IcoPath = await _client.GetArtworkAsync(x),
Action = _ =>
{
_client.Play(x.Uri);
return true;
}
}).ToArray();
await Task.WhenAll(results);
return searchResults.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private async Task<List<Result>> SearchLikeTrack(string param) {
if (!_client.ApiConnected) return AuthenticateResult;
// Like/Unlike currently playing song if no {track} param is passed
if (string.IsNullOrWhiteSpace(param))
{
var currentSongName = _client.CurrentPlaybackName;
var currentSongId = _client.CurrentPlaybackId;
var currentSongIsLiked = _client.CheckLikedById(currentSongId);
var subtitle = currentSongIsLiked
? $"Remove '{currentSongName}' from liked songs"
: $"Add '{currentSongName}' to liked songs";
return SingleResultInList("Like", subtitle, action: _client.ToggleLikeCurrentSong);
}
// Retrieve data and return the first 20 results
var searchResults = _client.GetTracks(param).Result;
var results = searchResults.Select(async x => new Result()
{
Title = x.Name,
SubTitle = _client.CheckLikedById(x.Id)
? $"Remove '{x.Name}' by {string.Join(", ", x.Artists.Select(a => a.Name))} from liked songs"
: $"Add '{x.Name}' by {string.Join(", ", x.Artists.Select(a => a.Name))} to liked songs",
IcoPath = await _client.GetArtworkAsync(x),
Action = _ =>
{
_client.ToggleLikeById(x.Id);
return true;
}
}).ToArray();
await Task.WhenAll(results);
return results.Any() ? results.Select(x => x.Result).ToList() : NothingFoundResult;
}
private async Task<List<Result>> GetDevices(string param = null)
{
//Retrieve all available devices
var allDevices = await _client.GetDevicesAsync();
if (allDevices.Count == 0)
return SingleResultInList("No devices found on Spotify.", "Reconnect to client", action: ReconnectAction(_client));
var results = allDevices.Where(device => !device.IsRestricted).Select(x => new Result
{
Title = $"{x.Type} {x.Name}",
SubTitle = x.IsActive ? "Active Device" : "Inactive",
//TODO: Add computer and phone icons
//IcoPath = await _client.GetArtworkAsync(x.Images,x.Uri),
IcoPath = SpotifyIcon,
Action = (a) =>
{
_ = _client.SetDevice(x.Id);
return true;
}
}).ToList();
return results.Any() ? results : NothingFoundResult;
}
private async Task ReconnectAsync(bool keepRefreshToken = true)
{
if (authSemaphore.CurrentCount == 0)
{
await authSemaphore.WaitAsync();
authSemaphore.Release();
return;
}
await authSemaphore.WaitAsync();
await _client.ConnectWebClient(keepRefreshToken);
currentUserId = await _client.GetUserIdAsync();
authSemaphore.Release();
}
//Return a generic reconnection action
private Action ReconnectAction(SpotifyPluginClient client, bool keepRefreshToken = true)
{
// ReSharper disable once AsyncVoidLambda
return async () =>
{
//Assign client ID asynchronously when connection finishes
try
{
await ReconnectAsync(keepRefreshToken);
_context.API.ChangeQuery(_context.CurrentPluginMetadata.ActionKeywords[0] + " ", true);
}
catch
{
Console.WriteLine("Failed to write client ID");
}
};
}
private List<Result> AuthenticateResult =>
SingleResultInList(
"Authentication required to search the Spotify library",
"Click this to authenticate",
action: ReconnectAction(_client));
// Returns a SingleResult if no search results are found
private List<Result> NothingFoundResult =>
SingleResultInList("No results found on Spotify.", "Please try refining your search", action: () => { });
// Returns a list with a single result
private List<Result> SingleResultInList(
string title,
string subtitle = "",
string icoPath = SpotifyIcon,
Action action = default,
bool hideAfterAction = true,
bool requery = true)
=>
new List<Result>
{
new ()
{
Title = title,
SubTitle = subtitle,
IcoPath = icoPath,
Action = _ =>
{
action?.Invoke();
if (requery)
RefreshDisplayInfo();
return hideAfterAction;
}
}
};
private async Task<List<Result>> QueueSearch(string param)
{
if (!_client.ApiConnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return SingleResultInList("sp queue {trackname}", "Search for a track to add it to your play queue.");
}
var results = await SearchTrack(param, true);
return results.Any() ? results : NothingFoundResult;
}
private void RefreshDisplayInfo() => _context.API.ChangeQuery(currentQuery, true);
}
}