$/, '');
miastr = miastr.trimRight().replace(/[\n\r]+$/, '');
miastr = miastr.replace(/
<\/div>$/, '');
miastr = miastr.replace(/
$/, '');
miastr = miastr.replace(/
$/, '');
miastr = miastr.replace(/
$/, '');
return miastr;
},
rimuoviAtPrimoCarattere(stringa) {
if (stringa.charAt(0) === '@') {
return stringa.slice(1);
} else {
return stringa;
}
},
// Funzione per ottenere le coordinate di una singola città
async getCityCoordinates(city) {
try {
const response = await axios.get(`https://nominatim.openstreetmap.org/search?format=json&q=${city.descr},${city.prov},Italia`);
if (response.data.length > 0) {
city.lat = parseFloat(response.data[0].lat);
city.long = parseFloat(response.data[0].lon);
// console.log(`${city.descr}: Lat ${city.lat}, Long ${city.long}`);
return city;
} else {
console.error(`Coordinate non trovate per ${city.descr}, ${city.prov}`);
}
return null;
} catch (error) {
console.error(`Errore durante il recupero delle coordinate per ${city.descr}, ${city.prov}:`, error.message);
}
},
/**
* Funzione per scaricare dati GeoJSON dei comuni d'Italia e salvarli localmente.
* @param {string} url - L'URL dal quale scaricare i dati GeoJSON.
* @param {string} outputPath - Il percorso del file in cui salvare i dati GeoJSON.
*/
async downloadGeoJSON(url, outputPath) {
try {
// Effettua una richiesta GET all'URL specificato per scaricare i dati.
const response = await axios.get(url);
const data = response.data;
// Salva i dati GeoJSON in un file locale.
await fs.writeFile(outputPath, JSON.stringify(data, null, 2));
console.log(`Dati GeoJSON salvati in ${outputPath}`);
} catch (error) {
console.error('Errore durante il download o il salvataggio dei dati GeoJSON:', error);
}
},
arrotondaA2Decimali(valore) {
try {
return parseFloat(valore).toFixed(2);
} catch (error) {
return valore;
}
},
cleanHtmlForTelegram(htmlContent) {
try {
// Assicurati che l'HTML abbia un elemento body
const dom = new JSDOM(`${htmlContent}`);
const document = dom.window.document;
// Definisci i tag permessi su Telegram
const allowedTags = ['b', 'strong', 'i', 'em', 'u', 's', 'strike', 'del', 'a', 'code'];
// Seleziona tutti gli elementi all'interno del body
const allElements = document.body.querySelectorAll('*');
allElements.forEach(element => {
const tagName = element.tagName.toLowerCase();
if (!allowedTags.includes(tagName)) {
// Crea un nodo di testo e sostituisci l'elemento con quel nodo di testo
const textNode = document.createTextNode(element.textContent || '');
element.replaceWith(textNode);
} else {
// Se il tag è permesso, rimuovi tutti gli attributi ad eccezione di 'href' per i link
if (tagName === 'a') {
const href = element.getAttribute('href');
while (element.attributes.length > 0) {
element.removeAttribute(element.attributes[0].name);
}
if (href) {
element.setAttribute('href', href);
}
} else {
while (element.attributes.length > 0) {
element.removeAttribute(element.attributes[0].name);
}
}
}
});
// Ritorna l'HTML pulito
return document.body.innerHTML;
} catch (error) {
console.error('Errore durante la pulizia dell\'HTML:', error);
throw error; // Oppure, gestisci l'errore come preferisci
}
},
getHostWithNoHttporHttps(url) {
try {
const parsedUrl = new URL(url);
return parsedUrl.host;
} catch (error) {
console.error('Errore durante la pulizia dell\'HTML:', error);
throw error; // Oppure, gestisci l'errore come preferisci
}
},
// Crea un file con all'interno il nome del dominio per ogni app:
createFileWithDomainName() {
const arrapps = getApps();
const filename = server_constants.FILECONFIG_SERVER;
for (const app of arrapps) {
fs.writeFile(filename, this.getHostWithNoHttporHttps(app.host), function (err) {
if (err) {
console.log(err);
}
});
if (app.host_test) {
fs.writeFile(filename, this.getHostWithNoHttporHttps(app.host_test), function (err) {
if (err) {
console.log(err);
}
});
}
}
},
ImageDownloader,
};