From d0c76d0a74d4a21d28ac4a9b48d476853a51e485 Mon Sep 17 00:00:00 2001 From: nicnacnic Date: Wed, 26 May 2021 13:50:53 -0400 Subject: [PATCH] Fixed read ECONNRESET error. --- extension.js | 150 ++++++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 67 deletions(-) diff --git a/extension.js b/extension.js index a8dd33a..e7eb40c 100644 --- a/extension.js +++ b/extension.js @@ -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', ''); @@ -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}`, { @@ -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: { @@ -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); }); } @@ -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); + }); } } \ No newline at end of file