Skip to content

Commit

Permalink
Fixed read ECONNRESET error.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicnacnic authored May 26, 2021
1 parent 8e68e8b commit d0c76d0
Showing 1 changed file with 83 additions and 67 deletions.
150 changes: 83 additions & 67 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fetch = require("node-fetch");
let oengusURL;

module.exports = function(nodecg) {
module.exports = function (nodecg) {

// Declare replicants.
const targetData = nodecg.Replicant('targetData', '');
Expand All @@ -11,7 +11,7 @@ module.exports = function(nodecg) {
const donationTotal = nodecg.Replicant('donationTotal', 0)

campaignData.value = { currentAmount: 0, targetAmount: 0 }

// Pull data from Tiltify/Oengus
if (nodecg.bundleConfig.donation.useTiltify) {
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}`, {
Expand Down Expand Up @@ -58,6 +58,7 @@ module.exports = function(nodecg) {
}

function getTiltifyData() {
// Get campaign data.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}`, {
method: 'GET',
headers: {
Expand All @@ -68,77 +69,89 @@ module.exports = function(nodecg) {
.then(data => {
campaignData.value = { name: data.data.name, url: data.data.url, description: data.data.description, currentAmount: data.data.amountRaised, targetAmount: data.data.fundraiserGoalAmount }
donationTotal.value = data.data.amountRaised;
}).catch(error => {
nodecg.log.warn('There was an error retrieving Tiltify campaign info.');
nodecg.log.warn(error);
});

// Get targets.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/challenges`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(targets => {
let targetArray = [];
if (targets.data !== '') {
targets.data.forEach(element => {
if ((element.endsAt > new Date().getTime() || element.endsAt === 0) && element.totalAmountRaised < element.amount && element.active) {
if (element.endsAt === 0)
element.endsAt = 9999999999999;
targetArray.push({ name: element.name, currentAmount: element.amount, targetAmount: element.totalAmountRaised, endsAt: element.endsAt, id: element.id })
}
})
targetArray.sort((a, b) => a.endsAt - b.endsAt);
targetData.value = { dataLength: targetArray.length, data: targetArray };
// Get targets.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/challenges`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(targets => {
let targetArray = [];
if (targets.data !== '') {
targets.data.forEach(element => {
if ((element.endsAt > new Date().getTime() || element.endsAt === 0) && element.totalAmountRaised < element.amount && element.active) {
if (element.endsAt === 0)
element.endsAt = 9999999999999;
targetArray.push({ name: element.name, currentAmount: element.amount, targetAmount: element.totalAmountRaised, endsAt: element.endsAt, id: element.id })
}
});
})
targetArray.sort((a, b) => a.endsAt - b.endsAt);
targetData.value = { dataLength: targetArray.length, data: targetArray };
}
}).catch(error => {
nodecg.log.warn('There was an error retrieving Tiltify targets.');
nodecg.log.warn(error);
});

// Get polls.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/polls`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(polls => {
let pollArray = [];
if (polls.data !== '') {
polls.data.forEach(element => {
if (element.active) {
let pollOptionArray = [];
element.options.forEach(pollOption => {
pollOptionArray.push({ name: pollOption.name, currentAmount: pollOption.totalAmountRaised })
})
pollOptionArray.sort((a, b) => b.currentAmount - a.currentAmount);
pollArray.push({ name: element.name, options: pollOptionArray, id: element.id })
}
// Get polls.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/polls`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(polls => {
let pollArray = [];
if (polls.data !== '') {
polls.data.forEach(element => {
if (element.active) {
let pollOptionArray = [];
element.options.forEach(pollOption => {
pollOptionArray.push({ name: pollOption.name, currentAmount: pollOption.totalAmountRaised })
})
pollData.value = { dataLength: pollArray.length, data: pollArray };
pollOptionArray.sort((a, b) => b.currentAmount - a.currentAmount);
pollArray.push({ name: element.name, options: pollOptionArray, id: element.id })
}
});
})
pollData.value = { dataLength: pollArray.length, data: pollArray };
}
}).catch(error => {
nodecg.log.warn('There was an error retrieving Tiltify polls.');
nodecg.log.warn(error);
});

// Get rewards.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/rewards`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(rewards => {
let rewardArray = [];
if (rewards.data !== '') {
rewards.data.forEach(element => {
if ((element.startsAt > new Date().getTime() || element.startsAt === 0) && (element.endsAt > new Date().getTime() || element.endsAt === 0) && element.active) {
if (element.endsAt === 0)
element.endsAt = 9999999999999;
rewardArray.push({ name: element.name, description: element.description, minDonation: element.amount, endsAt: element.endsAt, img: element.image.src, id: element.id })
}
})
rewardArray.sort((a, b) => a.endsAt - b.endsAt);
rewardData.value = { dataLength: rewardArray.length, data: rewardArray };
// Get rewards.
fetch(`https://tiltify.com/api/v3/campaigns/${nodecg.bundleConfig.donation.tiltifyCampaignID}/rewards`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${nodecg.bundleConfig.donation.tiltifyAuthToken}`
},
dataType: 'json',
}).then(response => response.json())
.then(rewards => {
let rewardArray = [];
if (rewards.data !== '') {
rewards.data.forEach(element => {
if ((element.startsAt > new Date().getTime() || element.startsAt === 0) && (element.endsAt > new Date().getTime() || element.endsAt === 0) && element.active) {
if (element.endsAt === 0)
element.endsAt = 9999999999999;
rewardArray.push({ name: element.name, description: element.description, minDonation: element.amount, endsAt: element.endsAt, img: element.image.src, id: element.id })
}
});
})
rewardArray.sort((a, b) => a.endsAt - b.endsAt);
rewardData.value = { dataLength: rewardArray.length, data: rewardArray };
}
}).catch(error => {
nodecg.log.warn('There was an error retrieving Tiltify rewards.');
nodecg.log.warn(error);
});
}

Expand Down Expand Up @@ -176,6 +189,9 @@ module.exports = function(nodecg) {
targetData.value = { dataLength: targets.length, data: targets };
pollData.value = { dataLength: polls.length, data: polls };
})
})
}).catch(error => {
nodecg.log.warn('There was an error retrieving Oengus data.');
nodecg.log.warn(error);
});
}
}

0 comments on commit d0c76d0

Please sign in to comment.