diff --git a/README.md b/README.md index babeaf4..4fdca37 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This is a collection of useful scripts to help you manage your Actual Budget. - [Installation](#installation) - Scripts: - [Loan Interest Calculator](#loan-interest-calculator) + - [Tracking Home Prices (Zillow's Zestimate)](#tracking-home-prices-zillows-zestimate) ## Requirements @@ -41,6 +42,10 @@ Run `npm install` to install any required dependencies. ## Scripts +Note that most of the scripts utilize account notes to set configuration on +each account. The scripts will find all accounts that are configured and +update them all in a single call. + ### Loan Interest Calculator This script calculates the interest for a loan account and adds the interest @@ -67,3 +72,33 @@ $ node apply-interest.js ``` It is recommended to run this script once per month. + +### Tracking Home Prices (Zillow's Zestimate) + +This script tracks the Zillow Zestimate for a home. It adds new transactions +to keep the account balance equal to the latest Zestimate. + +To use this script, you need to create a new account in Actual Budget and set +the account note to `zestimate:`. You can find the Zillow URL by +searching for the home on Zillow and copying the URL from the address bar. + +For example, if you want to track the Zestimate for a home with the URL +`https://www.zillow.com/homes/123-Example-St-Anytown-CA-12345/12345678_zpid/`, +set the account note to +`zestimate:https://www.zillow.com/homes/123-Example-St-Anytown-CA-12345/12345678_zpid/`. + +Optionally, you can also specify if you only own a portion of the home by +adding an `ownership:0.0X` tag to the account note. For example, if you own +10% of the home, add `ownership:0.10` to the account note. The script will +then use that percentage to track the home's value. + +You can optionally change the payee used for the transactions by setting +`IMPORTER_ZESTIMATE_PAYEE_NAME` in the `.env` file. + +To run: + +```console +$ node zestimate.js +``` + +It is recommended to run this script once per month. diff --git a/zestimate.js b/zestimate.js new file mode 100644 index 0000000..b661d3b --- /dev/null +++ b/zestimate.js @@ -0,0 +1,66 @@ +const api = require('@actual-app/api'); +const jsdom = require("jsdom"); +const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, openBudget, sleep } = require('./utils'); +require("dotenv").config(); + +async function getZestimate(URL) { + const response = await fetch(URL, { + headers: { + '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': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8', + 'Accept-Language': 'en-GB,en;q=0.6', + 'Referer': 'https://www.google.com/', + } + }); + + const html = await response.text(); + const dom = new jsdom.JSDOM(html); + + const zestimateText = dom.window.document.getElementById('home-details-home-values').getElementsByTagName('h3')[0].textContent; + return parseInt(zestimateText.replace('$', '').replace(',', '')) * 100; +} + +(async function() { + await openBudget(); + + const payeeId = await ensurePayee(process.env.IMPORTER_ZESTIMATE_PAYEE_NAME || 'Zestimate'); + + const accounts = await api.getAccounts(); + for (const account of accounts) { + const note = await getAccountNote(account); + + if (note && note.indexOf('zestimate:') > -1) { + const URL = note.split('zestimate:')[1].split(' ')[0]; + + let ownership = 1; + if (note.indexOf('ownership:') > -1) { + ownership = parseFloat(note.split('ownership:')[1].split(' ')[0]); + } + + console.log('Fetching zestimate for account:', account.name); + console.log('Zillow URL:', URL); + + const zestimate = await getZestimate(URL); + const balance = await getAccountBalance(account); + const diff = (zestimate * ownership) - balance; + + console.log('Zestimate:', zestimate); + console.log('Ownership:', zestimate * ownership); + console.log('Balance:', balance); + console.log('Difference:', diff); + + if (diff != 0) { + await api.importTransactions(account.id, [{ + date: new Date(), + payee: payeeId, + amount: diff, + notes: `Update Zestimate to ${zestimate * ownership / 100} (${zestimate / 100}*${ownership * 100}%)`, + }]); + } + + await sleep(1324); + } + } + + await closeBudget(); +})();