Created
February 22, 2011 14:56
-
-
Save johnbintz/838790 to your computer and use it in GitHub Desktop.
Use with watchr to reload pages on a remote site periodically to check for changes.
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' | |
require 'mechanize' | |
class RemoteSiteWatchr | |
def initialize(host, urls, &block) | |
@host = host | |
@urls = urls | |
@block = block | |
@agent = Mechanize.new | |
end | |
def start | |
prior_hashes = {} | |
Thread.new do | |
while true | |
@urls.each do |uri| | |
target = "http://#{@host}#{uri}" | |
begin | |
page = @agent.get(target) | |
hash = Digest::MD5.hexdigest(page.body) | |
prior_hashes[uri] = hash if !prior_hashes[uri] | |
if prior_hashes[uri] != hash | |
prior_hashes[uri] = hash | |
puts ">>> #{uri} changed, rerunning..." | |
@block.call | |
break | |
end | |
rescue StandardError => e | |
puts "[ERROR] #{target}: #{e.message}" | |
end | |
end | |
sleep 5 | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment