-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt-chat-parser.js
227 lines (201 loc) · 6.85 KB
/
yt-chat-parser.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
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
require('dotenv').config();
const fs = require('fs');
const readline = require('readline');
const { google } = require('googleapis');
const OAuth2 = google.auth.OAuth2;
const service = google.youtube('v3');
const { BehaviorSubject } = require('rxjs');
const { LiveChat } = require('youtube-chat');
const { translateInput } = require('./input-mapper');
const SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'];
let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.super-secrets/';
const TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json';
const CONTROLLER = process.env.YOUTUBE_CONTROLLER;
const VIDEO_ID = process.env.LIVE_VIDEO_ID;
const API_KEY = process.env.API_KEY;
const liveChat = new LiveChat({liveId: VIDEO_ID});
let authed;
const chatInput$ = new BehaviorSubject();
let videoInformation = {
channel: undefined,
chatId: undefined,
videoTitle: undefined,
pollingInterval: 10000,
}
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the YouTube API.
authorize(JSON.parse(content), getVideoDetails);
});
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
};
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
function getNewToken(oauth2Client, callback) {
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
};
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) throw err;
console.log('Token stored to ' + TOKEN_PATH);
});
};
/**
* Gets information related to a particular video.
* Must be authenticated in-order to make the call.
*
* @param {google.auth.OAuth2} auth The OAuth2 token.
*
*/
function getVideoDetails(auth) {
service.videos.list({
auth: auth,
id: VIDEO_ID,
part: 'snippet,liveStreamingDetails'
}, function(err, response) {
if (err) {
console.error('Encountered an error retrieving video details: ' + err);
return;
}
const videoDetails = response.data.items[0];
videoInformation.channel = videoDetails.snippet.channelTitle
videoInformation.videoTitle = videoDetails.snippet.title;
videoInformation.videoId = videoDetails.id;
videoInformation.chatId = videoDetails.liveStreamingDetails.activeLiveChatId;
if (videoDetails === 0) {
console.error(`No video with id ${VIDEO_ID} was found.`)
} else {
console.log('\n\n======= Source =======\n Channel = %s\n Video = %s\n Video ID = %s\n Live Chat Id = %s\n======================\n\n',
videoInformation.channel,
videoInformation.videoTitle,
videoInformation.videoId,
videoInformation.chatId);
getLiveChat(videoInformation.chatId);
}
});
};
/**
* Gets information from a live chat instance, on a live video.
* Must be authenticated in-order to make the call.
*
* @param liveChat the live chat session id of a live video.
*
*/
function getLiveChat(liveChat) {
service.liveChatMessages.list({
auth: authed,
key: API_KEY,
liveChatId: liveChat,
part: 'snippet'
}, function(err, response) {
if (err) {
console.error('Encountered an error retrieving liveChat log: ' + err);
return;
}
const liveChatDetails = response.data;
const nextPageToken = liveChatDetails.nextPageToken;
if (liveChatDetails === 0) {
console.error(`No chat with id ${liveChat} was found.`);
} else {
handleLiveChatEvents(liveChat, nextPageToken);
}
});
};
function handleLiveChatEvents() {
liveChat.on('start', (liveId) => console.log(`handling liveChats from live video id: ${liveId}`));
liveChat.on("error", (err) => {
console.log('LiveChat error: ' + err);
})
liveChat.on("end", (reason) => {
console.log('LiveChat ended: ' + reason);
})
liveChat.on('chat', (chatItem) => {
const author = chatItem.author?.name ? chatItem.author.name : 'unknown';
const time = chatItem.timestamp;
const filteredMessageText = chatItem.message.filter(el => el.text).flatMap(el => el?.text.trim());
const text = filteredMessageText.join(' ');
const chat = {
author: author,
time: time,
message: text
};
if (chat.message != '') chatInput$.next(chat);
});
const run = liveChat.start();
if (!run) console.error('Failed to start; check emitted error');
};
/**
* emit keyboard events
*
* @param {Array} input BehaviourSubject's value
*/
function actionAvatar(input) {
const key = input.message;
translateInput(key, input.author, CONTROLLER);
};
chatInput$.subscribe((x) => {
if (x != null) actionAvatar(chatInput$.getValue());
});