-
Notifications
You must be signed in to change notification settings - Fork 7
/
SpotifyPlugin.cs
244 lines (209 loc) · 7.91 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Wox.Plugin.Spotify
{
public class SpotifyPlugin : IPlugin
{
private PluginInitContext _context;
private SpotifyApi _api;
private readonly Dictionary<string, Func<string, List<Result>>> _terms = new Dictionary<string, Func<string, List<Result>>>();
private const string SpotifyIcon = "icon.png";
public void Init(PluginInitContext context)
{
_context = context;
// initialize data, passing it the plugin directory
Task.Run(() => _api = new SpotifyApi(_context.CurrentPluginMetadata.PluginDirectory));
_terms.Add("artist", SearchArtist);
_terms.Add("album", SearchAlbum);
_terms.Add("track", SearchTrack);
_terms.Add("next", PlayNext);
_terms.Add("pause", Pause);
_terms.Add("play", Play);
_terms.Add("mute", ToggleMute);
}
private List<Result> Play(string arg) =>
SingleResult("Play", $"Resume: {_api.CurrentTrack.TrackResource.Name}", _api.Play);
private List<Result> Pause(string arg = null) =>
SingleResult("Pause", $"Pause: {_api.CurrentTrack.TrackResource.Name}", _api.Pause);
private List<Result> PlayNext(string arg) =>
SingleResult("Next", $"Skip: {_api.CurrentTrack.TrackResource.Name}", _api.Skip);
private List<Result> GetPlaying()
{
var t = _api.CurrentTrack;
if (t == null)
{
return SingleResult("No track playing");
}
var status = _api.IsPlaying ? "Now Playing" : "Paused";
var toggleAction = _api.IsPlaying ? "Pause" : "Resume";
var icon = _api.GetArtworkAsync(t);
icon.Wait();
return new List<Result>()
{
new Result()
{
Title = t.TrackResource.Name,
SubTitle = $"{status} | by {t.ArtistResource.Name}",
IcoPath = icon.Result
},
new Result()
{
IcoPath = SpotifyIcon,
Title = "Pause / Resume",
SubTitle = $"{toggleAction}: {t.TrackResource.Name}",
Action = _ =>
{
if (_api.IsPlaying)
_api.Pause();
else
_api.Play();
return true;
}
},
new Result()
{
IcoPath = SpotifyIcon,
Title = "Next",
SubTitle = $"Skip: {t.TrackResource.Name}",
Action = context =>
{
_api.Skip();
return true;
}
},
ToggleMute().First()
};
}
private List<Result> ToggleMute(string arg = null)
{
var toggleAction = _api.IsMuted ? "Unmute" : "Mute";
return SingleResult("Toggle Mute", $"{toggleAction}: {_api.CurrentTrack.TrackResource.Name}", _api.ToggleMute);
}
public List<Result> Query(Query query)
{
if (!_api.IsRunning)
{
return SingleResult("Spotify is not running", "select to open Spotify", () =>
{
_api.RunSpotify();
_context.API.ChangeQuery("");
});
}
else if (!_api.IsConnected)
{
_api.ConnectToSpotify();
}
try
{
// display status if no parameters are added
if (string.IsNullOrWhiteSpace(query.Search))
{
return GetPlaying();
}
if (_terms.ContainsKey(query.FirstSearch))
{
var results = _terms[query.FirstSearch].Invoke(query.SecondToEndSearch);
return results;
}
return SearchTrack(query.Search);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return SingleResult("No results found on Spotify.");
}
private List<Result> SearchTrack(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
}
// Retrieve data and return the first 20 results
var results = _api.GetTracks(param).Select(async x => new Result()
{
Title = x.Name,
SubTitle = "Artist: " + string.Join(", ", x.Artists.Select(a => a.Name)),
IcoPath = await _api.GetArtworkAsync(x),
Action = _ =>
{
_api.Play(x.Uri);
return true;
}
}).ToArray();
Task.WaitAll(results);
return results.Select(x => x.Result).ToList();
}
private List<Result> SearchAlbum(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
}
// Retrieve data and return the first 10 results
var results = _api.GetAlbums(param).Select(async x => new Result()
{
Title = x.Name,
SubTitle = "by " + string.Join(", ", x.Artists.Select(a => a.Name)),
IcoPath = await _api.GetArtworkAsync(x),
Action = _ =>
{
_api.Play(x.Uri);
return true;
}
}).ToArray();
Task.WaitAll(results);
return results.Select(x => x.Result).ToList();
}
private List<Result> SearchArtist(string param)
{
if (!_api.IsWebApiCOnnected) return AuthenticateResult;
if (string.IsNullOrWhiteSpace(param))
{
return new List<Result>();
}
// Retrieve data and return the first 10 results
var results = _api.GetArtists(param).Select(async x => new Result()
{
Title = x.Name,
SubTitle = $"Popularity: {x.Popularity}%",
IcoPath = await _api.GetArtworkAsync(x),
// When selected, open it with the spotify client
Action = _ =>
{
_api.Play(x.Uri);
return true;
}
}).ToArray();
Task.WaitAll(results);
return results.Select(x => x.Result).ToList();
}
private List<Result> AuthenticateResult =>
SingleResult("Authentication required to search the Spotify library", "Click this to authenticate", () =>
{
// This will prompt the user to authenticate
var t = new System.Threading.Thread(async () => await _api.ConnectWebApi());
t.Start();
});
// Returns a list with a single result
private List<Result> SingleResult(string title, string subtitle = "", Action action = default(Action)) =>
new List<Result>()
{
new Result()
{
Title = title,
SubTitle = subtitle,
IcoPath = SpotifyIcon,
Action = _ =>
{
action();
return true;
}
}
};
}
}