-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (77 loc) · 2.38 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
const q = require('daskeyboard-applet');
const {google} = require('googleapis')
const logger = q.logger;
class StreamerStatus extends q.DesktopApp {
constructor() {
super();
// run every 10 min
this.pollingInterval = 10000;
}
// function to search youtube for active livestreams
async getStreamStatus(channelID, youtube) {
const res = await youtube.search.list({
part: 'snippet',
"channelId": channelID,
"eventType": "live",
"type": [
"video"
]
});
return (res);
}
generateSignal(streamStatus, onlineColour, offlineColour) {
// If stream is live/if there are any results.
if (streamStatus.data.items[0]) {
return new q.Signal({
points: [
[new q.Point(onlineColour)]
],
link: {
url: `https://www.youtube.com/watch?v=${streamStatus.data.items[0]['id'].videoId}`,
label: 'This channel is streaming!',
},
name: "This channel is streaming!",
message: "This channel is streaming!"
});
} else {
// If stream is offline/if there are no results
return new q.Signal({
points: [
[new q.Point(offlineColour)]
],
link: {
},
name: "This channel is offline!",
message: "This channel is offline!"
});
}};
async run() {
logger.info("Stream check running.");
const onlineColour = this.config.onlineColour;
const offlineColour = this.config.offlineColour;
const channelID = this.config.channelID;
const youtube = google.youtube({
version: 'v3',
auth: this.authorization.apiKey,
})
if (channelID) {
logger.info("My channel ID is: " + channelID);
return this.getStreamStatus(channelID, youtube).then(streamStatus => {
return this.generateSignal(streamStatus, onlineColour, offlineColour);
}).catch((error) => {
logger.error("Error while getting stream status: " + error);
if(`${error.message}`.includes("getaddrinfo")){
}else{
return q.Signal.error([`The Youtube API returned an error. Detail: ${error}`]);
}
});
} else {
logger.info("No channel ID configured.");
return null;
}
}
}
module.exports = {
StreamerStatus: StreamerStatus
};
const applet = new StreamerStatus();