/
addChatItemAction.ts
570 lines (513 loc) · 16.8 KB
/
addChatItemAction.ts
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
import {
AddChatItemAction,
AddMembershipItemAction,
AddMembershipMilestoneItemAction,
AddPlaceholderItemAction,
AddSuperChatItemAction,
AddSuperStickerItemAction,
AddViewerEngagementMessageAction,
LiveChatMode,
ModeChangeAction,
AddPollResultAction,
MembershipGiftPurchaseAction,
MembershipGiftRedemptionAction,
MembershipGiftPurchaseTickerContent,
ModerationMessageAction,
} from "../../interfaces/actions";
import {
YTAddChatItemAction,
YTLiveChatMembershipItemRenderer,
YTLiveChatModeChangeMessageRenderer,
YTLiveChatModerationMessageRenderer,
YTLiveChatPaidMessageRenderer,
YTLiveChatPaidStickerRenderer,
YTLiveChatPlaceholderItemRenderer,
YTLiveChatSponsorshipsGiftPurchaseAnnouncementRenderer,
YTLiveChatSponsorshipsGiftRedemptionAnnouncementRenderer,
YTLiveChatTextMessageRenderer,
YTLiveChatViewerEngagementMessageRenderer,
YTRun,
YTRunContainer,
YTTextRun,
} from "../../interfaces/yt/chat";
import {
debugLog,
durationToSeconds,
splitRunsByNewLines,
stringify,
tsToDate,
} from "../../utils";
import { parseBadges, parseMembership } from "../badge";
import { parseAmountText, parseSuperChat } from "../superchat";
import { parseColorCode, pickThumbUrl } from "../utils";
export function parseAddChatItemAction(payload: YTAddChatItemAction) {
const { item } = payload;
if ("liveChatTextMessageRenderer" in item) {
// Chat
const renderer = item["liveChatTextMessageRenderer"]!;
return parseLiveChatTextMessageRenderer(renderer);
} else if ("liveChatPaidMessageRenderer" in item) {
// Super Chat
const renderer = item["liveChatPaidMessageRenderer"]!;
return parseLiveChatPaidMessageRenderer(renderer);
} else if ("liveChatPaidStickerRenderer" in item) {
// Super Sticker
const renderer = item["liveChatPaidStickerRenderer"]!;
return parseLiveChatPaidStickerRenderer(renderer);
} else if ("liveChatMembershipItemRenderer" in item) {
// Membership updates
const renderer = item["liveChatMembershipItemRenderer"]!;
return parseLiveChatMembershipItemRenderer(renderer);
} else if ("liveChatViewerEngagementMessageRenderer" in item) {
// Engagement message
const renderer = item["liveChatViewerEngagementMessageRenderer"]!;
return parseLiveChatViewerEngagementMessageRenderer(renderer);
} else if ("liveChatPlaceholderItemRenderer" in item) {
// Placeholder chat
const renderer = item["liveChatPlaceholderItemRenderer"]!;
return parseLiveChatPlaceholderItemRenderer(renderer);
} else if ("liveChatModeChangeMessageRenderer" in item) {
// Mode change message (e.g. toggle members-only)
const renderer = item["liveChatModeChangeMessageRenderer"]!;
return parseLiveChatModeChangeMessageRenderer(renderer);
} else if ("liveChatSponsorshipsGiftPurchaseAnnouncementRenderer" in item) {
// Sponsorships gift purchase announcement
const renderer =
item["liveChatSponsorshipsGiftPurchaseAnnouncementRenderer"];
return parseLiveChatSponsorshipsGiftPurchaseAnnouncementRenderer(
renderer
) as MembershipGiftPurchaseAction;
} else if ("liveChatSponsorshipsGiftRedemptionAnnouncementRenderer" in item) {
// Sponsorships gift purchase announcement
const renderer =
item["liveChatSponsorshipsGiftRedemptionAnnouncementRenderer"];
return parseLiveChatSponsorshipsGiftRedemptionAnnouncementRenderer(
renderer
);
} else if ("liveChatModerationMessageRenderer" in item) {
const renderer = item["liveChatModerationMessageRenderer"];
return parseLiveChatModerationMessageRenderer(renderer);
}
debugLog(
"[action required] Unrecognized chat item renderer type:",
JSON.stringify(item)
);
}
// Chat
export function parseLiveChatTextMessageRenderer(
renderer: YTLiveChatTextMessageRenderer
) {
const {
id,
timestampUsec,
authorExternalChannelId: authorChannelId,
} = renderer;
const timestamp = tsToDate(timestampUsec);
const authorName = renderer.authorName
? stringify(renderer.authorName)
: undefined;
const authorPhoto =
renderer.authorPhoto.thumbnails[renderer.authorPhoto.thumbnails.length - 1]
.url;
const { isVerified, isOwner, isModerator, membership } =
parseBadges(renderer);
const contextMenuEndpointParams =
renderer.contextMenuEndpoint!.liveChatItemContextMenuEndpoint.params;
if (renderer.authorName && !("simpleText" in renderer.authorName)) {
debugLog(
"[action required] non-simple authorName (live chat):",
JSON.stringify(renderer.authorName)
);
}
// message can somehow be a blank object (in quite rare occasion though)
const message = renderer.message.runs as YTRun[] | undefined;
const parsed: AddChatItemAction = {
type: "addChatItemAction",
id,
timestamp,
timestampUsec,
authorName,
authorChannelId,
authorPhoto,
message,
membership,
isVerified,
isOwner,
isModerator,
contextMenuEndpointParams,
rawMessage: message, // deprecated
};
return parsed;
}
// Super Chat
export function parseLiveChatPaidMessageRenderer(
renderer: YTLiveChatPaidMessageRenderer
) {
const { timestampUsec, authorExternalChannelId: authorChannelId } = renderer;
const timestamp = tsToDate(timestampUsec);
const authorName = stringify(renderer.authorName);
const authorPhoto = pickThumbUrl(renderer.authorPhoto);
if (renderer.authorName && !("simpleText" in renderer.authorName)) {
debugLog(
"[action required] non-simple authorName (super chat):",
JSON.stringify(renderer.authorName)
);
}
const message = renderer.message?.runs ?? null;
const superchat = parseSuperChat(renderer);
const parsed: AddSuperChatItemAction = {
type: "addSuperChatItemAction",
id: renderer.id,
timestamp,
timestampUsec,
authorName,
authorChannelId,
authorPhoto,
message,
...superchat,
superchat, // deprecated
rawMessage: renderer.message?.runs, // deprecated
};
return parsed;
}
// Super Sticker
export function parseLiveChatPaidStickerRenderer(
rdr: YTLiveChatPaidStickerRenderer
): AddSuperStickerItemAction {
const { timestampUsec, authorExternalChannelId: authorChannelId } = rdr;
const timestamp = tsToDate(timestampUsec);
const authorName = stringify(rdr.authorName);
const authorPhoto = pickThumbUrl(rdr.authorPhoto);
if (!authorName) {
debugLog(
"[action required] empty authorName (super sticker)",
JSON.stringify(rdr)
);
}
const stickerUrl = "https:" + pickThumbUrl(rdr.sticker);
const stickerText = rdr.sticker.accessibility!.accessibilityData.label;
const { amount, currency } = parseAmountText(
rdr.purchaseAmountText.simpleText
);
const parsed: AddSuperStickerItemAction = {
type: "addSuperStickerItemAction",
id: rdr.id,
timestamp,
timestampUsec,
authorName,
authorChannelId,
authorPhoto,
stickerUrl,
stickerText,
amount,
currency,
stickerDisplayWidth: rdr.stickerDisplayWidth,
stickerDisplayHeight: rdr.stickerDisplayHeight,
moneyChipBackgroundColor: parseColorCode(rdr.moneyChipBackgroundColor),
moneyChipTextColor: parseColorCode(rdr.moneyChipTextColor),
backgroundColor: parseColorCode(rdr.backgroundColor),
authorNameTextColor: parseColorCode(rdr.authorNameTextColor),
};
return parsed;
}
// Membership
export function parseLiveChatMembershipItemRenderer(
renderer: YTLiveChatMembershipItemRenderer
) {
const id = renderer.id;
const timestampUsec = renderer.timestampUsec;
const timestamp = tsToDate(timestampUsec);
const authorName = renderer.authorName
? stringify(renderer.authorName)
: undefined;
const authorChannelId = renderer.authorExternalChannelId;
const authorPhoto = pickThumbUrl(renderer.authorPhoto);
// observed, MODERATOR
// observed, undefined renderer.authorBadges
const membership = renderer.authorBadges
? parseMembership(renderer.authorBadges[renderer.authorBadges.length - 1])
: undefined;
if (!membership) {
debugLog(
`missing membership information while parsing neww membership action: ${JSON.stringify(
renderer
)}`
);
}
const isMilestoneMessage = "empty" in renderer || "message" in renderer;
if (isMilestoneMessage) {
const message = renderer.message ? renderer.message.runs : null;
const durationText = renderer
.headerPrimaryText!.runs.slice(1)
.map((r) => r.text)
.join("");
// duration > membership.since
// e.g. 12 months > 6 months
const duration = durationToSeconds(durationText);
const level = renderer.headerSubtext
? stringify(renderer.headerSubtext)
: undefined;
const parsed: AddMembershipMilestoneItemAction = {
type: "addMembershipMilestoneItemAction",
id,
timestamp,
timestampUsec,
authorName,
authorChannelId,
authorPhoto,
membership,
level,
message,
duration,
durationText,
};
return parsed;
} else {
/**
* no level -> ["New Member"]
* multiple levels -> ["Welcome", "<level>", "!"]
*/
const subRuns = (renderer.headerSubtext as YTRunContainer<YTTextRun>).runs;
const level = subRuns.length > 1 ? subRuns[1].text : undefined;
const parsed: AddMembershipItemAction = {
type: "addMembershipItemAction",
id,
timestamp,
timestampUsec,
authorName,
authorChannelId,
authorPhoto,
membership,
level,
};
return parsed;
}
}
// Engagement message
export function parseLiveChatViewerEngagementMessageRenderer(
renderer: YTLiveChatViewerEngagementMessageRenderer
) {
/**
* YOUTUBE_ROUND: engagement message
* POLL: poll result message
*/
const {
id,
timestampUsec,
icon: { iconType },
} = renderer;
if ("simpleText" in renderer.message) {
debugLog(
"[action required] message is simpleText (engagement):",
JSON.stringify(renderer)
);
}
switch (iconType) {
case "YOUTUBE_ROUND": {
const timestamp = tsToDate(timestampUsec!);
const actionUrl =
renderer.actionButton?.buttonRenderer.navigationEndpoint.urlEndpoint
.url;
const parsed: AddViewerEngagementMessageAction = {
type: "addViewerEngagementMessageAction",
id,
message: renderer.message,
actionUrl,
timestamp,
timestampUsec: timestampUsec!,
};
return parsed;
}
case "POLL": {
// [{"id":"ChkKF1hTbnRZYzNTQk91R2k5WVA1cDJqd0FV","message":{"runs":[{"text":"生まれは?","bold":true},{"text":"\n"},{"text":"平成 (80%)"},{"text":"\n"},{"text":"昭和 (19%)"},{"text":"\n"},{"text":"\n"},{"text":"Poll complete: 84 votes"}]},"messageType":"poll","type":"addViewerEngagementMessageAction","originVideoId":"1SzuFU7t450","originChannelId":"UC3Z7UaEe_vMoKRz9ABQrI5g"}]
// <!> addViewerEngagementMessageAction [{"id":"ChkKF3VDX3RZWS1PQl95QWk5WVBrUGFENkFz","message":{"runs":[{"text":"2 (73%)"},{"text":"\n"},{"text":"4 (26%)"},{"text":"\n"},{"text":"\n"},{"text":"Poll complete: 637 votes"}]},"messageType":"poll","type":"addViewerEngagementMessageAction","originVideoId":"8sne4hKHNeo","originChannelId":"UC2hc-00y-MSR6eYA4eQ4tjQ"}]
// Poll complete: 637 votes
// Poll complete: 1.9K votes
// split runs by {"text": "\n"}
// has question: {text: "...", "bold": true}, {emoji: ...}
// {emoji: ...}, {text: "...", "bold": true}
// otherwise: {emoji: ...}, {text: " (\d%)"}
const runs = (renderer.message as YTRunContainer<YTTextRun>).runs;
const runsNL = splitRunsByNewLines(runs);
const hasQuestion = runsNL[0].some(
(run) => "bold" in run && run.bold == true
);
const question = hasQuestion ? runsNL[0] : undefined;
const total = /: (.+?) vote/.exec(runs[runs.length - 1].text)![1];
const choiceStart = hasQuestion ? 1 : 0;
const choices = runsNL.slice(choiceStart, -2).map((choiceRuns) => {
const last = choiceRuns[choiceRuns.length - 1] as YTTextRun;
const [lastTextFragment, votePercentage] = /(?:^(.+?))? \((\d+%)\)$/
.exec(last.text)!
.slice(1);
let text = choiceRuns;
if (lastTextFragment) {
(text[text.length - 1] as YTTextRun).text = lastTextFragment;
} else {
text.pop();
}
return { text, votePercentage };
});
const parsed: AddPollResultAction = {
type: "addPollResultAction",
id,
question,
total,
choices,
};
return parsed;
}
default:
debugLog(
"[action required] unknown icon type (engagement message):",
JSON.stringify(renderer)
);
}
}
// Placeholder chat
export function parseLiveChatPlaceholderItemRenderer(
renderer: YTLiveChatPlaceholderItemRenderer
) {
const id = renderer.id;
const timestampUsec = renderer.timestampUsec;
const timestamp = tsToDate(timestampUsec);
const parsed: AddPlaceholderItemAction = {
type: "addPlaceholderItemAction",
id,
timestamp,
timestampUsec,
};
return parsed;
}
// Mode change message
export function parseLiveChatModeChangeMessageRenderer(
renderer: YTLiveChatModeChangeMessageRenderer
) {
const text = stringify(renderer.text);
const description = stringify(renderer.subtext);
let mode = LiveChatMode.Unknown;
if (/Slow mode/.test(text)) {
mode = LiveChatMode.Slow;
} else if (/Members-only mode/.test(text)) {
mode = LiveChatMode.MembersOnly;
} else if (/subscribers-only/.test(text)) {
mode = LiveChatMode.SubscribersOnly;
} else {
debugLog(
"[action required] Unrecognized mode (modeChangeAction):",
JSON.stringify(renderer)
);
}
const enabled = /(is|turned) on/.test(text);
const parsed: ModeChangeAction = {
type: "modeChangeAction",
mode,
enabled,
description,
};
return parsed;
}
// Sponsorships gift purchase announcement
export function parseLiveChatSponsorshipsGiftPurchaseAnnouncementRenderer(
renderer: YTLiveChatSponsorshipsGiftPurchaseAnnouncementRenderer
): MembershipGiftPurchaseAction | MembershipGiftPurchaseTickerContent {
const id = renderer.id;
/** timestampUsec can be undefined when passed from ticker action */
const timestampUsec = renderer.timestampUsec;
const timestamp = timestampUsec ? tsToDate(timestampUsec) : undefined;
const authorChannelId = renderer.authorExternalChannelId;
const header = renderer.header.liveChatSponsorshipsHeaderRenderer;
const authorName = stringify(header.authorName);
const authorPhoto = pickThumbUrl(header.authorPhoto);
const channelName = header.primaryText.runs[3].text;
const amount = parseInt(header.primaryText.runs[1].text, 10);
const image = header.image.thumbnails[0].url;
if (!authorName) {
debugLog(
"[action required] empty authorName (gift purchase)",
JSON.stringify(renderer)
);
}
const membership = parseMembership(
header.authorBadges[header.authorBadges.length - 1]
)!;
if (!membership) {
debugLog(
"[action required] empty membership (gift purchase)",
JSON.stringify(renderer)
);
}
if (!timestampUsec || !timestamp) {
const tickerContent: MembershipGiftPurchaseTickerContent = {
id,
channelName,
amount,
membership,
authorName,
authorChannelId,
authorPhoto,
image,
};
return tickerContent;
}
const parsed: MembershipGiftPurchaseAction = {
type: "membershipGiftPurchaseAction",
id,
timestamp,
timestampUsec,
channelName,
amount,
membership,
authorName,
authorChannelId,
authorPhoto,
image,
};
return parsed;
}
// Sponsorships gift redemption announcement
export function parseLiveChatSponsorshipsGiftRedemptionAnnouncementRenderer(
renderer: YTLiveChatSponsorshipsGiftRedemptionAnnouncementRenderer
) {
const id = renderer.id;
const timestampUsec = renderer.timestampUsec;
const timestamp = tsToDate(timestampUsec);
const authorChannelId = renderer.authorExternalChannelId;
const authorName = stringify(renderer.authorName);
const authorPhoto = pickThumbUrl(renderer.authorPhoto);
const senderName = renderer.message.runs[1].text;
if (!authorName) {
debugLog(
"[action required] empty authorName (gift redemption)",
JSON.stringify(renderer)
);
}
const parsed: MembershipGiftRedemptionAction = {
type: "membershipGiftRedemptionAction",
id,
timestamp,
timestampUsec,
senderName,
authorName,
authorChannelId,
authorPhoto,
};
return parsed;
}
// Moderation message
export function parseLiveChatModerationMessageRenderer(
renderer: YTLiveChatModerationMessageRenderer
) {
const id = renderer.id;
const timestampUsec = renderer.timestampUsec;
const timestamp = tsToDate(timestampUsec);
const message = renderer.message.runs;
const parsed: ModerationMessageAction = {
type: "moderationMessageAction",
id,
timestamp,
timestampUsec,
message,
};
return parsed;
}