-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
118 lines (110 loc) · 3.02 KB
/
index.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
const request = require('request')
const {EventEmitter} = require('events')
/**
* The main hub for acquire live chat with the YouTube Date API.
* @extends {EventEmitter}
*/
class YouTube extends EventEmitter {
/**
* @param {string} ChannelID ID of the channel to acquire with
* @param {string} APIKey You'r API key
*/
constructor(channelId, apiKey) {
super()
this.id = channelId
this.key = apiKey
this.getLive()
}
getLive() {
const url = 'https://www.googleapis.com/youtube/v3/search'+
'?eventType=live'+
'&part=id'+
`&channelId=${this.id}`+
'&type=video'+
`&key=${this.key}`
this.request(url, data => {
if (!data.items[0])
this.emit('error', 'Can not find live.')
else {
this.liveId = data.items[0].id.videoId
this.getChatId()
}
})
}
getChatId() {
if (!this.liveId) return this.emit('error', 'Live id is invalid.')
const url = 'https://www.googleapis.com/youtube/v3/videos'+
'?part=liveStreamingDetails'+
`&id=${this.liveId}`+
`&key=${this.key}`
this.request(url, data => {
if (!data.items.length)
this.emit('error', 'Can not find chat.')
else {
this.chatId = data.items[0].liveStreamingDetails.activeLiveChatId
this.emit('ready')
}
})
}
/**
* Gets live chat messages.
* See {@link https://developers.google.com/youtube/v3/live/docs/liveChatMessages/list#response|docs}
* @return {object}
*/
getChat() {
if (!this.chatId) return this.emit('error', 'Chat id is invalid.')
const url = 'https://www.googleapis.com/youtube/v3/liveChat/messages'+
`?liveChatId=${this.chatId}`+
'&part=id,snippet,authorDetails'+
'&maxResults=2000'+
`&key=${this.key}`
this.request(url, data => {
this.emit('json', data)
})
}
request(url, callback) {
request({
url: url,
method: 'GET',
json: true,
}, (error, response, data) => {
if (error)
this.emit('error', error)
else if (response.statusCode !== 200)
this.emit('error', data)
else
callback(data)
})
}
/**
* Gets live chat messages at regular intervals.
* @param {number} delay Interval to get live chat messages
* @fires YouTube#message
*/
listen(delay) {
let lastRead = 0, time = 0
this.interval = setInterval(() => this.getChat(), delay)
this.on('json', data => {
for (const item of data.items) {
time = new Date(item.snippet.publishedAt).getTime()
if (lastRead < time) {
lastRead = time
/**
* Emitted whenever a new message is recepted.
* See {@link https://developers.google.com/youtube/v3/live/docs/liveChatMessages#resource|docs}
* @event YouTube#message
* @type {object}
*/
this.emit('message', item)
}
}
})
}
/**
* Stops getting live chat messages at regular intervals.
*/
stop() {
clearInterval(this.interval)
}
}
module.exports = YouTube