-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathg4g.js
69 lines (57 loc) · 2.64 KB
/
g4g.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
// This is currently using an undocumented API, so might break at any point.
'use strict';
var request = require('request');
var statsAPI = 'https://api.gamingforgood.net/d/stats/fund/';
var donationsAPI = 'https://api.gamingforgood.net/d/donations/fund/'; // not used in this code yet
var twitchChannelID;
var g4gDonationTotalReplicant;
var nodecg = require('./utils/nodecg-api-context').get();
if (nodecg.bundleConfig && nodecg.bundleConfig.gaming4Good && nodecg.bundleConfig.gaming4Good.enable) {
// Will not enable if we have no way to get the Twitch channel ID.
if (!nodecg.bundleConfig.gaming4Good.twitchChannelID || (nodecg.bundleConfig.twitch && !nodecg.bundleConfig.twitch.enable))
nodecg.log.info("Gaming4Good integration was enabled but we have no way to know the Twitch channel.");
nodecg.log.info("Gaming4Good integration is enabled.");
// Used to store whatever the API says is the current donation total is.
g4gDonationTotalReplicant = nodecg.Replicant('g4gDonationTotal', {persistent: false, defaultValue: '0.00'});
// If a Twitch channel ID is specified manually in the config, use that instead.
// This is in case the user doesn't use the Twitch integration but still wants to use Gaming4Good.
if (nodecg.bundleConfig.gaming4Good.twitchChannelID) {
twitchChannelID = nodecg.bundleConfig.gaming4Good.twitchChannelID;
checkDonationTotal();
}
else {
// Waits until we have the Twitch channel info before doing anything.
var twitchChannelInfo = nodecg.Replicant('twitchChannelInfo', {persistent: false});
twitchChannelInfo.on('change', (newVal, oldVal) => {
if (!oldVal && newVal) {
twitchChannelID = newValue['_id'];
checkDonationTotal();
}
});
}
}
// Used to frequently get the current donation total from G4G and if it's changed, update the replicant.
function checkDonationTotal() {
request(createRequestOptions(statsAPI + twitchChannelID), function(error, response, body) {
var parsed; if (!error) {try {parsed = JSON.parse(body);} catch(e) {parsed = null;}}
if (parsed && !error && response.statusCode == 200) {
// If the donation total exists and has changed, update the replicant.
if (parsed['raisedCharity'] && g4gDonationTotalReplicant.value != parsed['raisedCharity']) {
g4gDonationTotalReplicant.value = parsed['raisedCharity'];
}
}
else {
nodecg.log.warn("Error occurred when requesting donation total from Gaming4Good.");
}
setTimeout(checkDonationTotal, 30000);
});
}
// Used to create the options for the API requests above, which includes the referer.
function createRequestOptions(apiURL) {
return {
url: apiURL,
headers: {
'Referer': 'https://www.gamingforgood.net'
}
}
}