-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.js
96 lines (83 loc) · 2.59 KB
/
a.js
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
import * as fs from 'node:fs';
export function getOptionsFromLivePage(data) {
let apiKey;
const keyResult = data.match(/['"]INNERTUBE_API_KEY['"]:\s*['"](.+?)['"]/)
if (keyResult) {
apiKey = keyResult[1]
} else {
throw new Error("API Key was not found")
}
let clientVersion;
const verResult = data.match(/['"]clientVersion['"]:\s*['"]([\d.]+?)['"]/)
if (verResult) {
clientVersion = verResult[1]
} else {
throw new Error("Client Version was not found")
}
let continuation;
const continuationResult = data.match(/['"]continuation['"]:\s*['"](.+?)['"]/)
if (continuationResult) {
continuation = continuationResult[1]
} else {
throw new Error("Continuation was not found")
}
return {
apiKey,
clientVersion,
continuation,
}
}
async function fetchChat(options) {
const url = `https://www.youtube.com/youtubei/v1/live_chat/get_live_chat?key=${options.apiKey}`
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({
context: {
client: {
clientVersion: options.clientVersion,
clientName: "WEB",
},
},
continuation: options.continuation,
}),
headers: {
'Content-Type': 'application/json',
},
})
return res.json()
}
const main = async () => {
const liveId = 'SABmm1fK5_0';
const result = await fetch(`https://www.youtube.com/watch?v=${liveId}`);
const text = await result.text();
await fs.promises.writeFile(`/home/aabccd021/tmp/live-${liveId}.html`, text);
const options = getOptionsFromLivePage(text);
console.log(options);
process.on('SIGINT', async () => {
process.exit(0);
});
while (true) {
const data = await fetchChat(options);
const actions = data.continuationContents.liveChatContinuation.actions;
if (actions === undefined) {
console.log(JSON.stringify(json, null, 2));
return;
}
const chats = actions.map((action) => {
const messageRenderer = action?.addChatItemAction?.item?.liveChatTextMessageRenderer;
if (messageRenderer === undefined) {
console.log(JSON.stringify(action, null, 2));
return undefined;
}
return {
message: messageRenderer.message.runs.map((run) => run.text).join(""),
authorName: messageRenderer.authorName.simpleText,
authorPhoto: messageRenderer.authorPhoto.thumbnails[0].url,
timestamp: new Date(parseInt(messageRenderer.timestampUsec)/1000).toLocaleString(),
};
}).filter((chat) => chat !== undefined);
console.log(chats);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
main();