-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-twitch.js
80 lines (61 loc) · 1.77 KB
/
check-twitch.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
require("sugar");
var TwitchClient = require("node-twitchtv");
var account = require("./user.json");
var fs = require("fs");
var irc = require("irc");
var ircDetails = require("./irc.json");
var client = new TwitchClient(account);
var users = ["gordallott", "Thinaran", "ChemiKhazi", "btsierra"]
var IsStreaming = function (username, cb) {
client.streams({channel: username}, function (err, resp) {
if (resp.hasOwnProperty("stream")) {
cb(resp.stream);
}
else {
cb(null);
}
});
}
var results = {}
var userCtr = 0;
var checkUser = function(index, cb) {
IsStreaming(users[index], function(stream) {
if (stream !== null) {
results[stream.channel.display_name] = stream;
}
userCtr++;
if (userCtr < users.length) {
checkUser(userCtr, cb);
}
else {
cb (results);
}
});
}
// g enerate new cache list
checkUser(userCtr, function (streams) {
var cachedStreams = {}
try {
cachedStreams = JSON.parse(fs.readFileSync('streamCache.json', 'utf8'));
}
catch (err) {
}
var foundItems = [];
Object.values(streams).each(function (stream) {
if (Object.values(cachedStreams).none(function (n) { return (stream.created_at === n.created_at); })) {
foundItems.push(stream.channel.display_name + " has gone live! playing " + stream.game + " at " + stream.channel.url);
}
});
foundItems.each(function (v) { console.log(v); });
fs.writeFileSync('streamCache.json', JSON.stringify(streams), 'utf8');
if (foundItems.length > 0) {
var ircClient = new irc.Client('rafiki.local', 'gord', Object.merge(ircDetails, {autoConnect:false}));
ircClient.connect(function () {
foundItems.each(function (announce) {
console.log("sending to irc: " + announce);
ircClient.say("#abandongames", announce);
});
ircClient.disconnect();
});
}
});