Skip to content

Instantly share code, notes, and snippets.

@mgrigajtis
Created January 30, 2012 17:18
Show Gist options
  • Save mgrigajtis/1705513 to your computer and use it in GitHub Desktop.
Save mgrigajtis/1705513 to your computer and use it in GitHub Desktop.
Script that searches Craigslist
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'tlsmail'
require 'time'
require 'net/smtp'
def send_email(from, from_alias, to, to_alias, subject, message)
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
Date: #{Time.now.rfc2822}
#{message.to_s}
END_OF_MESSAGE
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', from, 'gmailpassword', :login) do |smtp|
smtp.send_message(msg, from, to)
end
end
# List of Urls that we're going to be scraping
urls = ['http://craigslist', 'http://urls', 'http://go', 'http://here']
keywords = ['keywords', 'go', 'here']
item_title = String.new
price = String.new
matches = Array.new
# Scrape each URL that we have
urls.each do |url|
doc = Nokogiri::HTML(open(url))
doc.css(".row").each do |item|
contains = false
# Get the item title and price
keywords.each do |keyword|
if item.css("a")[0].content.downcase.include?(keyword)
contains = true
end
end
if contains
item_title = "#{item.css("a")[0].content.to_s} - #{item.text[/\$[0-9]+/]} #{item.css("a")[0]['href']}"
matches.push(item_title)
end
end
end
#Notify the important people
send_email('[email protected]', 'From Alias', '[email protected]', 'To Alias', 'Found on Craigslist', matches.map { |i| " " + i.to_s + " "}.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment