Created
March 22, 2024 02:41
-
-
Save bsidhom/1e5e4290c892d10d69f7a7425516da2a to your computer and use it in GitHub Desktop.
Extract unrealized lot data from Vanguard accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let main = async (account, units) => { | |
const scrapeAccount = (accountWidget) => { | |
const accountId = accountWidget.id; | |
const accountName = accountWidget | |
.querySelector("span.c11n-accordion__heading") | |
.innerText.trim(); | |
const holdingsLinks = [ | |
...accountWidget.querySelectorAll( | |
`app-holdings tr[app-holdings-row] div.holdings-link` | |
), | |
]; | |
const holdingsIds = holdingsLinks.map((el) => el.id); | |
return { accountId, accountName, holdingsIds }; | |
}; | |
const scrapeAccounts = () => { | |
const accountWidgets = [...document.querySelectorAll("c11n-accordion")]; | |
return accountWidgets.map(scrapeAccount); | |
}; | |
const fetchLots = async (account, holding) => { | |
const unrealizedUrl = `https://personal1.vanguard.com/smn-client-cost-basis-accounting-webservice/costbasis/external/lots?request=unrealized`; | |
const headers = new Headers(); | |
headers.append("Consumer-Application-Code", "HDV"); | |
headers.append("X-Account-Id", account); | |
// headers.append("X-Hdv-Correlation-Id", "HDV-UI-<uuid>"); | |
headers.append("X-Holding-Id", holding); | |
const resp = await fetch(unrealizedUrl, { | |
headers, | |
credentials: "include", | |
}); | |
const j = await resp.json(); | |
return j.coveredLots; | |
}; | |
const fetchAccount = async ({ accountId, accountName, holdingsIds }) => { | |
const lots = await Promise.all( | |
holdingsIds.map(async (holdingId) => { | |
return await fetchLots(accountId, holdingId); | |
}) | |
); | |
return { | |
accountId, | |
accountName, | |
holdingsIds, | |
lots, | |
}; | |
}; | |
const accounts = scrapeAccounts(); | |
const data = await Promise.all(accounts.map(fetchAccount)); | |
const resultString = JSON.stringify(data); | |
const blob = new Blob([resultString], { type: "text/plain;charset=UTF-8" }); | |
const url = URL.createObjectURL(blob); | |
window.open(url); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment