require('request')
const request = require('request-promise-native')
const xray = require('x-ray-select')
async function getPhoto(link) {
const photoData = await request(link)
const spec = [{
$root: 'head',
img: 'meta[property="og:image"]@content'
}]
return xray(photoData)(spec)
}
async function getData(profile) {
const data = await request(`https://orangecounty.craigslist.org/search/cto?search_distance=`+profile.radius+`&postal=`+profile.zip+`&auto_make_model=`+profile.make+`&min_auto_year=`+profile.year.min+`&max_auto_year=`+profile.year.max)
const spec = [{
$root: '.result-row',
title: '.result-title',
price: '.result-price',
place: '.result-hood',
link: '.result-image@href',
imgLink: '.result-image @data-ids',
img: ''
}]
//console.log(data)
return xray(data)(spec)
}
const profile = {
zip: '92663',
make: 'BMW',
radius: 10,
year: {min: 2010, max: 2015}
}
/*
Tasks:
1) Add more fields to the result
2) Support changing of radius, zip, make and year in search profile
*/
var img
var res = await getData(profile)
var imgLink
for (var i = 0; i < res.length; i++) {
if (res[i].place !== undefined) {
res[i].place = res[i].place.replace(/\s|\)|\(/g,'')
}
imgLink = 'https://images.craigslist.org/' + res[i].imgLink.substr(2,res[i].imgLink.indexOf(',')-2) + '_600x450.jpg'
res[i].imgLink = imgLink
res[i].price = +res[i].price.substr(1)
img = await getPhoto(res[i].link)
res[i].img = img[0].img
console.log(res[i])
}
//console.log(res)