Last active
December 12, 2015 05:58
-
-
Save mion/4725629 to your computer and use it in GitHub Desktop.
Track changes to a website! Run it in a cron.
This file contains 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
# Track changes to a website, dude! | |
require 'open-uri' | |
require 'digest/md5' | |
require 'tempfile' | |
############### Dem helper methods yo | |
def write_md5(md5, filename) | |
File.open(filename, 'w') { |f| f.write(md5) } | |
end | |
def read_md5(filename) | |
md5 = '' | |
File.open(filename, 'r') { |f| md5 = f.read } | |
md5 | |
end | |
def notify | |
# Send yourself an email or something | |
puts "OMGZOR!!!1!! Can I has change?? YES ploxx" | |
end | |
############### Da badass skript man | |
# Target URL | |
URL = 'http://rubylearning.com/satishtalim/ruby_exceptions.html' | |
# File to store MD5 | |
FILENAME = 'last_md5.txt' | |
# Download HTML into a temp file and create a md5 digest | |
temp = Tempfile.new('temp') | |
begin | |
temp.write(open(URL).read) | |
md5 = Digest::MD5.file(temp.path).hexdigest | |
ensure | |
temp.close | |
temp.unlink | |
end | |
# If there's something new, save it and let me know | |
if File.exists?(FILENAME) | |
last_md5 = read_md5(FILENAME) | |
unless md5.eql?(last_md5) | |
write_md5(md5, FILENAME) | |
notify | |
end | |
else | |
write_md5(md5, FILENAME) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment