@sdeck/plugin-spotify 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +106 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1316 -0
- package/dist/index.js.map +12 -0
- package/dist/panels.d.ts +11 -0
- package/dist/panels.d.ts.map +1 -0
- package/package.json +60 -4
- package/src/api.ts +425 -0
- package/src/index.tsx +873 -0
- package/src/panels.tsx +261 -0
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1316 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.tsx
|
|
3
|
+
import {
|
|
4
|
+
defineAction,
|
|
5
|
+
definePlugin,
|
|
6
|
+
defineTrigger,
|
|
7
|
+
Shared,
|
|
8
|
+
useKeyPress,
|
|
9
|
+
useSensorPress
|
|
10
|
+
} from "@sdeck/plugin";
|
|
11
|
+
import {
|
|
12
|
+
iconLayer,
|
|
13
|
+
iconLook,
|
|
14
|
+
textLayer
|
|
15
|
+
} from "@sdeck/schema";
|
|
16
|
+
|
|
17
|
+
// src/api.ts
|
|
18
|
+
import { createHash, randomBytes } from "crypto";
|
|
19
|
+
var SCOPES = [
|
|
20
|
+
"user-read-playback-state",
|
|
21
|
+
"user-modify-playback-state",
|
|
22
|
+
"user-read-currently-playing",
|
|
23
|
+
"user-library-read",
|
|
24
|
+
"user-library-modify",
|
|
25
|
+
"playlist-read-private",
|
|
26
|
+
"playlist-read-collaborative",
|
|
27
|
+
"playlist-modify-public",
|
|
28
|
+
"playlist-modify-private"
|
|
29
|
+
];
|
|
30
|
+
var SPOTIFY = {
|
|
31
|
+
accounts: "https://accounts.spotify.com",
|
|
32
|
+
api: "https://api.spotify.com/v1"
|
|
33
|
+
};
|
|
34
|
+
function createPkce() {
|
|
35
|
+
const verifier = randomBytes(64).toString("base64url");
|
|
36
|
+
const challenge = createHash("sha256").update(verifier).digest("base64url");
|
|
37
|
+
return { verifier, challenge };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class RateLimited extends Error {
|
|
41
|
+
retryAfterMs;
|
|
42
|
+
constructor(retryAfterMs) {
|
|
43
|
+
super(`Spotify asks to slow down for ${Math.ceil(retryAfterMs / 1000)} s`);
|
|
44
|
+
this.retryAfterMs = retryAfterMs;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class SignedOut extends Error {
|
|
49
|
+
constructor(message = "Sign in to Spotify from the plugin's settings") {
|
|
50
|
+
super(message);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
var REASONS = {
|
|
54
|
+
PREMIUM_REQUIRED: "Spotify only lets Premium accounts control playback",
|
|
55
|
+
NO_ACTIVE_DEVICE: "nothing is playing: start Spotify on a device first"
|
|
56
|
+
};
|
|
57
|
+
var page = (message) => new Response(`<!doctype html><meta charset="utf-8"><title>sdeck</title><p>${message}</p>`, {
|
|
58
|
+
headers: { "content-type": "text/html; charset=utf-8" }
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
class SpotifyClient {
|
|
62
|
+
#options;
|
|
63
|
+
#endpoints;
|
|
64
|
+
#fetch;
|
|
65
|
+
#access;
|
|
66
|
+
#refreshing;
|
|
67
|
+
constructor(options) {
|
|
68
|
+
this.#options = options;
|
|
69
|
+
this.#endpoints = options.endpoints ?? SPOTIFY;
|
|
70
|
+
this.#fetch = options.fetch ?? fetch;
|
|
71
|
+
}
|
|
72
|
+
get signedIn() {
|
|
73
|
+
return this.#options.tokens.get() !== undefined;
|
|
74
|
+
}
|
|
75
|
+
signIn(port, timeoutMs = 300000) {
|
|
76
|
+
const clientId = this.#options.clientId();
|
|
77
|
+
if (!clientId)
|
|
78
|
+
throw new Error("set your Spotify client id first");
|
|
79
|
+
const { verifier, challenge } = createPkce();
|
|
80
|
+
const state = randomBytes(16).toString("base64url");
|
|
81
|
+
let finish = () => {};
|
|
82
|
+
const done = new Promise((resolve, reject) => {
|
|
83
|
+
finish = (error) => error ? reject(error) : resolve();
|
|
84
|
+
});
|
|
85
|
+
const server = Bun.serve({
|
|
86
|
+
hostname: "127.0.0.1",
|
|
87
|
+
port,
|
|
88
|
+
fetch: async (request) => {
|
|
89
|
+
const url = new URL(request.url);
|
|
90
|
+
if (url.pathname !== "/callback")
|
|
91
|
+
return new Response("not found", { status: 404 });
|
|
92
|
+
if (url.searchParams.get("state") !== state) {
|
|
93
|
+
return new Response("wrong state", { status: 400 });
|
|
94
|
+
}
|
|
95
|
+
const code = url.searchParams.get("code");
|
|
96
|
+
if (!code) {
|
|
97
|
+
close(new Error(`Spotify said: ${url.searchParams.get("error") ?? "no code"}`));
|
|
98
|
+
return page("sdeck was not given access. You can close this tab.");
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
await this.#token({
|
|
102
|
+
grant_type: "authorization_code",
|
|
103
|
+
code,
|
|
104
|
+
redirect_uri: redirect,
|
|
105
|
+
code_verifier: verifier
|
|
106
|
+
});
|
|
107
|
+
close();
|
|
108
|
+
return page("sdeck is connected to Spotify. You can close this tab.");
|
|
109
|
+
} catch (error) {
|
|
110
|
+
close(error);
|
|
111
|
+
return page("Signing in failed. See the plugin's settings in sdeck.");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
const redirect = `http://127.0.0.1:${server.port}/callback`;
|
|
116
|
+
const timer = setTimeout(() => close(new Error("signing in timed out")), timeoutMs);
|
|
117
|
+
let closed = false;
|
|
118
|
+
const close = (error) => {
|
|
119
|
+
if (closed)
|
|
120
|
+
return;
|
|
121
|
+
closed = true;
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
setTimeout(() => server.stop(), 100);
|
|
124
|
+
finish(error);
|
|
125
|
+
};
|
|
126
|
+
const url = new URL("/authorize", this.#endpoints.accounts);
|
|
127
|
+
url.search = new URLSearchParams({
|
|
128
|
+
client_id: clientId,
|
|
129
|
+
response_type: "code",
|
|
130
|
+
redirect_uri: redirect,
|
|
131
|
+
scope: SCOPES.join(" "),
|
|
132
|
+
state,
|
|
133
|
+
code_challenge_method: "S256",
|
|
134
|
+
code_challenge: challenge
|
|
135
|
+
}).toString();
|
|
136
|
+
return { url: url.toString(), done, cancel: () => close(new Error("sign-in cancelled")) };
|
|
137
|
+
}
|
|
138
|
+
async signOut() {
|
|
139
|
+
this.#access = undefined;
|
|
140
|
+
await this.#options.tokens.set(undefined);
|
|
141
|
+
}
|
|
142
|
+
async request(method, path, query = {}, body) {
|
|
143
|
+
const url = new URL(`${this.#endpoints.api}${path}`);
|
|
144
|
+
for (const [key, value] of Object.entries(query))
|
|
145
|
+
url.searchParams.set(key, value);
|
|
146
|
+
for (let attempt = 0;; attempt++) {
|
|
147
|
+
const token = await this.#accessToken();
|
|
148
|
+
const response = await this.#fetch(url, {
|
|
149
|
+
method,
|
|
150
|
+
headers: {
|
|
151
|
+
authorization: `Bearer ${token}`,
|
|
152
|
+
...body !== undefined && { "content-type": "application/json" }
|
|
153
|
+
},
|
|
154
|
+
...body !== undefined && { body: JSON.stringify(body) },
|
|
155
|
+
signal: AbortSignal.timeout(1e4)
|
|
156
|
+
});
|
|
157
|
+
if (response.status === 401 && attempt === 0) {
|
|
158
|
+
this.#access = undefined;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (response.status === 429) {
|
|
162
|
+
throw new RateLimited(Number(response.headers.get("retry-after") ?? 5) * 1000);
|
|
163
|
+
}
|
|
164
|
+
const text = await response.text();
|
|
165
|
+
let answer;
|
|
166
|
+
try {
|
|
167
|
+
answer = text ? JSON.parse(text) : undefined;
|
|
168
|
+
} catch {
|
|
169
|
+
answer = undefined;
|
|
170
|
+
}
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
const error = answer?.error;
|
|
173
|
+
if (response.status === 403 && /scope/i.test(error?.message ?? "")) {
|
|
174
|
+
throw new Error("Sign in to Spotify again to allow this (sdeck asks for more now)");
|
|
175
|
+
}
|
|
176
|
+
throw new Error(REASONS[error?.reason ?? ""] ?? error?.message ?? `Spotify answered ${response.status}`);
|
|
177
|
+
}
|
|
178
|
+
return answer;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async#accessToken() {
|
|
182
|
+
if (this.#access && Date.now() < this.#access.expires)
|
|
183
|
+
return this.#access.token;
|
|
184
|
+
const refresh = this.#options.tokens.get();
|
|
185
|
+
if (!refresh)
|
|
186
|
+
throw new SignedOut;
|
|
187
|
+
this.#refreshing ??= this.#token({
|
|
188
|
+
grant_type: "refresh_token",
|
|
189
|
+
refresh_token: refresh
|
|
190
|
+
}).finally(() => {
|
|
191
|
+
this.#refreshing = undefined;
|
|
192
|
+
});
|
|
193
|
+
return this.#refreshing;
|
|
194
|
+
}
|
|
195
|
+
async#token(fields) {
|
|
196
|
+
const response = await this.#fetch(new URL("/api/token", this.#endpoints.accounts), {
|
|
197
|
+
method: "POST",
|
|
198
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
199
|
+
body: new URLSearchParams({ ...fields, client_id: this.#options.clientId() }),
|
|
200
|
+
signal: AbortSignal.timeout(1e4)
|
|
201
|
+
});
|
|
202
|
+
const body = await response.json().catch(() => ({}));
|
|
203
|
+
if (!response.ok || !body.access_token) {
|
|
204
|
+
if (body.error === "invalid_grant") {
|
|
205
|
+
await this.signOut();
|
|
206
|
+
throw new SignedOut("The Spotify sign-in expired: sign in again");
|
|
207
|
+
}
|
|
208
|
+
throw new Error(body.error_description ?? body.error ?? "Spotify gave no token");
|
|
209
|
+
}
|
|
210
|
+
if (body.refresh_token)
|
|
211
|
+
await this.#options.tokens.set(body.refresh_token);
|
|
212
|
+
const lifetime = Math.max(0, (body.expires_in ?? 3600) - 60);
|
|
213
|
+
this.#access = { token: body.access_token, expires: Date.now() + lifetime * 1000 };
|
|
214
|
+
return body.access_token;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
var IDLE = {
|
|
218
|
+
playing: false,
|
|
219
|
+
uri: "",
|
|
220
|
+
track: "",
|
|
221
|
+
artist: "",
|
|
222
|
+
album: "",
|
|
223
|
+
art: "",
|
|
224
|
+
progress: 0,
|
|
225
|
+
positionMs: 0,
|
|
226
|
+
durationMs: 0,
|
|
227
|
+
volume: undefined,
|
|
228
|
+
shuffle: false,
|
|
229
|
+
repeat: "off",
|
|
230
|
+
device: "",
|
|
231
|
+
deviceId: "",
|
|
232
|
+
context: ""
|
|
233
|
+
};
|
|
234
|
+
function parsePlayer(body) {
|
|
235
|
+
if (typeof body !== "object" || body === null)
|
|
236
|
+
return IDLE;
|
|
237
|
+
const raw = body;
|
|
238
|
+
const item = raw.item ?? undefined;
|
|
239
|
+
const images = item?.album?.images ?? item?.images ?? [];
|
|
240
|
+
const art = [...images].sort((a, b) => Math.abs((a.width ?? 0) - 300) - Math.abs((b.width ?? 0) - 300))[0]?.url ?? "";
|
|
241
|
+
const duration = item?.duration_ms ?? 0;
|
|
242
|
+
const position = raw.progress_ms ?? 0;
|
|
243
|
+
return {
|
|
244
|
+
playing: raw.is_playing === true,
|
|
245
|
+
uri: item?.uri ?? "",
|
|
246
|
+
track: item?.name ?? "",
|
|
247
|
+
artist: item?.artists?.map((a) => a.name).join(", ") ?? item?.show?.name ?? "",
|
|
248
|
+
album: item?.album?.name ?? "",
|
|
249
|
+
art,
|
|
250
|
+
progress: duration > 0 ? Math.round(100 * position / duration) : 0,
|
|
251
|
+
positionMs: position,
|
|
252
|
+
durationMs: duration,
|
|
253
|
+
volume: raw.device?.volume_percent ?? undefined,
|
|
254
|
+
shuffle: raw.shuffle_state === true,
|
|
255
|
+
repeat: raw.repeat_state === "track" || raw.repeat_state === "context" ? raw.repeat_state : "off",
|
|
256
|
+
device: raw.device?.name ?? "",
|
|
257
|
+
deviceId: raw.device?.id ?? "",
|
|
258
|
+
context: raw.context?.uri ?? ""
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
var KINDS = ["track", "episode", "album", "playlist", "artist", "show"];
|
|
262
|
+
function spotifyUri(text) {
|
|
263
|
+
const trimmed = text.trim();
|
|
264
|
+
const fromUri = /^spotify:([a-z]+):([A-Za-z0-9]+)$/.exec(trimmed);
|
|
265
|
+
const fromLink = /^https?:\/\/open\.spotify\.com\/(?:intl-[a-z-]+\/)?([a-z]+)\/([A-Za-z0-9]+)(?:[/?#].*)?$/i.exec(trimmed);
|
|
266
|
+
const [, kind, id] = fromUri ?? fromLink ?? [];
|
|
267
|
+
if (!kind || !id || !KINDS.includes(kind))
|
|
268
|
+
return;
|
|
269
|
+
return { uri: `spotify:${kind}:${id}`, kind };
|
|
270
|
+
}
|
|
271
|
+
function playBody(uri, kind) {
|
|
272
|
+
return kind === "track" || kind === "episode" ? { uris: [uri] } : { context_uri: uri };
|
|
273
|
+
}
|
|
274
|
+
function parsePlaylists(body, me) {
|
|
275
|
+
const items = body?.items ?? [];
|
|
276
|
+
return items.flatMap((item) => {
|
|
277
|
+
const p = item;
|
|
278
|
+
if (!p?.id || !p.uri)
|
|
279
|
+
return [];
|
|
280
|
+
return [
|
|
281
|
+
{
|
|
282
|
+
id: p.id,
|
|
283
|
+
uri: p.uri,
|
|
284
|
+
name: p.name || "Untitled playlist",
|
|
285
|
+
writable: p.owner?.id === me || p.collaborative === true
|
|
286
|
+
}
|
|
287
|
+
];
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
function parseDevices(body) {
|
|
291
|
+
const devices = body?.devices ?? [];
|
|
292
|
+
return devices.flatMap((item) => {
|
|
293
|
+
const d = item;
|
|
294
|
+
if (!d?.id || d.is_restricted)
|
|
295
|
+
return [];
|
|
296
|
+
return [{ id: d.id, name: d.name ?? d.id, type: d.type ?? "", active: d.is_active === true }];
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/panels.tsx
|
|
301
|
+
import { useHostState, useImage } from "@sdeck/plugin";
|
|
302
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
303
|
+
var GREEN = "#1ed760";
|
|
304
|
+
var BACKGROUND = "#121212";
|
|
305
|
+
var MUTED = "#a7a7a7";
|
|
306
|
+
var FAINT = "#2a2a2a";
|
|
307
|
+
var disc = (color, opacity = 1) => `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100"><circle cx="50" cy="50" r="50" fill="${color}" fill-opacity="${opacity}"/></svg>`)}`;
|
|
308
|
+
var box = { display: "flex" };
|
|
309
|
+
function Icon({ icon, size, color }) {
|
|
310
|
+
const src = useImage(`icon:${icon}`, color);
|
|
311
|
+
return src ? /* @__PURE__ */ jsx("img", {
|
|
312
|
+
src,
|
|
313
|
+
width: size,
|
|
314
|
+
height: size,
|
|
315
|
+
alt: ""
|
|
316
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
317
|
+
style: { ...box, width: size, height: size }
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function Progress({ value, height }) {
|
|
321
|
+
return /* @__PURE__ */ jsx("div", {
|
|
322
|
+
style: { ...box, width: "100%", height, background: "rgba(255,255,255,0.18)" },
|
|
323
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
324
|
+
style: {
|
|
325
|
+
...box,
|
|
326
|
+
width: `${Math.min(100, Math.max(0, value))}%`,
|
|
327
|
+
height,
|
|
328
|
+
background: GREEN
|
|
329
|
+
}
|
|
330
|
+
})
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
function useNowPlaying() {
|
|
334
|
+
const asText = (value) => typeof value === "string" ? value : "";
|
|
335
|
+
const track = asText(useHostState("spotify.track"));
|
|
336
|
+
const artist = asText(useHostState("spotify.artist"));
|
|
337
|
+
const artUrl = asText(useHostState("spotify.art"));
|
|
338
|
+
const progress = Number(useHostState("spotify.progress") ?? 0) || 0;
|
|
339
|
+
const playing = useHostState("spotify.playing") === true;
|
|
340
|
+
const liked = useHostState("spotify.liked") === true;
|
|
341
|
+
const art = useImage(artUrl);
|
|
342
|
+
return { track, artist, art, progress, playing, liked };
|
|
343
|
+
}
|
|
344
|
+
function NowPlaying({ kind }) {
|
|
345
|
+
const { track, artist, art, progress, playing, liked } = useNowPlaying();
|
|
346
|
+
if (kind === "sensor") {
|
|
347
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
348
|
+
style: {
|
|
349
|
+
...box,
|
|
350
|
+
width: "100%",
|
|
351
|
+
height: "100%",
|
|
352
|
+
alignItems: "center",
|
|
353
|
+
background: BACKGROUND,
|
|
354
|
+
gap: 4,
|
|
355
|
+
padding: "0 4px"
|
|
356
|
+
},
|
|
357
|
+
children: [
|
|
358
|
+
/* @__PURE__ */ jsx(Icon, {
|
|
359
|
+
icon: playing ? "tb/TbPlayerPlayFilled" : "tb/TbPlayerPauseFilled",
|
|
360
|
+
size: 10,
|
|
361
|
+
color: playing ? GREEN : MUTED
|
|
362
|
+
}),
|
|
363
|
+
/* @__PURE__ */ jsx("div", {
|
|
364
|
+
style: { ...box, flexGrow: 1 },
|
|
365
|
+
children: /* @__PURE__ */ jsx(Progress, {
|
|
366
|
+
value: track ? progress : 0,
|
|
367
|
+
height: 4
|
|
368
|
+
})
|
|
369
|
+
})
|
|
370
|
+
]
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (!track) {
|
|
374
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
375
|
+
style: {
|
|
376
|
+
...box,
|
|
377
|
+
width: "100%",
|
|
378
|
+
height: "100%",
|
|
379
|
+
flexDirection: kind === "window" ? "row" : "column",
|
|
380
|
+
alignItems: "center",
|
|
381
|
+
justifyContent: "center",
|
|
382
|
+
gap: 8,
|
|
383
|
+
background: BACKGROUND
|
|
384
|
+
},
|
|
385
|
+
children: [
|
|
386
|
+
/* @__PURE__ */ jsx(Icon, {
|
|
387
|
+
icon: "si/SiSpotify",
|
|
388
|
+
size: kind === "window" ? 30 : 40,
|
|
389
|
+
color: GREEN
|
|
390
|
+
}),
|
|
391
|
+
/* @__PURE__ */ jsx("div", {
|
|
392
|
+
style: { ...box, color: MUTED, fontSize: kind === "window" ? 15 : 11, fontWeight: 700 },
|
|
393
|
+
children: "Not playing"
|
|
394
|
+
})
|
|
395
|
+
]
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if (kind === "window") {
|
|
399
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
400
|
+
style: {
|
|
401
|
+
...box,
|
|
402
|
+
width: "100%",
|
|
403
|
+
height: "100%",
|
|
404
|
+
flexDirection: "column",
|
|
405
|
+
background: BACKGROUND
|
|
406
|
+
},
|
|
407
|
+
children: [
|
|
408
|
+
/* @__PURE__ */ jsxs("div", {
|
|
409
|
+
style: { ...box, flexGrow: 1, alignItems: "center", gap: 10, padding: "0 10px 0 0" },
|
|
410
|
+
children: [
|
|
411
|
+
art ? /* @__PURE__ */ jsx("img", {
|
|
412
|
+
src: art,
|
|
413
|
+
width: 55,
|
|
414
|
+
height: 55,
|
|
415
|
+
alt: ""
|
|
416
|
+
}) : /* @__PURE__ */ jsx("div", {
|
|
417
|
+
style: {
|
|
418
|
+
...box,
|
|
419
|
+
width: 55,
|
|
420
|
+
height: 55,
|
|
421
|
+
background: FAINT,
|
|
422
|
+
alignItems: "center",
|
|
423
|
+
justifyContent: "center"
|
|
424
|
+
},
|
|
425
|
+
children: /* @__PURE__ */ jsx(Icon, {
|
|
426
|
+
icon: "lucide/music",
|
|
427
|
+
size: 26,
|
|
428
|
+
color: MUTED
|
|
429
|
+
})
|
|
430
|
+
}),
|
|
431
|
+
/* @__PURE__ */ jsxs("div", {
|
|
432
|
+
style: {
|
|
433
|
+
...box,
|
|
434
|
+
flexDirection: "column",
|
|
435
|
+
flexGrow: 1,
|
|
436
|
+
flexShrink: 1,
|
|
437
|
+
minWidth: 0,
|
|
438
|
+
fontWeight: 700
|
|
439
|
+
},
|
|
440
|
+
children: [
|
|
441
|
+
/* @__PURE__ */ jsx("div", {
|
|
442
|
+
style: {
|
|
443
|
+
...box,
|
|
444
|
+
color: "#ffffff",
|
|
445
|
+
fontSize: 15,
|
|
446
|
+
overflow: "hidden",
|
|
447
|
+
whiteSpace: "nowrap"
|
|
448
|
+
},
|
|
449
|
+
children: track
|
|
450
|
+
}),
|
|
451
|
+
/* @__PURE__ */ jsx("div", {
|
|
452
|
+
style: {
|
|
453
|
+
...box,
|
|
454
|
+
color: MUTED,
|
|
455
|
+
fontSize: 12,
|
|
456
|
+
overflow: "hidden",
|
|
457
|
+
whiteSpace: "nowrap"
|
|
458
|
+
},
|
|
459
|
+
children: artist
|
|
460
|
+
})
|
|
461
|
+
]
|
|
462
|
+
}),
|
|
463
|
+
liked ? /* @__PURE__ */ jsx(Icon, {
|
|
464
|
+
icon: "tb/TbHeartFilled",
|
|
465
|
+
size: 16,
|
|
466
|
+
color: GREEN
|
|
467
|
+
}) : null,
|
|
468
|
+
/* @__PURE__ */ jsx(Icon, {
|
|
469
|
+
icon: playing ? "tb/TbPlayerPauseFilled" : "tb/TbPlayerPlayFilled",
|
|
470
|
+
size: 20,
|
|
471
|
+
color: playing ? "#ffffff" : GREEN
|
|
472
|
+
})
|
|
473
|
+
]
|
|
474
|
+
}),
|
|
475
|
+
/* @__PURE__ */ jsx(Progress, {
|
|
476
|
+
value: progress,
|
|
477
|
+
height: 3
|
|
478
|
+
})
|
|
479
|
+
]
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
483
|
+
style: {
|
|
484
|
+
...box,
|
|
485
|
+
width: "100%",
|
|
486
|
+
height: "100%",
|
|
487
|
+
position: "relative",
|
|
488
|
+
background: BACKGROUND
|
|
489
|
+
},
|
|
490
|
+
children: [
|
|
491
|
+
art ? /* @__PURE__ */ jsx("img", {
|
|
492
|
+
src: art,
|
|
493
|
+
width: 96,
|
|
494
|
+
height: 96,
|
|
495
|
+
alt: "",
|
|
496
|
+
style: { position: "absolute", left: 0, top: 0, width: "100%", height: "100%" }
|
|
497
|
+
}) : null,
|
|
498
|
+
/* @__PURE__ */ jsxs("div", {
|
|
499
|
+
style: {
|
|
500
|
+
...box,
|
|
501
|
+
position: "absolute",
|
|
502
|
+
left: 0,
|
|
503
|
+
right: 0,
|
|
504
|
+
bottom: 0,
|
|
505
|
+
flexDirection: "column",
|
|
506
|
+
padding: "14px 6px 0 6px",
|
|
507
|
+
backgroundImage: "linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.88) 45%)",
|
|
508
|
+
fontWeight: 700
|
|
509
|
+
},
|
|
510
|
+
children: [
|
|
511
|
+
/* @__PURE__ */ jsx("div", {
|
|
512
|
+
style: {
|
|
513
|
+
...box,
|
|
514
|
+
color: "#ffffff",
|
|
515
|
+
fontSize: 12,
|
|
516
|
+
overflow: "hidden",
|
|
517
|
+
whiteSpace: "nowrap"
|
|
518
|
+
},
|
|
519
|
+
children: track
|
|
520
|
+
}),
|
|
521
|
+
/* @__PURE__ */ jsx("div", {
|
|
522
|
+
style: {
|
|
523
|
+
...box,
|
|
524
|
+
color: "#d4d4d4",
|
|
525
|
+
fontSize: 10,
|
|
526
|
+
overflow: "hidden",
|
|
527
|
+
whiteSpace: "nowrap",
|
|
528
|
+
marginBottom: 5
|
|
529
|
+
},
|
|
530
|
+
children: artist
|
|
531
|
+
}),
|
|
532
|
+
/* @__PURE__ */ jsx("div", {
|
|
533
|
+
style: { ...box, margin: "0 -6px" },
|
|
534
|
+
children: /* @__PURE__ */ jsx(Progress, {
|
|
535
|
+
value: progress,
|
|
536
|
+
height: 3
|
|
537
|
+
})
|
|
538
|
+
})
|
|
539
|
+
]
|
|
540
|
+
}),
|
|
541
|
+
playing ? null : /* @__PURE__ */ jsx("div", {
|
|
542
|
+
style: {
|
|
543
|
+
...box,
|
|
544
|
+
position: "absolute",
|
|
545
|
+
right: 6,
|
|
546
|
+
top: 6,
|
|
547
|
+
width: 26,
|
|
548
|
+
height: 26,
|
|
549
|
+
borderRadius: 13,
|
|
550
|
+
alignItems: "center",
|
|
551
|
+
justifyContent: "center",
|
|
552
|
+
background: GREEN
|
|
553
|
+
},
|
|
554
|
+
children: /* @__PURE__ */ jsx(Icon, {
|
|
555
|
+
icon: "tb/TbPlayerPlayFilled",
|
|
556
|
+
size: 14,
|
|
557
|
+
color: "#000000"
|
|
558
|
+
})
|
|
559
|
+
})
|
|
560
|
+
]
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// src/index.tsx
|
|
565
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
566
|
+
var DEFAULT_PORT = 43821;
|
|
567
|
+
var POLL_MS = 3000;
|
|
568
|
+
var context;
|
|
569
|
+
var signingIn;
|
|
570
|
+
var spotify = new SpotifyClient({
|
|
571
|
+
clientId: () => String(context?.settings().clientId ?? "").trim(),
|
|
572
|
+
tokens: {
|
|
573
|
+
get: () => context?.secrets.get("refreshToken"),
|
|
574
|
+
set: async (token) => {
|
|
575
|
+
await context?.secrets.set("refreshToken", token);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
var pollNow = () => {};
|
|
580
|
+
var player = new Shared((emit) => {
|
|
581
|
+
let stopped = false;
|
|
582
|
+
let timer;
|
|
583
|
+
let running = false;
|
|
584
|
+
const tick = async () => {
|
|
585
|
+
if (running || stopped)
|
|
586
|
+
return;
|
|
587
|
+
running = true;
|
|
588
|
+
clearTimeout(timer);
|
|
589
|
+
let wait = POLL_MS;
|
|
590
|
+
try {
|
|
591
|
+
const next = parsePlayer(await spotify.request("GET", "/me/player"));
|
|
592
|
+
if (!stopped)
|
|
593
|
+
emit(next);
|
|
594
|
+
context?.status("Signed in to Spotify", "ok");
|
|
595
|
+
} catch (error) {
|
|
596
|
+
if (!stopped)
|
|
597
|
+
emit(undefined);
|
|
598
|
+
if (error instanceof RateLimited)
|
|
599
|
+
wait = error.retryAfterMs;
|
|
600
|
+
else if (error instanceof SignedOut)
|
|
601
|
+
wait = 1e4;
|
|
602
|
+
context?.status(error.message, error instanceof SignedOut ? "info" : "error");
|
|
603
|
+
}
|
|
604
|
+
running = false;
|
|
605
|
+
if (!stopped)
|
|
606
|
+
timer = setTimeout(tick, wait);
|
|
607
|
+
};
|
|
608
|
+
pollNow = () => {
|
|
609
|
+
clearTimeout(timer);
|
|
610
|
+
timer = setTimeout(tick, 400);
|
|
611
|
+
};
|
|
612
|
+
tick();
|
|
613
|
+
return () => {
|
|
614
|
+
stopped = true;
|
|
615
|
+
clearTimeout(timer);
|
|
616
|
+
pollNow = () => {};
|
|
617
|
+
};
|
|
618
|
+
});
|
|
619
|
+
var readPlayer = async () => parsePlayer(await spotify.request("GET", "/me/player"));
|
|
620
|
+
async function command(method, path, query, body) {
|
|
621
|
+
await spotify.request(method, path, query, body);
|
|
622
|
+
pollNow();
|
|
623
|
+
}
|
|
624
|
+
async function current() {
|
|
625
|
+
const now = await readPlayer();
|
|
626
|
+
if (!now.uri)
|
|
627
|
+
throw new Error("nothing is playing");
|
|
628
|
+
return now;
|
|
629
|
+
}
|
|
630
|
+
var isLiked = async (uri) => (await spotify.request("GET", "/me/library/contains", { uris: uri }))[0] === true;
|
|
631
|
+
var likedNow = () => {};
|
|
632
|
+
var liked = new Shared((emit) => {
|
|
633
|
+
let uri = "";
|
|
634
|
+
let checkedAt = 0;
|
|
635
|
+
let stopped = false;
|
|
636
|
+
const off = player.listen((p) => {
|
|
637
|
+
const next = p?.uri ?? "";
|
|
638
|
+
if (next === uri && Date.now() - checkedAt < 30000)
|
|
639
|
+
return;
|
|
640
|
+
uri = next;
|
|
641
|
+
checkedAt = Date.now();
|
|
642
|
+
if (!next)
|
|
643
|
+
return emit(undefined);
|
|
644
|
+
isLiked(next).then((value) => !stopped && uri === next && emit(value), () => !stopped && uri === next && emit(undefined));
|
|
645
|
+
});
|
|
646
|
+
likedNow = (track, value) => {
|
|
647
|
+
if (track !== uri)
|
|
648
|
+
return;
|
|
649
|
+
checkedAt = Date.now();
|
|
650
|
+
emit(value);
|
|
651
|
+
};
|
|
652
|
+
return () => {
|
|
653
|
+
stopped = true;
|
|
654
|
+
off();
|
|
655
|
+
likedNow = () => {};
|
|
656
|
+
};
|
|
657
|
+
});
|
|
658
|
+
var setLiked = async (uri, value) => {
|
|
659
|
+
await spotify.request(value ? "PUT" : "DELETE", "/me/library", { uris: uri });
|
|
660
|
+
likedNow(uri, value);
|
|
661
|
+
};
|
|
662
|
+
var me;
|
|
663
|
+
function userId() {
|
|
664
|
+
me ??= spotify.request("GET", "/me").then((body) => {
|
|
665
|
+
const id = body?.id;
|
|
666
|
+
if (!id)
|
|
667
|
+
throw new Error("Spotify did not say who is signed in");
|
|
668
|
+
return id;
|
|
669
|
+
});
|
|
670
|
+
me.catch(() => {
|
|
671
|
+
me = undefined;
|
|
672
|
+
});
|
|
673
|
+
return me;
|
|
674
|
+
}
|
|
675
|
+
async function playlists() {
|
|
676
|
+
const id = await userId();
|
|
677
|
+
const all = [];
|
|
678
|
+
for (let offset = 0;offset < 200; offset += 50) {
|
|
679
|
+
const page = await spotify.request("GET", "/me/playlists", {
|
|
680
|
+
limit: "50",
|
|
681
|
+
offset: String(offset)
|
|
682
|
+
});
|
|
683
|
+
const found = parsePlaylists(page, id);
|
|
684
|
+
all.push(...found);
|
|
685
|
+
if (!page?.next)
|
|
686
|
+
break;
|
|
687
|
+
}
|
|
688
|
+
return all;
|
|
689
|
+
}
|
|
690
|
+
var key = (icon, caption = "", color = "#ffffff") => iconLook({ icon, caption, background: BACKGROUND, color });
|
|
691
|
+
var dot = (active) => iconLayer("dot", disc(GREEN), { size: 7, position: "bottom", visible: active });
|
|
692
|
+
var switched = (active, on, off, colors) => [
|
|
693
|
+
iconLayer("on", on, { color: colors.on, size: 58, visible: active }),
|
|
694
|
+
iconLayer("off", off, { color: colors.off, size: 58, visible: `!(${active})` })
|
|
695
|
+
];
|
|
696
|
+
var setPlaying = (play) => command("PUT", play ? "/me/player/play" : "/me/player/pause");
|
|
697
|
+
var playPause = defineAction({
|
|
698
|
+
id: "play-pause",
|
|
699
|
+
name: "Play / pause",
|
|
700
|
+
description: "Shows the cover of what is playing; pressing it plays or pauses.",
|
|
701
|
+
category: "Spotify",
|
|
702
|
+
icon: "play",
|
|
703
|
+
slots: ["key", "sensor"],
|
|
704
|
+
settings: {
|
|
705
|
+
cover: { type: "boolean", label: "Show the cover art", default: true, appearance: true }
|
|
706
|
+
},
|
|
707
|
+
run: async () => {
|
|
708
|
+
const before = (await readPlayer()).playing;
|
|
709
|
+
await setPlaying(!before);
|
|
710
|
+
return { label: before ? "Pause" : "Play", undo: () => setPlaying(before) };
|
|
711
|
+
},
|
|
712
|
+
look: (s) => {
|
|
713
|
+
const centered = (size) => {
|
|
714
|
+
const at = (100 - size) / 2;
|
|
715
|
+
return { x: at, y: at, width: size, height: size };
|
|
716
|
+
};
|
|
717
|
+
const button = (visible) => [
|
|
718
|
+
iconLayer("disc", disc(GREEN), { frame: centered(70), visible }),
|
|
719
|
+
iconLayer("pause", "tb/TbPlayerPauseFilled", {
|
|
720
|
+
color: "#000000",
|
|
721
|
+
frame: centered(34),
|
|
722
|
+
visible: `(${visible}) && spotify.playing`
|
|
723
|
+
}),
|
|
724
|
+
iconLayer("play", "tb/TbPlayerPlayFilled", {
|
|
725
|
+
color: "#000000",
|
|
726
|
+
frame: { ...centered(34), x: 35 },
|
|
727
|
+
visible: `(${visible}) && !spotify.playing`
|
|
728
|
+
})
|
|
729
|
+
];
|
|
730
|
+
if (!s.cover)
|
|
731
|
+
return { background: BACKGROUND, layers: button("true") };
|
|
732
|
+
return {
|
|
733
|
+
background: BACKGROUND,
|
|
734
|
+
layers: [
|
|
735
|
+
iconLayer("cover", "{{spotify.art}}", { frame: centered(100), visible: "spotify.art" }),
|
|
736
|
+
iconLayer("shade", disc("#000000", 0.6), {
|
|
737
|
+
frame: centered(48),
|
|
738
|
+
visible: "spotify.art && !spotify.playing"
|
|
739
|
+
}),
|
|
740
|
+
iconLayer("resume", "tb/TbPlayerPlayFilled", {
|
|
741
|
+
frame: { ...centered(24), x: 39 },
|
|
742
|
+
visible: "spotify.art && !spotify.playing"
|
|
743
|
+
}),
|
|
744
|
+
...button("!spotify.art")
|
|
745
|
+
]
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
var skip = (id, name, icon, back) => defineAction({
|
|
750
|
+
id,
|
|
751
|
+
name,
|
|
752
|
+
category: "Spotify",
|
|
753
|
+
icon,
|
|
754
|
+
slots: ["key", "sensor"],
|
|
755
|
+
run: async () => {
|
|
756
|
+
await command("POST", `/me/player/${id}`);
|
|
757
|
+
return { label: name, undo: () => command("POST", `/me/player/${back}`) };
|
|
758
|
+
},
|
|
759
|
+
look: () => key(id === "next" ? "tb/TbPlayerSkipForwardFilled" : "tb/TbPlayerSkipBackFilled")
|
|
760
|
+
});
|
|
761
|
+
var next = skip("next", "Next track", "skip-forward", "previous");
|
|
762
|
+
var previous = skip("previous", "Previous track", "skip-back", "next");
|
|
763
|
+
var nowPlaying = defineAction({
|
|
764
|
+
id: "now-playing",
|
|
765
|
+
name: "Now playing",
|
|
766
|
+
description: "The current track and artist, with how far through it is.",
|
|
767
|
+
category: "Spotify",
|
|
768
|
+
icon: "music",
|
|
769
|
+
slots: ["window", "key", "sensor"],
|
|
770
|
+
settings: {
|
|
771
|
+
press: {
|
|
772
|
+
type: "select",
|
|
773
|
+
label: "When pressed",
|
|
774
|
+
default: "play-pause",
|
|
775
|
+
options: [
|
|
776
|
+
{ value: "play-pause", label: "Play or pause" },
|
|
777
|
+
{ value: "next", label: "Next track" },
|
|
778
|
+
{ value: "like", label: "Like or unlike" },
|
|
779
|
+
{ value: "none", label: "Nothing" }
|
|
780
|
+
]
|
|
781
|
+
}
|
|
782
|
+
},
|
|
783
|
+
display: true,
|
|
784
|
+
run: async () => ({ result: await readPlayer() }),
|
|
785
|
+
component: ({ settings, slot, plugin }) => {
|
|
786
|
+
const act = () => {
|
|
787
|
+
const press = settings.press;
|
|
788
|
+
if (press === "none")
|
|
789
|
+
return;
|
|
790
|
+
const done = press === "next" ? command("POST", "/me/player/next") : press === "like" ? current().then(async ({ uri }) => setLiked(uri, !await isLiked(uri))) : readPlayer().then((p) => setPlaying(!p.playing));
|
|
791
|
+
done.catch((error) => plugin.log("now playing:", error.message));
|
|
792
|
+
};
|
|
793
|
+
useKeyPress(slot.kind === "key" ? act : undefined, {
|
|
794
|
+
key: slot.kind === "key" ? slot.index : -1
|
|
795
|
+
});
|
|
796
|
+
useSensorPress(slot.kind === "sensor" ? act : undefined, {
|
|
797
|
+
sensor: slot.kind === "sensor" ? slot.index : -1
|
|
798
|
+
});
|
|
799
|
+
return /* @__PURE__ */ jsx2(NowPlaying, {
|
|
800
|
+
kind: slot.kind
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
var unmuted = 50;
|
|
805
|
+
var volume = defineAction({
|
|
806
|
+
id: "volume",
|
|
807
|
+
name: "Spotify volume",
|
|
808
|
+
category: "Spotify",
|
|
809
|
+
icon: "volume-1",
|
|
810
|
+
slots: ["key", "sensor"],
|
|
811
|
+
settings: {
|
|
812
|
+
operation: {
|
|
813
|
+
type: "select",
|
|
814
|
+
label: "When pressed",
|
|
815
|
+
default: "up",
|
|
816
|
+
options: [
|
|
817
|
+
{ value: "up", label: "Volume up" },
|
|
818
|
+
{ value: "down", label: "Volume down" },
|
|
819
|
+
{ value: "set", label: "Set a level" },
|
|
820
|
+
{ value: "mute", label: "Mute / unmute" }
|
|
821
|
+
]
|
|
822
|
+
},
|
|
823
|
+
step: {
|
|
824
|
+
type: "number",
|
|
825
|
+
label: "Step (%)",
|
|
826
|
+
default: 10,
|
|
827
|
+
min: 1,
|
|
828
|
+
max: 50,
|
|
829
|
+
visibleIf: { operation: ["up", "down"] }
|
|
830
|
+
},
|
|
831
|
+
level: {
|
|
832
|
+
type: "number",
|
|
833
|
+
label: "Level (%)",
|
|
834
|
+
default: 50,
|
|
835
|
+
min: 0,
|
|
836
|
+
max: 100,
|
|
837
|
+
visibleIf: { operation: "set" }
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
run: async ({ settings }) => {
|
|
841
|
+
const before = (await readPlayer()).volume;
|
|
842
|
+
if (before === undefined)
|
|
843
|
+
throw new Error("this device does not report its volume");
|
|
844
|
+
const { operation, step } = settings;
|
|
845
|
+
if (operation === "mute" && before > 0)
|
|
846
|
+
unmuted = before;
|
|
847
|
+
const wanted = operation === "mute" ? before > 0 ? 0 : unmuted : operation === "set" ? settings.level : before + (operation === "up" ? step : -step);
|
|
848
|
+
const level = Math.min(100, Math.max(0, Math.round(wanted)));
|
|
849
|
+
const set = (value) => command("PUT", "/me/player/volume", { volume_percent: String(value) });
|
|
850
|
+
await set(level);
|
|
851
|
+
return { result: level, label: `Spotify volume ${level}%`, undo: () => set(before) };
|
|
852
|
+
},
|
|
853
|
+
look: (s) => {
|
|
854
|
+
const icon = s.operation === "mute" ? "lucide/volume-x" : s.operation === "down" ? "lucide/volume-1" : "lucide/volume-2";
|
|
855
|
+
const level = {
|
|
856
|
+
id: "level",
|
|
857
|
+
type: "bar",
|
|
858
|
+
value: "spotify.volume ?? 0",
|
|
859
|
+
color: GREEN,
|
|
860
|
+
track: FAINT,
|
|
861
|
+
position: "bottom",
|
|
862
|
+
thickness: 6
|
|
863
|
+
};
|
|
864
|
+
if (s.operation === "mute") {
|
|
865
|
+
return {
|
|
866
|
+
background: BACKGROUND,
|
|
867
|
+
layers: [
|
|
868
|
+
...switched("spotify.volume == 0", icon, "lucide/volume-2", {
|
|
869
|
+
on: "#f87171",
|
|
870
|
+
off: "#ffffff"
|
|
871
|
+
}),
|
|
872
|
+
level
|
|
873
|
+
]
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
return {
|
|
877
|
+
background: BACKGROUND,
|
|
878
|
+
layers: [
|
|
879
|
+
iconLayer("icon", icon, {
|
|
880
|
+
color: "#ffffff",
|
|
881
|
+
size: s.operation === "set" ? 42 : 52,
|
|
882
|
+
position: s.operation === "set" ? "top" : "middle"
|
|
883
|
+
}),
|
|
884
|
+
...s.operation === "set" ? [textLayer("label", `${s.level}%`, { color: MUTED, size: 14, position: "bottom" })] : [],
|
|
885
|
+
level
|
|
886
|
+
]
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
var shuffle = defineAction({
|
|
891
|
+
id: "shuffle",
|
|
892
|
+
name: "Shuffle",
|
|
893
|
+
category: "Spotify",
|
|
894
|
+
icon: "shuffle",
|
|
895
|
+
slots: ["key", "sensor"],
|
|
896
|
+
run: async () => {
|
|
897
|
+
const before = (await readPlayer()).shuffle;
|
|
898
|
+
const set = (state) => command("PUT", "/me/player/shuffle", { state: String(state) });
|
|
899
|
+
await set(!before);
|
|
900
|
+
return { result: !before, label: `Shuffle ${before ? "off" : "on"}`, undo: () => set(before) };
|
|
901
|
+
},
|
|
902
|
+
look: () => ({
|
|
903
|
+
background: BACKGROUND,
|
|
904
|
+
layers: [
|
|
905
|
+
...switched("spotify.shuffle", "lucide/shuffle", "lucide/shuffle", {
|
|
906
|
+
on: GREEN,
|
|
907
|
+
off: MUTED
|
|
908
|
+
}),
|
|
909
|
+
dot("spotify.shuffle")
|
|
910
|
+
]
|
|
911
|
+
})
|
|
912
|
+
});
|
|
913
|
+
function nextRepeat(mode) {
|
|
914
|
+
return mode === "off" ? "context" : mode === "context" ? "track" : "off";
|
|
915
|
+
}
|
|
916
|
+
var repeat = defineAction({
|
|
917
|
+
id: "repeat",
|
|
918
|
+
name: "Repeat",
|
|
919
|
+
description: "Cycles repeat: off, the playlist, one track.",
|
|
920
|
+
category: "Spotify",
|
|
921
|
+
icon: "repeat",
|
|
922
|
+
slots: ["key", "sensor"],
|
|
923
|
+
run: async () => {
|
|
924
|
+
const before = (await readPlayer()).repeat;
|
|
925
|
+
const state = nextRepeat(before);
|
|
926
|
+
const set = (value) => command("PUT", "/me/player/repeat", { state: value });
|
|
927
|
+
await set(state);
|
|
928
|
+
return { result: state, label: `Repeat ${state}`, undo: () => set(before) };
|
|
929
|
+
},
|
|
930
|
+
look: () => ({
|
|
931
|
+
background: BACKGROUND,
|
|
932
|
+
layers: [
|
|
933
|
+
iconLayer("off", "lucide/repeat", {
|
|
934
|
+
color: MUTED,
|
|
935
|
+
size: 58,
|
|
936
|
+
visible: 'spotify.repeat != "context" && spotify.repeat != "track"'
|
|
937
|
+
}),
|
|
938
|
+
iconLayer("all", "lucide/repeat", {
|
|
939
|
+
color: GREEN,
|
|
940
|
+
size: 58,
|
|
941
|
+
visible: 'spotify.repeat == "context"'
|
|
942
|
+
}),
|
|
943
|
+
iconLayer("one", "lucide/repeat-1", {
|
|
944
|
+
color: GREEN,
|
|
945
|
+
size: 58,
|
|
946
|
+
visible: 'spotify.repeat == "track"'
|
|
947
|
+
}),
|
|
948
|
+
dot('spotify.repeat == "context" || spotify.repeat == "track"')
|
|
949
|
+
]
|
|
950
|
+
})
|
|
951
|
+
});
|
|
952
|
+
var like = defineAction({
|
|
953
|
+
id: "like",
|
|
954
|
+
name: "Like",
|
|
955
|
+
description: "Adds what is playing to Liked Songs, or takes it out. Lit while it is liked.",
|
|
956
|
+
category: "Spotify",
|
|
957
|
+
icon: "heart",
|
|
958
|
+
slots: ["key", "sensor"],
|
|
959
|
+
settings: {
|
|
960
|
+
mode: {
|
|
961
|
+
type: "select",
|
|
962
|
+
label: "When pressed",
|
|
963
|
+
default: "toggle",
|
|
964
|
+
options: [
|
|
965
|
+
{ value: "toggle", label: "Like / unlike" },
|
|
966
|
+
{ value: "like", label: "Like" },
|
|
967
|
+
{ value: "unlike", label: "Unlike" }
|
|
968
|
+
]
|
|
969
|
+
}
|
|
970
|
+
},
|
|
971
|
+
run: async ({ settings }) => {
|
|
972
|
+
const { uri, track } = await current();
|
|
973
|
+
const before = await isLiked(uri);
|
|
974
|
+
const after = settings.mode === "toggle" ? !before : settings.mode === "like";
|
|
975
|
+
if (after !== before)
|
|
976
|
+
await setLiked(uri, after);
|
|
977
|
+
return {
|
|
978
|
+
result: after,
|
|
979
|
+
label: `${after ? "Liked" : "Unliked"} ${track}`,
|
|
980
|
+
...after !== before && { undo: () => setLiked(uri, before) }
|
|
981
|
+
};
|
|
982
|
+
},
|
|
983
|
+
look: () => ({
|
|
984
|
+
background: BACKGROUND,
|
|
985
|
+
layers: switched("spotify.liked == true", "tb/TbHeartFilled", "tb/TbHeart", {
|
|
986
|
+
on: GREEN,
|
|
987
|
+
off: MUTED
|
|
988
|
+
})
|
|
989
|
+
})
|
|
990
|
+
});
|
|
991
|
+
var addToPlaylist = defineAction({
|
|
992
|
+
id: "add-to-playlist",
|
|
993
|
+
name: "Add to playlist",
|
|
994
|
+
description: "Adds what is playing to one of your playlists.",
|
|
995
|
+
category: "Spotify",
|
|
996
|
+
icon: "list-plus",
|
|
997
|
+
slots: ["key", "sensor"],
|
|
998
|
+
settings: {
|
|
999
|
+
playlist: {
|
|
1000
|
+
type: "select",
|
|
1001
|
+
label: "Playlist",
|
|
1002
|
+
options: [],
|
|
1003
|
+
source: "playlists",
|
|
1004
|
+
description: "Yours, and ones you collaborate on."
|
|
1005
|
+
},
|
|
1006
|
+
text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
|
|
1007
|
+
},
|
|
1008
|
+
run: async ({ settings }) => {
|
|
1009
|
+
if (!settings.playlist)
|
|
1010
|
+
throw new Error("Pick a playlist in the key's settings");
|
|
1011
|
+
const { uri, track } = await current();
|
|
1012
|
+
await spotify.request("POST", `/playlists/${encodeURIComponent(settings.playlist)}/items`, {}, {
|
|
1013
|
+
uris: [uri]
|
|
1014
|
+
});
|
|
1015
|
+
return { label: `Added ${track} to a playlist` };
|
|
1016
|
+
},
|
|
1017
|
+
look: (s) => key("lucide/list-plus", s.text)
|
|
1018
|
+
});
|
|
1019
|
+
var play = defineAction({
|
|
1020
|
+
id: "play",
|
|
1021
|
+
name: "Play",
|
|
1022
|
+
description: "Plays a playlist, album, artist, track or episode, or adds it to the queue.",
|
|
1023
|
+
category: "Spotify",
|
|
1024
|
+
icon: "list-music",
|
|
1025
|
+
slots: ["key", "sensor"],
|
|
1026
|
+
settings: {
|
|
1027
|
+
item: {
|
|
1028
|
+
type: "text",
|
|
1029
|
+
label: "What to play",
|
|
1030
|
+
source: "library",
|
|
1031
|
+
placeholder: "A Spotify link, or pick a playlist",
|
|
1032
|
+
description: "In Spotify: Share \u2192 Copy link."
|
|
1033
|
+
},
|
|
1034
|
+
mode: {
|
|
1035
|
+
type: "select",
|
|
1036
|
+
label: "When pressed",
|
|
1037
|
+
default: "play",
|
|
1038
|
+
options: [
|
|
1039
|
+
{ value: "play", label: "Play now" },
|
|
1040
|
+
{ value: "queue", label: "Add to the queue (tracks and episodes)" }
|
|
1041
|
+
]
|
|
1042
|
+
},
|
|
1043
|
+
shuffle: {
|
|
1044
|
+
type: "boolean",
|
|
1045
|
+
label: "Shuffle",
|
|
1046
|
+
default: false,
|
|
1047
|
+
visibleIf: { mode: "play" }
|
|
1048
|
+
},
|
|
1049
|
+
text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
|
|
1050
|
+
},
|
|
1051
|
+
run: async ({ settings }) => {
|
|
1052
|
+
const target = spotifyUri(settings.item);
|
|
1053
|
+
if (!target)
|
|
1054
|
+
throw new Error("set what to play: a Spotify link or spotify: URI");
|
|
1055
|
+
if (settings.mode === "queue") {
|
|
1056
|
+
if (target.kind !== "track" && target.kind !== "episode") {
|
|
1057
|
+
throw new Error("only tracks and episodes can be queued");
|
|
1058
|
+
}
|
|
1059
|
+
await command("POST", "/me/player/queue", { uri: target.uri });
|
|
1060
|
+
return { label: "Added to the queue" };
|
|
1061
|
+
}
|
|
1062
|
+
await command("PUT", "/me/player/play", {}, playBody(target.uri, target.kind));
|
|
1063
|
+
if (settings.shuffle)
|
|
1064
|
+
await command("PUT", "/me/player/shuffle", { state: "true" });
|
|
1065
|
+
return { label: `Play ${target.kind}` };
|
|
1066
|
+
},
|
|
1067
|
+
look: (s) => key(s.mode === "queue" ? "lucide/list-end" : "lucide/list-music", s.text)
|
|
1068
|
+
});
|
|
1069
|
+
var seek = defineAction({
|
|
1070
|
+
id: "seek",
|
|
1071
|
+
name: "Seek",
|
|
1072
|
+
description: "Jumps forward or back in the track.",
|
|
1073
|
+
category: "Spotify",
|
|
1074
|
+
icon: "fast-forward",
|
|
1075
|
+
slots: ["key", "sensor"],
|
|
1076
|
+
settings: {
|
|
1077
|
+
direction: {
|
|
1078
|
+
type: "select",
|
|
1079
|
+
label: "Direction",
|
|
1080
|
+
default: "forward",
|
|
1081
|
+
options: [
|
|
1082
|
+
{ value: "forward", label: "Forward" },
|
|
1083
|
+
{ value: "back", label: "Back" },
|
|
1084
|
+
{ value: "start", label: "To the start" }
|
|
1085
|
+
]
|
|
1086
|
+
},
|
|
1087
|
+
seconds: {
|
|
1088
|
+
type: "number",
|
|
1089
|
+
label: "Seconds",
|
|
1090
|
+
default: 15,
|
|
1091
|
+
min: 1,
|
|
1092
|
+
max: 600,
|
|
1093
|
+
visibleIf: { direction: ["forward", "back"] }
|
|
1094
|
+
}
|
|
1095
|
+
},
|
|
1096
|
+
run: async ({ settings }) => {
|
|
1097
|
+
const { positionMs, durationMs } = await current();
|
|
1098
|
+
const jump = settings.seconds * 1000;
|
|
1099
|
+
const wanted = settings.direction === "start" ? 0 : positionMs + (settings.direction === "forward" ? jump : -jump);
|
|
1100
|
+
const to = Math.round(Math.min(Math.max(0, durationMs - 1000), Math.max(0, wanted)));
|
|
1101
|
+
const go = (ms) => command("PUT", "/me/player/seek", { position_ms: String(ms) });
|
|
1102
|
+
await go(to);
|
|
1103
|
+
return { label: `Seek to ${Math.round(to / 1000)} s`, undo: () => go(positionMs) };
|
|
1104
|
+
},
|
|
1105
|
+
look: (s) => key(s.direction === "start" ? "lucide/undo-2" : s.direction === "forward" ? "lucide/fast-forward" : "lucide/rewind", s.direction === "start" ? "" : `${s.direction === "forward" ? "+" : "\u2212"}${s.seconds}s`)
|
|
1106
|
+
});
|
|
1107
|
+
var transfer = (id) => command("PUT", "/me/player", {}, { device_ids: [id], play: true });
|
|
1108
|
+
var device = defineAction({
|
|
1109
|
+
id: "device",
|
|
1110
|
+
name: "Play on a device",
|
|
1111
|
+
description: "Moves playback to another speaker, phone or computer.",
|
|
1112
|
+
category: "Spotify",
|
|
1113
|
+
icon: "speaker",
|
|
1114
|
+
slots: ["key", "sensor"],
|
|
1115
|
+
settings: {
|
|
1116
|
+
device: {
|
|
1117
|
+
type: "select",
|
|
1118
|
+
label: "Device",
|
|
1119
|
+
options: [],
|
|
1120
|
+
source: "devices",
|
|
1121
|
+
description: "Only devices with Spotify open are listed."
|
|
1122
|
+
},
|
|
1123
|
+
text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
|
|
1124
|
+
},
|
|
1125
|
+
run: async ({ settings }) => {
|
|
1126
|
+
if (!settings.device)
|
|
1127
|
+
throw new Error("Pick a device in the key's settings");
|
|
1128
|
+
const before = (await readPlayer()).deviceId;
|
|
1129
|
+
await transfer(settings.device);
|
|
1130
|
+
return {
|
|
1131
|
+
label: "Moved playback",
|
|
1132
|
+
...before && before !== settings.device && { undo: () => transfer(before) }
|
|
1133
|
+
};
|
|
1134
|
+
},
|
|
1135
|
+
look: (s) => key("lucide/speaker", s.text)
|
|
1136
|
+
});
|
|
1137
|
+
var trackChanged = defineTrigger({
|
|
1138
|
+
id: "track-changed",
|
|
1139
|
+
name: "When the track changes",
|
|
1140
|
+
description: "Fires with the new track whenever something else starts playing.",
|
|
1141
|
+
subscribe: ({ fire }) => {
|
|
1142
|
+
let uri;
|
|
1143
|
+
return player.listen((p) => {
|
|
1144
|
+
const next = p?.uri ?? "";
|
|
1145
|
+
if (uri !== undefined && next && next !== uri)
|
|
1146
|
+
fire(p);
|
|
1147
|
+
if (p)
|
|
1148
|
+
uri = next;
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
var stateOf = (id, name, type) => ({
|
|
1153
|
+
id,
|
|
1154
|
+
name,
|
|
1155
|
+
type,
|
|
1156
|
+
start: (set) => player.listen((p) => set(p?.[id]))
|
|
1157
|
+
});
|
|
1158
|
+
var playlistOptions = async (writable) => (await playlists()).filter((p) => !writable || p.writable).map((p) => ({ value: writable ? p.id : p.uri, label: p.name }));
|
|
1159
|
+
var src_default = definePlugin({
|
|
1160
|
+
id: "@sdeck/plugin-spotify",
|
|
1161
|
+
name: "Spotify",
|
|
1162
|
+
version: "0.2.0",
|
|
1163
|
+
namespace: "spotify",
|
|
1164
|
+
description: "Now playing, playback, Liked Songs, playlists and devices through the Spotify Web API.",
|
|
1165
|
+
author: "sdeck",
|
|
1166
|
+
permissions: ["network"],
|
|
1167
|
+
settings: {
|
|
1168
|
+
clientId: {
|
|
1169
|
+
type: "text",
|
|
1170
|
+
label: "Client ID",
|
|
1171
|
+
description: "Create an app at developer.spotify.com and add the redirect URI http://127.0.0.1:43821/callback (with your port)."
|
|
1172
|
+
},
|
|
1173
|
+
port: {
|
|
1174
|
+
type: "number",
|
|
1175
|
+
label: "Sign-in port",
|
|
1176
|
+
default: DEFAULT_PORT,
|
|
1177
|
+
min: 1024,
|
|
1178
|
+
max: 65535
|
|
1179
|
+
}
|
|
1180
|
+
},
|
|
1181
|
+
onboarding: {
|
|
1182
|
+
intro: "Spotify lets apps in only with an app of your own. It takes a minute.",
|
|
1183
|
+
steps: [
|
|
1184
|
+
{
|
|
1185
|
+
id: "app",
|
|
1186
|
+
title: "Create a Spotify app",
|
|
1187
|
+
body: "Open the Spotify developer dashboard, sign in and press **Create app**. Give it any name, add the redirect URI `http://127.0.0.1:43821/callback`, tick **Web API**, and save.\n\nThen copy the app's **Client ID** here.",
|
|
1188
|
+
link: {
|
|
1189
|
+
label: "Spotify developer dashboard",
|
|
1190
|
+
url: "https://developer.spotify.com/dashboard"
|
|
1191
|
+
},
|
|
1192
|
+
settings: ["clientId"]
|
|
1193
|
+
},
|
|
1194
|
+
{
|
|
1195
|
+
id: "sign-in",
|
|
1196
|
+
title: "Sign in",
|
|
1197
|
+
body: "Allow sdeck in the browser that opens. Controlling playback needs Spotify Premium.",
|
|
1198
|
+
command: "sign-in",
|
|
1199
|
+
done: (activated) => activated.secrets.get("refreshToken") !== undefined
|
|
1200
|
+
}
|
|
1201
|
+
],
|
|
1202
|
+
starterPage: {
|
|
1203
|
+
name: "Spotify",
|
|
1204
|
+
keys: {
|
|
1205
|
+
0: { action: "previous" },
|
|
1206
|
+
1: { action: "play-pause" },
|
|
1207
|
+
2: { action: "next" },
|
|
1208
|
+
3: { action: "like" },
|
|
1209
|
+
4: { action: "volume", settings: { operation: "down" } },
|
|
1210
|
+
5: { action: "volume", settings: { operation: "up" } },
|
|
1211
|
+
6: { action: "shuffle" },
|
|
1212
|
+
7: { action: "repeat" }
|
|
1213
|
+
},
|
|
1214
|
+
window: { action: "now-playing" }
|
|
1215
|
+
}
|
|
1216
|
+
},
|
|
1217
|
+
commands: [
|
|
1218
|
+
{
|
|
1219
|
+
id: "sign-in",
|
|
1220
|
+
name: "Sign in",
|
|
1221
|
+
description: "Opens Spotify in your browser to allow access.",
|
|
1222
|
+
run: (activated) => {
|
|
1223
|
+
signingIn?.cancel();
|
|
1224
|
+
signingIn = spotify.signIn(Number(activated.settings().port));
|
|
1225
|
+
activated.status("Waiting for you to allow access in the browser", "info");
|
|
1226
|
+
signingIn.done.then(() => {
|
|
1227
|
+
activated.status("Signed in to Spotify", "ok");
|
|
1228
|
+
me = undefined;
|
|
1229
|
+
pollNow();
|
|
1230
|
+
}, (error) => activated.status(error.message, "error"));
|
|
1231
|
+
return signingIn.url;
|
|
1232
|
+
}
|
|
1233
|
+
},
|
|
1234
|
+
{
|
|
1235
|
+
id: "sign-out",
|
|
1236
|
+
name: "Sign out",
|
|
1237
|
+
run: async (activated) => {
|
|
1238
|
+
signingIn?.cancel();
|
|
1239
|
+
await spotify.signOut();
|
|
1240
|
+
me = undefined;
|
|
1241
|
+
activated.status("Signed out", "info");
|
|
1242
|
+
pollNow();
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
],
|
|
1246
|
+
actions: [
|
|
1247
|
+
playPause,
|
|
1248
|
+
next,
|
|
1249
|
+
previous,
|
|
1250
|
+
nowPlaying,
|
|
1251
|
+
like,
|
|
1252
|
+
addToPlaylist,
|
|
1253
|
+
play,
|
|
1254
|
+
seek,
|
|
1255
|
+
volume,
|
|
1256
|
+
shuffle,
|
|
1257
|
+
repeat,
|
|
1258
|
+
device
|
|
1259
|
+
],
|
|
1260
|
+
triggers: [trackChanged],
|
|
1261
|
+
optionSources: {
|
|
1262
|
+
playlists: () => playlistOptions(true),
|
|
1263
|
+
library: () => playlistOptions(false),
|
|
1264
|
+
devices: async () => parseDevices(await spotify.request("GET", "/me/player/devices")).map((d) => ({
|
|
1265
|
+
value: d.id,
|
|
1266
|
+
label: `${d.name}${d.active ? " (playing)" : ""}`
|
|
1267
|
+
}))
|
|
1268
|
+
},
|
|
1269
|
+
state: [
|
|
1270
|
+
stateOf("playing", "Playing", "boolean"),
|
|
1271
|
+
stateOf("track", "Track", "string"),
|
|
1272
|
+
stateOf("artist", "Artist", "string"),
|
|
1273
|
+
stateOf("album", "Album", "string"),
|
|
1274
|
+
stateOf("art", "Cover art", "string"),
|
|
1275
|
+
stateOf("progress", "Progress", "number"),
|
|
1276
|
+
stateOf("volume", "Volume", "number"),
|
|
1277
|
+
stateOf("shuffle", "Shuffle", "boolean"),
|
|
1278
|
+
stateOf("repeat", "Repeat", "string"),
|
|
1279
|
+
stateOf("device", "Device", "string"),
|
|
1280
|
+
stateOf("uri", "Track URI", "string"),
|
|
1281
|
+
{
|
|
1282
|
+
id: "liked",
|
|
1283
|
+
name: "Liked",
|
|
1284
|
+
type: "boolean",
|
|
1285
|
+
description: "Whether what is playing is in Liked Songs.",
|
|
1286
|
+
start: (set) => liked.listen(set)
|
|
1287
|
+
}
|
|
1288
|
+
],
|
|
1289
|
+
activate(activated) {
|
|
1290
|
+
context = activated;
|
|
1291
|
+
activated.status(activated.secrets.get("refreshToken") ? "Signed in to Spotify" : "Set your client id, then sign in", "info");
|
|
1292
|
+
return () => {
|
|
1293
|
+
signingIn?.cancel();
|
|
1294
|
+
context = undefined;
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
});
|
|
1298
|
+
export {
|
|
1299
|
+
DEFAULT_PORT,
|
|
1300
|
+
IDLE,
|
|
1301
|
+
RateLimited,
|
|
1302
|
+
SCOPES,
|
|
1303
|
+
SPOTIFY,
|
|
1304
|
+
SignedOut,
|
|
1305
|
+
SpotifyClient,
|
|
1306
|
+
createPkce,
|
|
1307
|
+
src_default as default,
|
|
1308
|
+
nextRepeat,
|
|
1309
|
+
parseDevices,
|
|
1310
|
+
parsePlayer,
|
|
1311
|
+
parsePlaylists,
|
|
1312
|
+
playBody,
|
|
1313
|
+
spotifyUri
|
|
1314
|
+
};
|
|
1315
|
+
|
|
1316
|
+
//# debugId=A943F62868708C8764756E2164756E21
|