Created
July 10, 2012 04:06
-
-
Save mkscrg/3080929 to your computer and use it in GitHub Desktop.
Ruby script to periodically match a web page against a regex
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 'net/http' | |
$stdout.sync = true | |
if ARGV.include?("--help") || ![2,3,4].include?(ARGV.length) | |
puts "ruby checksite.rb URL REGEX [NOTIFY_EMAIL] [INTERVAL_SECS]" | |
exit! | |
end | |
uri = URI.parse(ARGV[0]) | |
regex = Regexp.compile(ARGV[1]) | |
interval = ARGV[2].to_i | |
email = ARGV[3] | |
loop do | |
puts "Getting page from " + uri.to_s | |
resp = Net::HTTP.get(uri) | |
if resp =~ regex | |
puts "Found a match!" | |
if email | |
puts "Emailing " + email | |
`echo "Go get em tiger! #{uri.to_s}" | sendmail #{email}` | |
else | |
puts "No email, doing nothing" | |
end | |
break | |
end | |
puts "No match." | |
if interval != 0 | |
puts "Sleeping for " + interval.to_s + " seconds" | |
sleep(interval) | |
else | |
break | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment