Skip to content

Instantly share code, notes, and snippets.

@kevinwestern
Created January 14, 2013 05:43
Show Gist options
  • Save kevinwestern/4527965 to your computer and use it in GitHub Desktop.
Save kevinwestern/4527965 to your computer and use it in GitHub Desktop.
var http = require('restler'),
jsdom = require('jsdom');
function getPrice(html) {
var start = html.search(/\$\d+/);
if (start === -1) {
return -1;
}
return +html.substring(start + 1, html.substring(start + 1).search(/\D+/) + 1);
};
function parse(html) {
jsdom.env({
html: html,
done: function(err, window) {
var listings = [];
[].slice.call(window.document.getElementsByClassName('row')).forEach(function(el) {
var price = getPrice(el.querySelector('.itemph').innerHTML),
link = el.querySelector('a'), linkHref, linkText;
if (price === -1) {
return;
}
linkHref = link.href;
linkText = link.innerHTML;
listings.push({
text: linkText,
href: linkHref,
price: price
});
});
console.log(listings.filter(function(list) {
var text = list.text.toLowerCase(),
bed = /2[-\s]*(bed|br|bedroom)/,
bath = /2[-\s]*(bath|ba|bathroom)/
return list.price < 4000 && text.search(bed) !== -1 && text.search(bath) !== -1;
}));
}
});
};
http.get('http://sfbay.craigslist.org/sfc/apa/').on('complete', parse);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment