diff --git a/README.md b/README.md index 76e522d..c8eb517 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ This is a collection of useful scripts to help you manage your Actual Budget. - [Loan Interest Calculator](#loan-interest-calculator) - [Tracking Home Prices (RentCast's Value Estimate)](#tracking-home-prices-rentcasts-value-estimate) - [Tracking Home Prices (Zillow's Zestimate)](#tracking-home-prices-zillows-zestimate) - - [Tracking Car Prices (Kelley Blue Book)](#tracking-car-prices-kelley-blue-book) + - [Tracking Vehicle Prices (Kelley Blue Book)](#tracking-vehicle-prices-kelley-blue-book) - [Tracking Investment Accounts](#tracking-investment-accounts) - [Tracking Bitcoin Price](#tracking-bitcoin-price) @@ -255,12 +255,12 @@ node zestimate.js It is recommended to run this script once per month. -### Tracking Car Prices (Kelley Blue Book) +### Tracking Vehicle Prices (Kelley Blue Book) **Note: this script requires having a starting balance in the account** -This script tracks the Kelley Blue Book value for a car. It adds new -transactions to keep the account balance equal to the latest KBB value. +This script tracks the Kelley Blue Book value for a car or motorcycle. It adds +new transactions to keep the account balance equal to the latest KBB value. To use this script, first you need to use the KBB website to find the value of your car. Be sure to select "Private Party" for the value. It should show @@ -285,6 +285,15 @@ on the values in the URL. - `kbbVehicleid:XXXXXX` - `kbbOptions:XXX,XXX,XXX,...` +If you are tracking a motorcycle, use these settings: + +``` +kbbURL:https://www.kbb.com/motorcycles/suzuki/dr650s/2018/? +kbbPriceType:tradein (or retail) +``` + +Be sure to find the correct URL for your specific motorcycle and year. + You can optionally change the payee used for the transactions by setting `KBB_PAYEE_NAME` in the `.env` file. diff --git a/kbb.js b/kbb.js index d9fdbb1..a8f1c38 100644 --- a/kbb.js +++ b/kbb.js @@ -18,8 +18,15 @@ async function getKBB(URL) { const html = await response.text(); const dom = new jsdom.JSDOM(html); - const kbbText = dom.window.document.getElementById('PriceAdvisor').getElementsByTagName('text')[3].textContent; - return parseInt(kbbText.replace('$', '').replaceAll(',', '')) * 100; + const advisor = dom.window.document.getElementById('PriceAdvisor'); + if (advisor) { + const kbbText = advisor.getElementsByTagName('text')[3].textContent; + return parseInt(kbbText.replace('$', '').replaceAll(',', '')) * 100; + } + + const regex = /"value":\s*(\d+)/; + const match = html.match(regex); + return parseInt(match[1]) * 100; } (async function() { @@ -49,6 +56,9 @@ async function getKBB(URL) { const options = getTagValue(note, 'kbbOptions'); if (options) URL += `&optionids=${options}`; + const pricetype = getTagValue(note, 'kbbPriceType'); + if (pricetype) URL += `&pricetype=${pricetype}`; + console.log('Fetching KBB for account:', account.name); const kbb = await getKBB(URL);