Last active
April 3, 2024 22:56
-
-
Save Toxicable/095e0f60f52c66b2d5b7d5780c8605b4 to your computer and use it in GitHub Desktop.
realestate map average price
This file contains 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
1. Install Tempermonkey https://chromewebstore.google.com/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo | |
2. CLick the Tampermonkey icon | |
3. Click Create New Script | |
4. Paste in the below code | |
4.5 Hit CMD+S to save it | |
5. Go to https://www.realestate.com.au/buy or https://www.realestate.com.au/sold | |
6. Click Filters, select "Only show properties with a sold price" | |
7. Search and click the Map view | |
It'll have issues when the results have 2 apartments in the same building - I was looking for houses so didn't add support for that at the second |
This file contains 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
// ==UserScript== | |
// @name Average house price | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-03-31 | |
// @description try to take over the world! | |
// @author You | |
// @match https://www.realestate.com.au/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=realestate.com.au | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
const regSingle = /(?<single>\d+)/; | |
const regRange = /(?<from>\d+)( - |-| to )(?<to>\d+)/; | |
(function () { | |
"use strict"; | |
const { fetch: originalFetch } = window; | |
let priceDisplay = null; | |
let USDollar = new Intl.NumberFormat("en-US", { | |
style: "currency", | |
currency: "USD", | |
}); | |
window.fetch = async (...args) => { | |
let [resource, config] = args; | |
// request interceptor here | |
const response = await originalFetch(resource, config); | |
const cloned = response.clone(); | |
const json = await cloned.json(); | |
try { | |
if (!priceDisplay) { | |
const displayContainer = document.querySelector( | |
"div#large-screen-search-panel div" | |
); | |
displayContainer.style.display = "unset"; | |
const div = document.createElement("p"); | |
displayContainer.appendChild(div); | |
priceDisplay = div; | |
} | |
const data = json?.data?.buyMapSearch ?? json?.data?.soldMapSearch; | |
if ( | |
(data?.__typename === "BuyMapResolvedSearch" || | |
data?.__typename === "SoldMapResolvedSearch") && | |
Array.isArray(data.results.items) | |
) { | |
const results = data.results.items.flatMap( | |
(r) => r.results ?? r.listing | |
); | |
const prices = results | |
.filter((r) => r.price) | |
.map((r) => r.price.display.replaceAll("$", "").replaceAll(",", "")); | |
const parsedPrices = prices.map((p) => { | |
const range = p.match(regRange); | |
if (range) { | |
const from = Number(range.groups["from"]); | |
const to = Number(range.groups["to"]); | |
return { | |
input: p, | |
output: (from + to) / 2, | |
}; | |
} | |
const single = p.match(regSingle); | |
if (single) { | |
const price = Number(single.groups["single"]); | |
// this one might have picked up prices like 1.1m or 50k | |
// we could fix it by expanding out those numbers | |
// or just filter out the wildly broken ones | |
if (price < 50000) { | |
return { input: p }; | |
} | |
return { | |
input: p, | |
output: single.groups["single"], | |
}; | |
} | |
return { input: p }; | |
}); | |
const missing = parsedPrices.filter((p) => p.output == null); | |
console.log('unable to parse', missing.map((m) => m.input)); | |
const filtered = parsedPrices.flatMap((p) => p.output ?? []); | |
const total = filtered.reduce((total, next) => Number(next) + total, 0); | |
const average = USDollar.format((total / filtered.length).toFixed(0)); | |
console.log("total: ", average); | |
priceDisplay.innerText = `Average = ${average}`; | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
// response interceptor here | |
return response; | |
}; | |
console.log("fetch patched"); | |
// Your code here... | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment