-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
263 lines (252 loc) · 9.1 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Flow.Launcher.Plugin.Cider2
{
/// <inheritdoc />
public class Cider2 : IPlugin
{
private const string ApiBase = @"http://localhost:10767/api/v1/playback";
/// <summary>
/// Temporary icon path generated from the artwork
/// </summary>
private const string TmpIcon = "Images/tmp.png";
private const string CiderIcon = "Images/icon.png";
private PluginInitContext _context;
/// <inheritdoc />
public void Init(PluginInitContext context)
{
_context = context;
}
/// <inheritdoc />
public List<Result> Query(Query query)
{
try
{
// if (!GetMethod("active"))
if (!IsCiderRunning())
{
return new List<Result>
{
new()
{
Title = "Cider2 is not active",
SubTitle = "Please start Cider2",
IcoPath = CiderIcon
}
};
}
}
catch (Exception)
{
return new List<Result>
{
new()
{
Title = "Cider2 is not active",
SubTitle = "Please start Cider2",
IcoPath = CiderIcon
}
};
}
var playback = GetPlayback();
_context.API.LogDebug("Cider2", "Got playback info");
_context.API.LogWarn("Cider2", "Got playback info");
var res = new List<Result>();
if (playback == null)
{
res.Add(new Result
{
Title = "No music is playing",
SubTitle = "Please start playing music",
IcoPath = CiderIcon
});
return res;
}
res.Add(new Result
{
// TODO: Use the artwork as the icon
SubTitle = $"{playback.ArtistName} - {playback.Name}",
Title = "Play/Pause",
IcoPath = TmpIcon,
Action = c => PostMethod("playpause")
});
res.Add(new Result
{
Title = "Next Track",
SubTitle = "Next track",
IcoPath = CiderIcon,
Action = c => PostMethod("next")
});
res.Add(new Result
{
Title = "Previous",
SubTitle = "Previous track",
IcoPath = CiderIcon,
Action = c => PostMethod("previous")
});
if (!playback.InLibrary)
{
res.Add(new Result
{
Title = "Add",
SubTitle = "Add to library",
IcoPath = CiderIcon,
Action = c => PostMethod("add-to-library")
});
}
if (!playback.InFavourite)
{
res.Add(new Result
{
Title = "Favourite",
SubTitle = "Add to favourites",
IcoPath = CiderIcon,
Action = c => ToggleFavourite(1)
});
res.Add(new Result
{
Title = "Dislike",
SubTitle = "Mark as less suggested",
IcoPath = CiderIcon,
Action = c => ToggleFavourite(-1)
});
}
else
{
res.Add(new Result
{
Title = "Remove",
SubTitle = "Remove from favourites",
IcoPath = CiderIcon,
Action = c => ToggleFavourite(0)
});
}
if (!playback.RepeatMode)
{
res.Add(new Result
{
Title = "Repeat mode",
SubTitle = "Toggle repeat mode",
IcoPath = CiderIcon,
Action = c => PostMethod("toggle-repeat")
});
}
if (!playback.ShuffleMode)
{
res.Add(new Result
{
Title = "Shuffle mode",
SubTitle = "Toggle shuffle mode",
IcoPath = CiderIcon,
Action = c => PostMethod("toggle-shuffle")
});
}
return res;
}
private static bool IsCiderRunning()
{
Process[] processes = Process.GetProcessesByName("Cider");
return processes.Length > 0;
}
// private static bool GetMethod(string method)
// {
// using HttpClient client = new();
// var response = client.GetAsync($"{ApiBase}/{method}").Result;
// return response.IsSuccessStatusCode;
// }
private static bool PostMethod(string method)
{
using HttpClient client = new();
var response = client.PostAsync($"{ApiBase}/{method}", null).Result;
return response.IsSuccessStatusCode;
}
/// <param name="ratingValue"><br/>
/// 1 for favourite <br/>
/// 0 for not favourite <br/>
/// -1 for less suggested
/// </param>
/// <returns></returns>
private static bool ToggleFavourite(int ratingValue)
{
using HttpClient client = new();
var ratingData = new { rating = ratingValue };
var json = JsonSerializer.Serialize(ratingData);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = client.PostAsync($"{ApiBase}/set-rating", content).Result;
return response.IsSuccessStatusCode;
}
// private static async Task<(bool repeatMode, bool ShuffleMode)> GetModesAsync()
// {
// using HttpClient client = new();
// var repeatTask = client.GetAsync($"{ApiBase}/repeat-mode");
// var shuffleTask = client.GetAsync($"{ApiBase}/shuffle-mode");
// var results = await Task.WhenAll(repeatTask, shuffleTask);
// return (results[0].IsSuccessStatusCode, results[1].IsSuccessStatusCode);
// }
private Playback GetPlayback()
{
using HttpClient client = new();
var response = client.GetAsync($"{ApiBase}/now-playing").Result;
if (!response.IsSuccessStatusCode) return null;
var content = response.Content.ReadAsStringAsync().Result;
using var doc = JsonDocument.Parse(content);
var root = doc.RootElement;
var info = root.GetProperty("info");
try
{
var artworkUrl = info.GetProperty("artwork").GetProperty("url").GetString();
var artistName = info.GetProperty("artistName").GetString();
var name = info.GetProperty("name").GetString();
var albumName = info.GetProperty("albumName").GetString();
var shuffleMode = info.GetProperty("shuffleMode").GetInt32() == 1;
var repeatMode = info.GetProperty("repeatMode").GetInt32() == 1;
var inLibrary = info.GetProperty("inLibrary").GetBoolean();
var inFavourite = info.GetProperty("inFavorites").GetBoolean();
try
{
using HttpClient imgClient = new();
byte[] imgBytes = imgClient.GetByteArrayAsync(artworkUrl).Result;
File.WriteAllBytes(TmpIcon, imgBytes);
}
catch (Exception e)
{
_context.API.LogWarn("Cider2", e.ToString());
}
return new Playback
{
ArtworkUrl = artworkUrl,
ArtistName = artistName,
Name = name,
AlbumName = albumName,
ShuffleMode = shuffleMode,
RepeatMode = repeatMode,
InLibrary = inLibrary,
InFavourite = inFavourite
};
}
catch (Exception)
{
return null;
}
}
}
/// <summary>
/// This class represents the current playback information
/// </summary>
public class Playback
{
public string ArtworkUrl { get; set; }
public string ArtistName { get; set; }
public string Name { get; set; }
public string AlbumName { get; set; }
public bool ShuffleMode { get; set; }
public bool RepeatMode { get; set; }
public bool InLibrary { get; set; }
public bool InFavourite { get; set; }
}
}