Last active
December 16, 2015 21:59
-
-
Save schoblaska/5503572 to your computer and use it in GitHub Desktop.
A rake task to scan Craigslist periodically and email any new listings that match a set of parameters.
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
| require 'open-uri' | |
| def in_bounds?(lat, lng) | |
| bounds = [ | |
| {:north => 41.87849, :east => -87.623917, :south => 41.872041, :west => -87.633787} | |
| ] | |
| bounds.any?{|b| lat >= b[:south] && lat <= b[:north] && lng <= b[:east] && lng >= b[:west]} | |
| end | |
| def email(body, to = "[email protected]") | |
| Pony.mail({ | |
| :to => to, | |
| :via => :smtp, | |
| :via_options => { | |
| :address => 'smtp.gmail.com', | |
| :port => '587', | |
| :enable_starttls_auto => true, | |
| :user_name => "[email protected]", | |
| :password => "password", | |
| :authentication => :plain, | |
| :domain => 'localhost' | |
| }, | |
| :subject => 'Cragslist Watcher', | |
| :from => "[email protected]", | |
| :body => body | |
| }) | |
| end | |
| def email_listings(good) | |
| body = good.map{|l| "#{l[:name]}\n#{l[:url]}"}.join("\n\n\n") | |
| email(body) | |
| end | |
| def email_error(error) | |
| email("#{error.message}\n\n#{error.backtrace.join("\n")}") | |
| end | |
| task :cl_watcher do | |
| begin | |
| query = "http://chicago.craigslist.org/search/apa?zoomToPosting=&altView=&query=loop&srchType=A&minAsk=1200&maxAsk=2000&addTwo=purrr&bedrooms=" | |
| file = "/tmp/cl_watcher.txt" | |
| dupes = [] | |
| `touch #{file}` | |
| File.open(file) do |f| | |
| while line = f.gets | |
| dupes << line.chop | |
| end | |
| end | |
| result = Nokogiri::HTML(open(query)) | |
| good_listings = [] | |
| result.css('p.row').each do |row| | |
| begin | |
| next unless row.attributes['data-longitude'] && row.attributes['data-latitude'] | |
| lat = row.attributes["data-latitude"].value.to_f | |
| lng = row.attributes["data-longitude"].value.to_f | |
| link = row.css('a')[1] | |
| url = 'http://chicago.craigslist.org' + link.attributes["href"].value | |
| name = link.children.first.text | |
| if !dupes.include?(name) && in_bounds?(lat, lng) | |
| good_listings << {:name => name, :url => url} | |
| dupes << name | |
| File.open(file, 'a'){|f| f.puts name} | |
| end | |
| rescue Exception => e | |
| email_error(e) | |
| end | |
| end | |
| email_listings(good_listings) unless good_listings.empty? | |
| rescue Exception => e | |
| email_error(e) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment