From 7f791e0075f11460e276dc9a8b8a05b0cd1d738b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Mon, 29 Nov 2021 19:05:01 +0200 Subject: [PATCH] Update fetch function in Stations class --- src/client/classes/Stations.js | 111 +++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/src/client/classes/Stations.js b/src/client/classes/Stations.js index 79bb234..caaf534 100644 --- a/src/client/classes/Stations.js +++ b/src/client/classes/Stations.js @@ -17,7 +17,11 @@ module.exports = class Stations extends Array { if(list){ this.length = 0; list.forEach(station => { - this.push(station); + try { + this.push(station); + } catch (error) { + + } }); if(options.show){ @@ -27,6 +31,16 @@ module.exports = class Stations extends Array { }); console.log("\n"); } + + list.forEach(async station => { + try { + let stationTest = await fetch(station.stream[station.stream.default]); + if(stationTest.ok === true) return; + this.splice(this.indexOf(station),1); + } catch (error) { + this.splice(this.indexOf(station),1); + } + }); } this.logger('Stations', 'Successfully fetched list'); @@ -48,64 +62,65 @@ module.exports = class Stations extends Array { search(key) { if (this === null) return false; - let foundStations = []; if (!key) return false; - if (key == "radio") return false; - this - .filter( - x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key - ) - .forEach(x => - foundStations.push({ station: x, name: x.name, probability: 100 }) - ); + let foundStations = []; + if (key == "radio") return false; - if (key.startsWith("radio ")) key = key.slice(6); - const probabilityIncrement = 100 / key.split(" ").length / 2; - for (let i = 0; i < key.split(" ").length; i++) { this .filter( - x => x.name.toUpperCase().includes(key.split(" ")[i].toUpperCase()) || x === key + x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key ) .forEach(x => - foundStations.push({ station: x, name: x.name, probability: probabilityIncrement }) + foundStations.push({ station: x, name: x.name, probability: 100 }) ); - } - if (foundStations.length === 0) return false; - for (let i = 0; i < foundStations.length; i++) { - for (let j = 0; j < foundStations.length; j++) { - if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); - } - } - for (let i = 0; i < foundStations.length; i++) { - if (foundStations[i].name.length > key.length) { - foundStations[i].probability -= - (foundStations[i].name.split(" ").length - key.split(" ").length) * - (probabilityIncrement * 0.5); - } else if (foundStations[i].name.length === key.length) { - foundStations[i].probability += probabilityIncrement * 0.9; - } - for (let j = 0; j < key.split(" ").length; j++) { - if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(" ")[j])) { - foundStations[i].probability -= probabilityIncrement * 0.5; + if (key.startsWith("radio ")) key = key.slice(6); + const probabilityIncrement = 100 / key.split(" ").length / 2; + for (let i = 0; i < key.split(" ").length; i++) { + this + .filter( + x => x.name.toUpperCase().includes(key.split(" ")[i].toUpperCase()) || x === key + ) + .forEach(x => + foundStations.push({ station: x, name: x.name, probability: probabilityIncrement }) + ); + } + if (foundStations.length === 0) return false; + for (let i = 0; i < foundStations.length; i++) { + for (let j = 0; j < foundStations.length; j++) { + if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); } } - } - let highestProbabilityStation; - for (let i = 0; i < foundStations.length; i++) { - if ( - !highestProbabilityStation || - highestProbabilityStation.probability < foundStations[i].probability - ) - highestProbabilityStation = foundStations[i]; - if ( - highestProbabilityStation && - highestProbabilityStation.probability === foundStations[i].probability - ) { - highestProbabilityStation = foundStations[i].station; + for (let i = 0; i < foundStations.length; i++) { + if (foundStations[i].name.length > key.length) { + foundStations[i].probability -= + (foundStations[i].name.split(" ").length - key.split(" ").length) * + (probabilityIncrement * 0.5); + } else if (foundStations[i].name.length === key.length) { + foundStations[i].probability += probabilityIncrement * 0.9; + } + + for (let j = 0; j < key.split(" ").length; j++) { + if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(" ")[j])) { + foundStations[i].probability -= probabilityIncrement * 0.5; + } + } } - } - return highestProbabilityStation; + let highestProbabilityStation; + for (let i = 0; i < foundStations.length; i++) { + if ( + !highestProbabilityStation || + highestProbabilityStation.probability < foundStations[i].probability + ) + highestProbabilityStation = foundStations[i]; + if ( + highestProbabilityStation && + highestProbabilityStation.probability === foundStations[i].probability + ) { + highestProbabilityStation = foundStations[i].station; + } + } + return highestProbabilityStation; } };