-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.js
154 lines (134 loc) · 4.55 KB
/
services.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
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import io from './index.js';
import { YouTubeLiveChat } from 'youtube-live-chat-ts';
dotenv.config();
const MESSAGES_API_URL = 'https://youtube.googleapis.com/youtube/v3/liveChat/messages';
const { YOUTUBE_CHANNEL_ID, API_KEY } = process.env;
const HELLO = "hello";
const QNA = "qna";
const NORMAL = "normal";
const handler = new YouTubeLiveChat(API_KEY);
let messages = [];
let authors = [];
let _pollingIntervalMillis, _nextPageToken;
let liveChatId = "";
let isFetchingMessages = false;
export function stopFetching() {
liveChatId = "";
messages = [];
}
export async function initialize() {
try {
const currentLiveStreams = await handler.searchChannelForLiveVideoIds(YOUTUBE_CHANNEL_ID);
const videoId = currentLiveStreams[0];
const newLiveChatId = await handler.getLiveChatIdFromVideoId(videoId);
console.log(`New livechatId: ${newLiveChatId}`)
if (newLiveChatId && newLiveChatId !== "") {
if (newLiveChatId !== liveChatId) {
liveChatId = newLiveChatId;
messages = [];
await fetchMessages();
} else if (!isFetchingMessages) {
messages = [];
await fetchMessages(_nextPageToken);
}
return true;
}
} catch (error) {
console.error(error.message);
}
isFetchingMessages = false;
return false;
}
export async function fetchMessages(pageToken = "") {
if(!liveChatId || liveChatId === "") {
console.log("Livechat Id is empty")
return;
}
console.log({ pageToken });
isFetchingMessages = true;
let ENDPOINT =
`${MESSAGES_API_URL}?liveChatId=${liveChatId}&part=snippet,authorDetails&key=${API_KEY}&maxResults=200`;
if (pageToken) {
ENDPOINT += `&pageToken=${pageToken}`;
}
try {
const response = await fetch(ENDPOINT);
const result = await response.json();
const { pollingIntervalMillis, nextPageToken, items } = result;
_pollingIntervalMillis = pollingIntervalMillis;
_nextPageToken = nextPageToken;
if (items && items.length !== 0) {
mapMessages(items);
}
} catch (error) {
console.error(error.message || error.response?.data?.message);
}
setTimeout(async () => {
console.log({ pageToken, _pollingIntervalMillis })
await fetchMessages(_nextPageToken)
}, _pollingIntervalMillis);
}
const mapMessages = (items) => {
for (let index = 0; index < items.length; index++) {
const { id, snippet, authorDetails } = items[index];
const { publishedAt, displayMessage } = snippet;
const { channelId, displayName, profileImageUrl, isChatOwner, isChatSponsor, isChatModerator } = authorDetails;
const existedAuthor = authors.find(({ id }) => id === channelId);
if (!existedAuthor) {
const author = {
id: channelId,
name: displayName,
avatarUrl: profileImageUrl,
// role: "chauongdev"
role: "👶"
};
if (isChatOwner) {
author.role = "👑";
}
if (isChatModerator) {
author.role = "🔧";
}
if (isChatSponsor) {
author.role = "💎";
}
authors.push(author);
io.emit("New author", author);
}
let messageType = NORMAL;
if (displayMessage.includes("!" + HELLO)) {
messageType = HELLO;
} else if (displayMessage.includes("!" + QNA)) {
messageType = QNA;
}
const message = {
id,
text: displayMessage,
authorId: channelId,
publishedAt,
messageType
};
messages.push(message);
io.emit("New message", message);
}
messages.sort((a, b) => +(new Date(a.publishedAt)) - +(new Date(b.publishedAt)));
}
export function getMessages(type) {
switch (type) {
case HELLO:
return messages.filter(({ messageType }) => messageType === HELLO);
case QNA:
return messages.filter(({ messageType }) => messageType === QNA);
case NORMAL:
return messages.filter(({ messageType }) => messageType === NORMAL);
default:
return messages;
}
}
export function getAuthors() {
return authors;
}
export function archiveMessage(messageId) {
messages = messages.filter(({ id }) => id !== messageId);
}