Skip to content

Instantly share code, notes, and snippets.

@KelOkekpe
Created November 9, 2020 18:36
Show Gist options
  • Save KelOkekpe/9fb4379910b47c2afbe68ed69d538c03 to your computer and use it in GitHub Desktop.
Save KelOkekpe/9fb4379910b47c2afbe68ed69d538c03 to your computer and use it in GitHub Desktop.
Automate Robinhood accout value retrieval
const { chromium } = require('playwright');
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);
const fetch = require('node-fetch');
(async () => {
const userDataDir = '/Users/jokekpe/Library/Application Support/Google/Chrome/Default'
const browser = await chromium.launchPersistentContext(userDataDir, {headless: true });
const page = await browser.newPage();
await page.goto('https://robinhood.com', {timeout: 60000, waitUntil: 'domcontentloaded'});
console.log('Your current total account value:')
await getAccountValue(page)
console.log('')
console.log('Your holdings values:')
const body = await listTickers(page)
fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
await setTimeoutPromise(1000);
await browser.close();
})();
async function getAccountValue(page) {
await page.waitForSelector('div[class=QzVHcLdwl2CEuEMpTUFaj]')
const price = await page.evaluate(() => {
return { price: document.querySelector('div[class=QzVHcLdwl2CEuEMpTUFaj]').innerText.replace(/[^0-9.]/g, "") }
});
console.log('$',price.price)
}
async function listTickers(page){
await page.waitForSelector('div[class=QzVHcLdwl2CEuEMpTUFaj]')
const tickers = await page.evaluate(() => {
array = []
const tickers_array = document.querySelectorAll('div[class=css-1drbkg9]')
for(i=0; i < tickers_array.length; i++) {
array.push(tickers_array[i].innerText.split(' ')[0])
}
return { array: array}
});
const tickerPrices = await page.evaluate(() => {
array = []
const ticker_prices = document.querySelectorAll('div[class=css-5h0m38]')
for(i=0; i < ticker_prices.length; i++) {
if (ticker_prices[i].innerText.includes('+')) {
const remove_gl = ticker_prices[i].innerText.split('+')[0]
let price = remove_gl.replace(/[^0-9.]/g, "")
array.push(price)
} else if (ticker_prices[i].innerText.includes('-')) {
const remove_gl = ticker_prices[i].innerText.split('-')[0]
let price = remove_gl.replace(/[^0-9.]/g, "")
array.push(price)
}
}
return { array: array }
});
json = {}
for(i=0; i<tickers.array.length; i++){
json[tickers.array[i]] = tickerPrices.array[i];
}
console.log(json)
return Promise.resolve(json)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment