-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeChatBotModule.js
200 lines (178 loc) · 5.62 KB
/
youtubeChatBotModule.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
import {google} from 'googleapis';
import {OAuth2Client} from 'google-auth-library';
import http from 'http';
import url from 'url';
import open from 'open';
import destroyer from 'server-destroy';
import FS from 'fs';
class YoutubeChat {
authorized = false;
constructor(oauthPath, tokensPath, serverCallbackHTTP){
this.serverCallbackHTTP = serverCallbackHTTP || 'http://localhost:3000';
try{
let keysRaw = FS.readFileSync(oauthPath, 'utf8');
this.keys = JSON.parse(keysRaw);
}
catch(err){
console.error(err);
}
this.tokensPath = tokensPath
this.oauthStart();
}
async oauthStart(){
this.oAuth2Client = await this.getAuthenticatedClient();
this.api = google.youtube({ version: 'v3', auth: this.oAuth2Client });
this.authorized = true;
}
async getAuthenticatedClient() {
return new Promise(async (resolve, reject) => {
let refreshing = false;
let refreshed = false;
let cachedTokens;
try{
if(FS.existsSync(this.tokensPath)) {
cachedTokens = FS.readFileSync('tokens', 'utf8');
refreshing = true;
}
}
catch(err) {
console.err(err);
}
const oAuth2Client = new OAuth2Client(
this.keys.web.client_id,
this.keys.web.client_secret,
this.keys.web.redirect_uris[0],
);
if(refreshing) {
oAuth2Client.setCredentials({
refresh_token: cachedTokens
})
await oAuth2Client.refreshAccessToken()
refreshed = true;
}
if (refreshed){
resolve(oAuth2Client);
return;
}
// Generate the url that will be used for the consent dialog.
const authorizeUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: 'https://www.googleapis.com/auth/youtube.force-ssl',
});
// Open an http server to accept the oauth callback. In this simple example, the
// only request to our webserver is to /oauth2callback?code=<code>
const server = http
.createServer(async (req, res) => {
try {
if (req.url.indexOf('/oauth2callback') > -1) {
// acquire the code from the querystring, and close the web server.
const qs = new url.URL(req.url, this.serverCallbackHTTP)
.searchParams;
const code = qs.get('code');
console.log(`Code is ${code}`);
res.end('Authentication successful! Please return to the console.');
server.destroy();
// Now that we have the code, use that to acquire tokens.
const r = await oAuth2Client.getToken(code);
// Make sure to set the credentials on the OAuth2 client.
oAuth2Client.setCredentials(r.tokens);
try{
FS.writeFileSync(this.tokensPath, r.tokens.refresh_token);
}
catch(err){
console.error(err)
}
console.info('Tokens acquired.');
resolve(oAuth2Client);
}
} catch (e) {
reject(e);
}
})
.listen(3000, () => {
// open the browser to the authorize url to start the workflow
console.log(`If the browser cannot be opened on your system, go here:`)
console.log(authorizeUrl);
open(authorizeUrl, {wait: false}).then(cp => cp.unref());
});
destroyer(server);
});
}
async sendMessage(msg, chatId) {
await this.checkAuthorization();
this.api.liveChatMessages.insert({
"part":'snippet',
"resource":{
"snippet":{
"type":"textMessageEvent",
"liveChatId": chatId,
"textMessageDetails":{
"messageText": msg
}
}
}
}, function(err,response){
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log(response);
}
});
}
async getChatId(streamid){
await this.checkAuthorization();
const list = await this.api.videos.list({
"part": [
"liveStreamingDetails"
],
"id": [
streamid
]
})
return list.data.items[0].liveStreamingDetails.activeLiveChatId;
}
async getLatestLiveStream(channelId){
await this.checkAuthorization();
channelId = await this.checkChannel(channelId);
const list = await this.api.search.list({
"part": "snippet",
"channelId": channelId,
"eventType": "live",
"type": "video"
})
return list.data.items[0].id.videoId;
}
async getChannelId(channelName) {
await this.checkAuthorization();
const list = await this.api.search.list({
"part": "snippet",
"q": channelName,
"type": "channel"
})
return list.data.items[0].id.channelId
}
async getLatestChatId(channelId){
let livestreamId = await this.getLatestLiveStream(channelId);
let chatId = await this.getChatId(livestreamId);
return chatId;
}
async sendMessageToChannel(msg, channelId) {
let chatId = await this.getLatestChatId(channelId);
this.sendMessage(msg, chatId);
}
async checkAuthorization() {
if(!this.authorized){
while(!this.authorized){
await new Promise(r => setTimeout(r, 1000));//block the function until we're authorized.
}
}
}
async checkChannel(channelName){
if(channelName.length == 24 && channelName.startsWith("UC")){
return channelName;
}
return this.getChannelId(channelName);
}
}
export default YoutubeChat;