-
Notifications
You must be signed in to change notification settings - Fork 8
/
youtubeService.js
161 lines (137 loc) · 4.35 KB
/
youtubeService.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
const { google } = require('googleapis');
// Put the following at the top of the file
// right below the'googleapis' import
const util = require('util');
const fs = require('fs');
let liveChatId; // Where we'll store the id of our liveChat
let nextPage; // How we'll keep track of pagination for chat messages
const intervalTime = 5000; // Miliseconds between requests to check chat messages
let interval; // variable to store and control the interval that will check messages
let chatMessages = []; // where we'll store all messages
const writeFilePromise = util.promisify(fs.writeFile);
const readFilePromise = util.promisify(fs.readFile);
const save = async (path, str) => {
await writeFilePromise(path, str);
console.log('Successfully Saved');
};
const read = async path => {
const fileContents = await readFilePromise(path);
return JSON.parse(fileContents);
};
const youtube = google.youtube('v3');
const OAuth2 = google.auth.OAuth2;
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const redirectURI = 'http://localhost:3000/callback';
// Permissions needed to view and submit live chat comments
const scope = [
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.force-ssl'
];
const auth = new OAuth2(clientId, clientSecret, redirectURI);
const youtubeService = {};
youtubeService.getCode = response => {
const authUrl = auth.generateAuthUrl({
access_type: 'offline',
scope
});
response.redirect(authUrl);
};
// Request access from tokens using code from login
youtubeService.getTokensWithCode = async code => {
const credentials = await auth.getToken(code);
youtubeService.authorize(credentials);
};
// Storing access tokens received from google in auth object
youtubeService.authorize = ({ tokens }) => {
auth.setCredentials(tokens);
console.log('Successfully set credentials');
console.log('tokens:', tokens);
save('./tokens.json', JSON.stringify(tokens));
};
youtubeService.findActiveChat = async () => {
const response = await youtube.liveBroadcasts.list({
auth,
part: 'snippet',
mine: 'true'
});
const latestChat = response.data.items[0];
if (latestChat && latestChat.snippet.liveChatId) {
liveChatId = latestChat.snippet.liveChatId;
console.log("Chat ID Found:", liveChatId);
} else {
console.log("No Active Chat Found");
}
};
// Update the tokens automatically when they expire
auth.on('tokens', tokens => {
if (tokens.refresh_token) {
// store the refresh_token in my database!
save('./tokens.json', JSON.stringify(auth.tokens));
console.log(tokens.refresh_token);
}
console.log(tokens.access_token);
});
// Read tokens from stored file
const checkTokens = async () => {
const tokens = await read('./tokens.json');
if (tokens) {
auth.setCredentials(tokens);
console.log('tokens set');
} else {
console.log('no tokens set');
}
};
const respond = newMessages => {
newMessages.forEach(message => {
const messageText = message.snippet.displayMessage.toLowerCase();
if (messageText.includes('thank')) {
const author = message.authorDetails.displayName;
const response = `You're welcome ${author}!`;
youtubeService.insertMessage(response);
}
});
};
const getChatMessages = async () => {
const response = await youtube.liveChatMessages.list({
auth,
part: 'snippet,authorDetails',
liveChatId,
pageToken: nextPage
});
const { data } = response;
const newMessages = data.items;
chatMessages.push(...newMessages);
nextPage = data.nextPageToken;
console.log('Total Chat Messages:', chatMessages.length);
respond(newMessages);
};
youtubeService.startTrackingChat = () => {
interval = setInterval(getChatMessages, intervalTime);
};
youtubeService.stopTrackingChat = () => {
clearInterval(interval);
};
youtubeService.insertMessage = messageText => {
youtube.liveChatMessages.insert(
{
auth,
part: 'snippet',
resource: {
snippet: {
type: 'textMessageEvent',
liveChatId,
textMessageDetails: {
messageText
}
}
}
},
() => {}
);
};
checkTokens();
// As we progress throug this turtorial, Keep the following line at the nery bottom of the file
// It will allow other files to access to our functions
module.exports = youtubeService;