-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsuperchat.ts
69 lines (61 loc) · 1.83 KB
/
superchat.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
import { stringify } from "../utils";
import { YTLiveChatPaidMessageRenderer, YTText } from "../interfaces/yt/chat";
import {
SuperChat,
SUPERCHAT_COLOR_MAP,
SUPERCHAT_SIGNIFICANCE_MAP,
} from "../interfaces/misc";
import { parseColorCode } from "./utils";
const AMOUNT_REGEXP = /[\d.,]+/;
const SYMBOL_TO_TLS_MAP: Record<string, string> = {
$: "USD",
"£": "GBP",
"¥": "JPY",
"JP¥": "JPY",
"₩": "KRW",
"₪": "ILS",
"€": "EUR",
"₱": "PHP",
"₹": "INR",
A$: "AUD",
CA$: "CAD",
HK$: "HKD",
MX$: "MXN",
NT$: "TWD",
NZ$: "NZD",
R$: "BRL",
};
export function toTLS(symbolOrTls: string): string {
return SYMBOL_TO_TLS_MAP[symbolOrTls] ?? symbolOrTls;
}
export function parseAmountText(purchaseAmountText: string) {
const input = stringify(purchaseAmountText);
const amountString = AMOUNT_REGEXP.exec(input)![0].replace(/,/g, "");
const amount = parseFloat(amountString);
const currency = toTLS(input.replace(AMOUNT_REGEXP, "").trim());
return { amount, currency };
}
export function parseSuperChat(
renderer: YTLiveChatPaidMessageRenderer
): SuperChat {
const { amount, currency } = parseAmountText(
renderer.purchaseAmountText.simpleText
);
const color =
SUPERCHAT_COLOR_MAP[
renderer.headerBackgroundColor.toString() as keyof typeof SUPERCHAT_COLOR_MAP
];
const significance = SUPERCHAT_SIGNIFICANCE_MAP[color];
return {
amount,
currency,
color,
significance,
authorNameTextColor: parseColorCode(renderer.authorNameTextColor),
timestampColor: parseColorCode(renderer.timestampColor),
headerBackgroundColor: parseColorCode(renderer.headerBackgroundColor),
headerTextColor: parseColorCode(renderer.headerTextColor),
bodyBackgroundColor: parseColorCode(renderer.bodyBackgroundColor),
bodyTextColor: parseColorCode(renderer.bodyTextColor),
};
}