nfp_sites/heimaerbest/autofetch.mjs

62 lines
1.8 KiB
JavaScript

import Client from './base/media/client.mjs'
const client = new Client()
function fetch(page = 1) {
return client.get(`http://fasteignir.visir.is/api/search?onpage=100&page=${page}`)
.then(data => {
return data.map(listing => {
if (listing.bathrooms === null) {
listing.bathrooms = 0
listing.invalid = true
}
listing.rooms = Number(listing.rooms)
listing.bedrooms = Number(listing.bedrooms)
listing.bathrooms = Number(listing.bathrooms)
listing.zip.zip = Number(listing.zip.zip)
listing.price = Number(listing.price)
listing.size = listing.size.split(',')[0]
return listing
})
})
}
async function parseAll() {
let page = 1
let data = []
while (page <= 10) {
console.log(`Fetching page ${page}`)
let add = await fetch(page)
if (add.length === 0) {
break
}
page += 1
data = data.concat(add)
}
console.log(`finished, got total ${data.length} listings`)
return data
}
parseAll().then((data) => {
for (let i = 0; i < data.length && i < 100; i++) {
let listing = data[i]
if (listing.invalid || listing.rooms === 0 || listing.zip.zip >= 950 || listing.bathrooms === 0 || listing.price === 0) {
let prefix = ''
if (listing.invalid && listing.rooms > 0) {
prefix += '[Invalid] '
}
if (listing.bathrooms === 0 && listing.rooms > 0) {
prefix += '[No bath] '
}
if (listing.price === 0) {
prefix += '[Tilboð] '
}
console.log(`${prefix}http://fasteignir.visir.is/property/${listing.id} : ${listing.street_name} ${listing.street_number}, ${listing.zip.zip} (${listing.sale_or_rent}). ${listing.rooms} rooms, ${listing.bedrooms} bedrooms, ${listing.bathrooms} baths. ${listing.size} m2 = ${listing.price} kr.`)
}
}
})
// http://fasteignir.visir.is/api/search?onpage=1000&page=1&zip=103