Skip to content

Commit

Permalink
Merge pull request #31 from ryanchild/main
Browse files Browse the repository at this point in the history
fix index.js hang on ETIMEDOUT
  • Loading branch information
tmm1 authored Jan 8, 2025
2 parents d46a48a + 66b8c30 commit 5b02dbf
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions PlutoIPTV/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,30 @@ const plutoIPTV = {

let startMoment = moment();

const MAX_RETRIES = 3;
const RETRY_DELAY = 60000; // 1 minute

function requestWithRetry(url, retries = MAX_RETRIES) {
return new Promise((resolve, reject) => {
function attempt() {
request(url, function (err, code, raw) {
if (err) {
if (retries > 0 && err.code === 'ETIMEDOUT') {
console.log(`Retrying request... (${MAX_RETRIES - retries + 1})`);
setTimeout(attempt, RETRY_DELAY);
retries--;
} else {
reject(err);
}
} else {
resolve(JSON.parse(raw));
}
});
}
attempt();
});
}

let timeRanges = [];
for (let i = 0; i < 4; i++) {
let endMoment = moment(startMoment).add(6, "hours");
Expand All @@ -268,17 +292,7 @@ const plutoIPTV = {
let url = `https://api.pluto.tv/v2/channels?start=${startTime}&stop=${stopTime}`;
console.log(url);

promises.push(
new Promise((resolve, reject) => {
request(url, function (err, code, raw) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(raw));
}
});
})
);
promises.push(requestWithRetry(url));
});

let channelsList = {};
Expand Down Expand Up @@ -306,6 +320,10 @@ const plutoIPTV = {
fs.writeFileSync("cache.json", JSON.stringify(sortedChannels));
callback(sortedChannels);
return;
})
.catch((err) => {
console.error(err);
process.exit(1);
});
},
};
Expand Down

0 comments on commit 5b02dbf

Please sign in to comment.