|
var request = require('request') |
|
var turf = require('@turf/helpers') |
|
var fs = require('fs') |
|
|
|
var credentials = new Buffer(`${process.env.HOMEAWAY_CLIENT_ID}:${process.env.HOMEAWAY_CLIENT_SECRET}`).toString('base64') |
|
|
|
var data = [] |
|
var token |
|
|
|
request.post('https://ws.homeaway.com/oauth/token', { |
|
headers: { |
|
Authorization: 'Basic ' + credentials |
|
} |
|
}, function (err, res, body) { |
|
if (err) throw err |
|
token = JSON.parse(body).access_token |
|
var opts = { |
|
json: true, |
|
qs: { |
|
lowerLeftLatitude: 48.385442, |
|
lowerLeftLongitude: -123.296814, |
|
upperRightLatitude: 48.828566, |
|
upperRightLongitude: -122.733765, |
|
|
|
pageSize: 30 |
|
}, |
|
headers: { |
|
Authorization: 'Bearer ' + token |
|
} |
|
} |
|
getData('https://ws.homeaway.com/public/search', opts, function (err, data) { |
|
if (err) throw err |
|
else { |
|
fs.writeFileSync('homeaway-listings.json', JSON.stringify(data, null, 2)) |
|
} |
|
}) |
|
}) |
|
|
|
function getData (url, opts, cb) { |
|
if (typeof opts === 'function') { |
|
cb = opts |
|
opts = {} |
|
} |
|
request.get(url, opts, function (err, res, body) { |
|
if (err) cb(err) |
|
if (res.statusCode !== 200) { |
|
cb('Server responded with: ', res.statusCode, res.statusMessage) |
|
} else { |
|
data = data.concat(body.entries.filter(function (entry) { |
|
return entry.location.country === 'US' |
|
}).map(convertToGeojson)) |
|
body.page <= body.pageCount && body.nextPage |
|
? getData(body.nextPage, { |
|
json: true, |
|
headers: { |
|
Authorization: 'Bearer ' + token |
|
} |
|
}, cb) |
|
: cb(null, turf.featureCollection(data)) |
|
} |
|
}) |
|
} |
|
|
|
function convertToGeojson (entry) { |
|
var point = turf.point([entry.location.lng, entry.location.lat], entry) |
|
return point |
|
} |