Skip to content

Instantly share code, notes, and snippets.

@Pezmc
Created May 11, 2020 11:55
Show Gist options
  • Save Pezmc/197f75a745f11aee7e6e29e0a4153dcd to your computer and use it in GitHub Desktop.
Save Pezmc/197f75a745f11aee7e6e29e0a4153dcd to your computer and use it in GitHub Desktop.
OctopusComparison quick scraping script
// Grabs prices from https://octopuscomparison.netlify.app/ and totals them up
// Simply paste this into the javascript console
// Relies on some hacks to trigger React onchange events
let allPrices = [];
(function() {
function setNativeValue(element, value) {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
}
allPrices = [];
const input = document.querySelector('[type="date"]')
const start = input.min
const end = input.max
let loop = new Date(start);
loop = new Date(loop.setDate(loop.getDate() + 1)); // skip first day
let endDate = new Date(end);
while(loop <= endDate){
setNativeValue(input, loop.toISOString().substr(0, 10))
input.dispatchEvent(new Event('input', { bubbles: true }));
const prices = document.querySelector("#root > div > div > div.prices > h1").textContent
const [ _agileName, agilePrice, _goName, goPrice ] = prices.split(/\||\:/).map((entry) => entry.trim().replace('£', ''))
allPrices.push({date: loop.toISOString().substr(0, 10), agilePrice, goPrice})
const newDate = loop.setDate(loop.getDate() + 1);
loop = new Date(newDate);
}
const totals = allPrices.reduce((accumulator, day) => {
accumulator.totalAgile += parseFloat(day.agilePrice)
accumulator.totalGo += parseFloat(day.goPrice)
accumulator.totalDifference += (parseFloat(day.goPrice) - parseFloat(day.agilePrice))
return accumulator
}, { totalAgile: 0, totalGo: 0, totalDifference: 0 })
const h1 = document.querySelector("#root > div > div > div.header > h1")
h1.textContent = `Total for ${allPrices.length} days on Agile: £${totals.totalAgile.toFixed(2)}, Go: £${totals.totalGo.toFixed(2)}, Difference: £${totals.totalDifference.toFixed(2)}`
return totals
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment