@sdeck/youtube 0.0.0-stage → 1.0.1

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.
@@ -0,0 +1,270 @@
1
+ /** Actions that only show something. */
2
+ import { setting } from "@sdeck/plugin";
3
+ import {
4
+ captionLayer,
5
+ iconLayer,
6
+ iconSize,
7
+ type Look,
8
+ meterLayer,
9
+ rectLayer,
10
+ surface,
11
+ textLayer,
12
+ } from "@sdeck/schema";
13
+ import { BACKGROUND, CORAL, category, MUTED, RED, short } from "./looks.ts";
14
+ import { plugin } from "./plugin.ts";
15
+ import * as values from "./state.ts";
16
+
17
+ const slots = ["key", "window"] as const;
18
+ const LIVE = values.live.key;
19
+ const OFF = `!${LIVE}`;
20
+
21
+ /** The window is wide and low: a line about the stream, over its title. */
22
+ function statusWindow(): Look {
23
+ const pill = { x: 4, y: 14, width: 19, height: 36 };
24
+ const subscribers = values.subscriberCount.key;
25
+ const known = `${OFF} && ${subscribers} != null`;
26
+ return {
27
+ background: surface(BACKGROUND),
28
+ layers: [
29
+ rectLayer("badge", { frame: pill, fill: RED, radius: 6, visible: LIVE }),
30
+ textLayer("live", "LIVE", { size: 13, frame: pill, visible: LIVE }),
31
+ iconLayer("eye", "lucide/eye", {
32
+ color: CORAL,
33
+ frame: { x: 27, y: 15, width: 9, height: 34 },
34
+ visible: LIVE,
35
+ }),
36
+ textLayer("viewers", short(values.viewers.key), {
37
+ size: 21,
38
+ align: "left",
39
+ frame: { x: 37.5, y: 10, width: 32, height: 44 },
40
+ visible: LIVE,
41
+ }),
42
+ textLayer("uptime", values.uptime.template, {
43
+ size: 15,
44
+ color: MUTED,
45
+ align: "right",
46
+ frame: { x: 69, y: 10, width: 27, height: 44 },
47
+ visible: LIVE,
48
+ }),
49
+ textLayer("title", values.title.template, {
50
+ size: 13,
51
+ color: MUTED,
52
+ align: "left",
53
+ frame: { x: 4, y: 58, width: 92, height: 32 },
54
+ visible: LIVE,
55
+ }),
56
+ iconLayer("logo", "si/SiYoutube", {
57
+ color: RED,
58
+ frame: { x: 19, y: 18, width: 17, height: 64 },
59
+ visible: OFF,
60
+ }),
61
+ textLayer("offline", "Offline", {
62
+ size: 20,
63
+ align: "left",
64
+ frame: { x: 41, y: 12, width: 55, height: 44 },
65
+ visible: known,
66
+ }),
67
+ textLayer("offline-alone", "Offline", {
68
+ size: 20,
69
+ align: "left",
70
+ frame: { x: 41, y: 20, width: 55, height: 60 },
71
+ visible: `${OFF} && ${subscribers} == null`,
72
+ }),
73
+ textLayer("subscribers", `${short(subscribers)} subscribers`, {
74
+ size: 13,
75
+ color: MUTED,
76
+ align: "left",
77
+ frame: { x: 41, y: 54, width: 55, height: 32 },
78
+ visible: known,
79
+ }),
80
+ ],
81
+ };
82
+ }
83
+
84
+ const status = plugin.action({
85
+ id: "status",
86
+ name: "Stream status",
87
+ description: "Whether you are live, with viewers and the time since it started.",
88
+ category,
89
+ icon: "radio",
90
+ slots,
91
+ display: true,
92
+ look: (_, slot): Look => {
93
+ if (slot === "window") return statusWindow();
94
+ const pill = { x: 22, y: 13, width: 56, height: 22 };
95
+ return {
96
+ background: surface(BACKGROUND),
97
+ layers: [
98
+ rectLayer("badge", { frame: pill, fill: RED, radius: 6, visible: LIVE }),
99
+ textLayer("live", "LIVE", { size: 13, frame: pill, visible: LIVE }),
100
+ textLayer("viewers", short(values.viewers.key), {
101
+ size: 25,
102
+ frame: { x: 0, y: 38, width: 100, height: 30 },
103
+ visible: LIVE,
104
+ }),
105
+ textLayer("uptime", values.uptime.template, {
106
+ size: 13,
107
+ color: MUTED,
108
+ frame: { x: 0, y: 69, width: 100, height: 20 },
109
+ visible: LIVE,
110
+ }),
111
+ iconLayer("logo", "si/SiYoutube", { color: MUTED, size: iconSize(true), visible: OFF }),
112
+ captionLayer("offline", "Offline", { color: MUTED, visible: OFF }),
113
+ ],
114
+ };
115
+ },
116
+ });
117
+
118
+ const goal = setting.number("Goal", {
119
+ default: 0,
120
+ min: 0,
121
+ description: "Shows a bar that fills up. 0 for none.",
122
+ appearance: true,
123
+ });
124
+
125
+ /** A count under an icon, or beside it in the window, with a bar towards a goal. */
126
+ function countLook(
127
+ slot: string,
128
+ options: { icon: string; path: string; unit: string; goal: number },
129
+ ): Look {
130
+ const { icon, path, unit, goal } = options;
131
+ const count = short(path);
132
+ const wide = slot === "window";
133
+ const row = wide ? { row: "line" } : {};
134
+ return {
135
+ background: surface(BACKGROUND),
136
+ layers: [
137
+ iconLayer("icon", icon, {
138
+ color: CORAL,
139
+ size: wide ? (goal > 0 ? 40 : 50) : goal > 0 ? 38 : 44,
140
+ ...row,
141
+ }),
142
+ textLayer("label", count, { size: wide ? (goal > 0 ? 20 : 26) : 18, ...row }),
143
+ ...(wide ? [textLayer("unit", unit, { color: MUTED, size: 14, ...row })] : []),
144
+ ...(goal > 0
145
+ ? [
146
+ meterLayer("goal", `${path} == null ? 0 : min(100, 100 * ${path} / ${goal})`, {
147
+ color: RED,
148
+ }),
149
+ ]
150
+ : []),
151
+ ],
152
+ };
153
+ }
154
+
155
+ const subscribers = plugin.action({
156
+ id: "subscribers",
157
+ name: "Subscribers",
158
+ description: "With a bar towards a goal. YouTube rounds the number down to three digits.",
159
+ category,
160
+ icon: "users",
161
+ slots,
162
+ display: true,
163
+ settings: { goal },
164
+ look: (s, slot) =>
165
+ countLook(slot, {
166
+ icon: "lucide/users",
167
+ path: values.subscriberCount.key,
168
+ unit: "subscribers",
169
+ goal: s.goal,
170
+ }),
171
+ });
172
+
173
+ const likes = plugin.action({
174
+ id: "likes",
175
+ name: "Likes",
176
+ description: "Of the stream that is live, with a bar towards a goal.",
177
+ category,
178
+ icon: "thumbs-up",
179
+ slots,
180
+ display: true,
181
+ settings: { goal },
182
+ look: (s, slot) =>
183
+ countLook(slot, {
184
+ icon: "lucide/thumbs-up",
185
+ path: values.likes.key,
186
+ unit: "likes",
187
+ goal: s.goal,
188
+ }),
189
+ });
190
+
191
+ const VIDEO = {
192
+ views: { icon: "lucide/square-play", path: values.videoViews.key },
193
+ likes: { icon: "lucide/thumbs-up", path: values.videoLikes.key },
194
+ comments: { icon: "lucide/message-circle", path: values.videoComments.key },
195
+ };
196
+
197
+ const newestVideo = plugin.action({
198
+ id: "video",
199
+ name: "Newest video",
200
+ description: "How the last video you published does: its views, likes or comments.",
201
+ category,
202
+ icon: "square-play",
203
+ slots,
204
+ display: true,
205
+ settings: {
206
+ show: setting.select("Show", { views: "Views", likes: "Likes", comments: "Comments" }),
207
+ goal,
208
+ },
209
+ look: (s, slot) => countLook(slot, { ...VIDEO[s.show], unit: s.show, goal: s.goal }),
210
+ });
211
+
212
+ const streamHealth = plugin.action({
213
+ id: "health",
214
+ name: "Stream health",
215
+ description: "How well YouTube receives your video. The window also shows what is wrong.",
216
+ category,
217
+ icon: "activity",
218
+ slots,
219
+ display: true,
220
+ look: (_, slot): Look => {
221
+ const name = values.healthName.key;
222
+ const problem = `!!${values.healthProblem.key}`;
223
+ const good = `(${name} == "Good" || ${name} == "OK")`;
224
+ const bad = `${name} == "Bad"`;
225
+ const colors = [
226
+ ["good", "#2ba640", good],
227
+ ["bad", RED, bad],
228
+ ["none", MUTED, `!${good} && !(${bad})`],
229
+ ] as const;
230
+ const wide = slot === "window";
231
+ return {
232
+ background: surface(BACKGROUND),
233
+ layers: [
234
+ ...colors.map(([id, color, visible]) =>
235
+ iconLayer(id, "lucide/activity", {
236
+ color,
237
+ visible,
238
+ size: iconSize(true),
239
+ ...(wide && { frame: { x: 5, y: 20, width: 17, height: 60 } }),
240
+ }),
241
+ ),
242
+ ...(wide
243
+ ? [
244
+ textLayer("label", values.healthName.template, {
245
+ size: 22,
246
+ align: "left",
247
+ frame: { x: 28, y: 20, width: 68, height: 60 },
248
+ visible: `!${problem}`,
249
+ }),
250
+ textLayer("label-small", values.healthName.template, {
251
+ size: 18,
252
+ align: "left",
253
+ frame: { x: 28, y: 10, width: 68, height: 42 },
254
+ visible: problem,
255
+ }),
256
+ textLayer("problem", values.healthProblem.template, {
257
+ size: 12,
258
+ color: MUTED,
259
+ align: "left",
260
+ frame: { x: 28, y: 54, width: 68, height: 34 },
261
+ visible: problem,
262
+ }),
263
+ ]
264
+ : [captionLayer("label", values.healthName.template)]),
265
+ ],
266
+ };
267
+ },
268
+ });
269
+
270
+ export const displays = [status, subscribers, likes, newestVideo, streamHealth];
package/src/index.tsx ADDED
@@ -0,0 +1,58 @@
1
+ import { broadcasts, controls } from "./actions.ts";
2
+ import { displays } from "./displays.ts";
3
+ import { isSignedIn, plugin, signIn, signOut, stopAll } from "./plugin.ts";
4
+ import { state } from "./state.ts";
5
+ import { triggers } from "./triggers.ts";
6
+
7
+ export default plugin.define({
8
+ actions: [...displays, ...controls],
9
+ state,
10
+ triggers,
11
+ sources: [broadcasts],
12
+ commands: [signIn, signOut],
13
+ activate: (context) => {
14
+ if (!isSignedIn()) context.status("Sign in to YouTube", "info");
15
+ return stopAll;
16
+ },
17
+ onboarding: {
18
+ steps: [
19
+ {
20
+ id: "app",
21
+ title: "Use an app of your own",
22
+ optional: true,
23
+ body: [
24
+ "YouTube limits what each app may do in a day, and everyone who signs in through sdeck's shares one limit. For long streams, or chat triggers, make an app of your own. It is free.",
25
+ "In the Google Cloud console, create a project and enable the **YouTube Data API v3** for it. Set up the consent screen for **External** users and press **Publish app**: while an app is only being tested, Google ends your sign-in every week.",
26
+ "Then create a client of the type **Desktop app**, and copy its **Client ID** and **Client secret** here.",
27
+ "When you sign in, Google warns that it has not verified the app. It is your own: press **Advanced**, and go on.",
28
+ ].join("\n\n"),
29
+ link: {
30
+ label: "Google Cloud console",
31
+ url: "https://console.cloud.google.com/apis/library/youtube.googleapis.com",
32
+ },
33
+ settings: ["clientId", "clientSecret"],
34
+ },
35
+ {
36
+ id: "sign-in",
37
+ title: "Sign in",
38
+ body: "Allow access in the browser that opens, with the channel you stream from.",
39
+ command: "sign-in",
40
+ done: isSignedIn,
41
+ },
42
+ ],
43
+ starterPage: {
44
+ name: "YouTube",
45
+ keys: {
46
+ 0: { action: "status" },
47
+ 1: { action: "go-live" },
48
+ 2: { action: "marker", settings: { back: 15 } },
49
+ 3: { action: "health" },
50
+ 4: { action: "subscribers" },
51
+ 5: { action: "likes" },
52
+ 6: { action: "video" },
53
+ 7: { action: "end-poll" },
54
+ },
55
+ window: { action: "status" },
56
+ },
57
+ },
58
+ });
package/src/looks.ts ADDED
@@ -0,0 +1,18 @@
1
+ /** What the plugin's keys have in common. */
2
+ import { iconLook } from "@sdeck/schema";
3
+
4
+ export const category = "YouTube";
5
+ export const slots = ["key", "sensor"] as const;
6
+
7
+ export const BACKGROUND = "#181818";
8
+ export const RED = "#ff0033";
9
+ /** Red that stands out on a dark key. */
10
+ export const CORAL = "#ff5c6c";
11
+ export const MUTED = "#aaaaaa";
12
+
13
+ export const key = (icon: string, caption = "") =>
14
+ iconLook({ icon, caption, background: BACKGROUND, color: CORAL });
15
+
16
+ /** A count: `12,345`, or `123K` and `1.23M` where that would not fit. Nothing until it is known. */
17
+ export const short = (path: string) =>
18
+ `{{${path} == null ? "" : ${path} >= 1000000 ? fmt(${path} / 1000000, 2) + "M" : ${path} >= 100000 ? fmt(${path} / 1000, 0) + "K" : fmt(${path}, 0, ",")}}`;