Created
January 14, 2013 05:43
-
-
Save kevinwestern/4527965 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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