Create a gist now

Instantly share code, notes, and snippets.

@bds /page_watcher.rb
Last active Dec 29, 2015

What would you like to do?
require 'open-uri'
require 'nokogiri'
require 'twilio-ruby'
require 'spinning_cursor'
class Page
attr_reader :etag, :last_modified, :data, :uri
def initialize(uri)
@uri = URI(uri)
end
def special_element
doc = Nokogiri::HTML(@data)
title, link, result = nil, nil, []
doc.css(".Proddatalink a").each do |item|
if !item.text.empty?
result << "#{item.text}: #{@uri.hostname}/#{item['href']}"
end
end
result
end
def request!(*args)
begin
open(@uri, *args) do |page|
@etag = page.meta['etag'],
@last_modified = page.meta['last-modified'],
@data = page.read
end
rescue OpenURI::HTTPError => e
raise e
end
end
end
class PageWatcher
attr_reader :page
def initialize(uri)
@page = Page.new(uri)
@page.request!
end
def updated?
last_seen_value = @page.special_element
@page.request!
updated_value = @page.special_element
diff = updated_value - last_seen_value
notify(diff) unless diff.empty?
end
def notify(msg)
account_sid = "YOUR_ACCOUNT_SID"
auth_token = "YOUR_AUTH_TOKEN"
client = Twilio::REST::Client.new account_sid, auth_token
client.account.messages.create(
:from => "TWILIO NUMBER",
:to => "YOUR PHONE",
:body => msg)
puts "#{Time.now}:#{msg}"
true
end
end
SpinningCursor.run do
banner "Working it"
type :dots
action do
@page_watcher = PageWatcher.new('http://www.wiialerts.com/')
# Update every 1-minutes
600.times do
SpinningCursor.set_banner "Checking for updates"
@page_watcher.updated?
SpinningCursor.set_banner "Sleeping"
sleep 60
end
end
message "Huh?! I'm awake!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment