add error handling

This commit is contained in:
Robert Dyer
2024-07-14 20:51:53 -05:00
parent ffd14caa15
commit a329eae2d1
+22 -11
View File
@@ -4,20 +4,31 @@ const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, openBudget,
require("dotenv").config(); require("dotenv").config();
async function getZestimate(URL) { async function getZestimate(URL) {
const response = await fetch(URL, { try {
headers: { const response = await fetch(URL, {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Accept-Language': 'en-GB,en;q=0.6', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
'Referer': 'https://www.google.com/', 'Accept-Language': 'en-GB,en;q=0.6',
} 'Referer': 'https://www.google.com/',
}); }
});
} catch (error) {
console.log('Error fetching Zillow URL:');
console.log(error);
}
const html = await response.text(); const html = await response.text();
const dom = new jsdom.JSDOM(html); try {
const dom = new jsdom.JSDOM(html);
const zestimateText = dom.window.document.getElementById('home-details-home-values').getElementsByTagName('h3')[0].textContent; const zestimateText = dom.window.document.getElementById('home-details-home-values').getElementsByTagName('h3')[0].textContent;
return parseInt(zestimateText.replace('$', '').replace(',', '')) * 100; return parseInt(zestimateText.replace('$', '').replace(',', '')) * 100;
} catch (error) {
console.log('Error parsing Zillow page:');
console.log(error);
console.log(html);
}
} }
(async function() { (async function() {