Last active
December 12, 2015 01:48
-
-
Save tourdedave/4693384 to your computer and use it in GitHub Desktop.
Grab the contents of a page by url and css selector, strip out the html, and return just the copy. When run, it checks twice, storing a copy and latest_copy. This is then used to check and see if the site has changed, only notifying when it has.
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
#!/usr/bin/env ruby | |
require 'rest-client' | |
require 'nokogiri' | |
require 'sanitize' | |
class Checker | |
attr_accessor :url, :scope, :sleep_interval | |
attr_reader :page, :copy, :latest_copy | |
def initialize(url, scope, sleep_interval) | |
@url = url; @scope = scope; @sleep_interval = sleep_interval | |
puts "Checking to see if #{@url} has been updated..." | |
get_page | |
@copy = get_copy | |
check_copy | |
refresh | |
end | |
def scope=(new_scope) | |
@scope = new_scope | |
end | |
def get_page | |
@page = Nokogiri::HTML(RestClient.get @url) | |
end | |
def get_copy | |
Sanitize.clean(@page.css(@scope).to_html) | |
end | |
def check_copy | |
if @copy.empty? | |
puts "There's nothing to check against. Exiting..." | |
exit | |
else | |
puts "Here's what it says right now:" | |
puts @copy | |
end | |
end | |
def refresh | |
get_page | |
@latest_copy = get_copy | |
end | |
end | |
site = Checker.new("http://www.rubygems.org", ".pitch", 60) | |
if site.copy == site.latest_copy | |
puts "No change yet. We will notify you when it has." | |
until site.copy != site.latest_copy do | |
site.refresh | |
sleep site.sleep_interval | |
end | |
end | |
puts "#{site.url} posted a new update." | |
puts "Here's what it says:" | |
puts site.latest_copy | |
# or add your notification method of choice | |
# e.g. a growl notification, or a tropo.com trigger URL to send a text message, etc. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment