-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleAPI.js
More file actions
269 lines (244 loc) · 7.37 KB
/
Copy pathgoogleAPI.js
File metadata and controls
269 lines (244 loc) · 7.37 KB
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
import fs from "fs";
import { google } from "googleapis";
const OAuth2 = google.auth.OAuth2;
const youtube = google.youtube("v3");
import settings from "./settings.js";
import commands from "./commands.js";
import * as chat from "./servers/chat.js";
import * as fetch from "./servers/fetch.js";
import vtubeAPI from "./vtubeAPI.js";
import "dotenv/config";
const googleAPI = {
oauth2Client: null,
};
/**
* Top level function to connect with an existing token or generate a new one
*/
googleAPI.authorize = () => {
googleAPI.oauth2Client = new OAuth2(
process.env.google_client_id,
process.env.google_client_secret,
process.env.google_redirect_uri
);
fs.readFile("./google_token.txt", (err, tokenJSON) => {
if (err) {
// No google_token.env
console.log("Failed to load google api token. Generating new token...");
generateAuthUrl();
} else {
let token = JSON.parse(tokenJSON);
if (token.expiry_date < Date.now()) {
// Token expired
console.log(
"Existing google api token is expired. Generating new token..."
);
generateAuthUrl();
} else {
// Token good
googleAPI.oauth2Client.setCredentials(token);
console.log("Set existing google api token");
startTracking(token);
}
}
});
};
/**
* Output an auth URL to terminal for user to click
*/
const generateAuthUrl = () => {
const scopes = [
"https://www.googleapis.com/auth/youtube.readonly",
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/youtube.force-ssl",
];
let authUrl = googleAPI.oauth2Client.generateAuthUrl({
access_type: "offline",
scope: scopes,
});
console.log("Authorize this app by visiting this url: ", authUrl);
};
/**
* Take code from redirect URL and generate and store a respective token
*
* @param {String} code Code from redirect URI
*/
googleAPI.getTokenFromCode = (code) => {
googleAPI.oauth2Client.getToken(code, (err, token) => {
if (err) {
console.error(
"Error while trying to retrieve google api access token",
err
);
return;
}
googleAPI.oauth2Client.setCredentials(token);
storeToken(token);
console.log("Set new google api token and saved to token.json");
startTracking();
});
};
/**
* Saves a token to google_token.env
*
* @param {String} token Token from getTokenFromCode
*/
const storeToken = (token) => {
fs.writeFile("./google_token.txt", JSON.stringify(token), (err) => {
if (err) return console.error("Error storing google api token", err);
console.log("Google API Token stored to google_token.txt");
});
};
/**
* Once client is connected, setup listener for new tokens and intervals for api calls
*/
const startTracking = () => {
googleAPI.oauth2Client.on("tokens", (token) => {
console.log("New token received");
storeToken(token);
});
getChannel();
findActiveChat();
};
/**
* Once client is connected, call api to find chat id
*/
const findActiveChat = () => {
youtube.liveBroadcasts.list(
{
auth: googleAPI.oauth2Client,
part: "snippet",
mine: "true",
},
(err, response) => {
if (err) {
console.log(err);
console.log(
`Couldn't find active chat. Trying again in ${
settings.findChatInterval / 1000
} seconds`
);
setTimeout(findActiveChat, settings.findChatInterval);
} else {
const latestChat = response.data.items[0];
let liveChatId = latestChat.snippet.liveChatId;
console.log("Chat ID found", liveChatId);
/*chat.io.emit('chat_event', {
id: "1",
authorDetails: {
isChatOwner: true,
isChatModerator: false,
isChatSponsor: false,
displayName: "solderpup"
},
snippet: {
type: "textMessageEvent",
publishedAt: "00000000000000000000",
textMessageDetails: {
messageText: "hello world"
}
}
})*/
getChat(liveChatId, null);
}
}
);
};
/**
* Fetch channel info and push to fetch window
*/
const getChannel = () => {
console.log("Getting channel...");
youtube.channels.list(
{
auth: googleAPI.oauth2Client,
part: "snippet,contentDetails,statistics",
mine: true,
},
(err, response) => {
if (err) return console.error("Error getting channel data", err);
//console.log('channel: ', response.data.items[0])
fetch.io.emit("channelUpdate", response.data.items[0]);
}
);
setTimeout(getChannel, settings.channelInterval);
};
/**
* Fetch chat events using global liveChatId and nextPage variables, and push to chat, fetch, and dunst windows
*
* @param {String} liveChatId User's chat ID from liveBroadcats.list
* @param {String} nextPage nextPageToken from liveChatMessages.list
*/
const getChat = (liveChatId, nextPage) => {
console.log("Getting chat...");
youtube.liveChatMessages.list(
{
auth: googleAPI.oauth2Client,
part: ["snippet", "id", "authorDetails"],
liveChatId,
pageToken: nextPage,
},
(err, response) => {
if (err) {
console.log(
`Couldn't find active chat. Trying again in ${
settings.findChatInterval / 1000
} seconds`
);
setTimeout(
() => getChat(liveChatId, nextPage),
settings.findChatInterval
);
} else {
const newChatEvents = response.data.items;
newChatEvents.forEach((chatEvent) => {
//console.log('chat event: ', event)
chat.io.emit("chat_event", chatEvent);
fetch.io.emit("chat_event", chatEvent);
//vtube superchat (depracated)
/*if (chatEvent.type == "superChatEvent") {
vtubeAPI.parseSuperchat(chatEvent)
}*/
//commands
if (
nextPage &&
Object.hasOwn(chatEvent.snippet, "displayMessage") &&
chatEvent.snippet.displayMessage &&
chatEvent.snippet.displayMessage.includes("!")
) {
let split = chatEvent.snippet.displayMessage.split();
console.log(`split message with !: ${split}`);
split.forEach((word, index) => {
if (word[0] == "!" && commands.has(word.slice(1))) {
let message = commands.get(word.slice(1))();
if (message) {
//Send Message in Chat
let request = {
auth: googleAPI.oauth2Client,
part: "snippet",
resource: {
snippet: {
liveChatId,
type: "textMessageEvent",
textMessageDetails: {
messageText: message,
},
},
},
};
youtube.liveChatMessages
.insert(request)
.then(`Executed command ${word}`);
}
}
});
}
});
setTimeout(
() => getChat(liveChatId, response.data.nextPageToken),
settings.chatInterval
);
}
}
);
};
export default googleAPI;