Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Created August 13, 2015 12:54
Show Gist options
  • Select an option

  • Save codeincontext/cecd86a81cd761a30988 to your computer and use it in GitHub Desktop.

Select an option

Save codeincontext/cecd86a81cd761a30988 to your computer and use it in GitHub Desktop.
Check the contents of an element on a remote page and send an SMS if it has changed
# page-element-watcher
#
# Checks the contents of an element on a remote page and sends an SMS if it has changed
require 'nokogiri'
require 'open-uri'
require 'redis'
require 'twilio-ruby'
# Constants
URL = 'http://www.sammlung-boros.de/visit/book-tour.html?L=1#item_0'
ELEMENT = '.accordion_item:first-child'
REDIS_KEY = "page-element-watcher:element-contents:#{URL}:#{ELEMENT}"
TWILIO_SID = ''
TWILIO_TOKEN = ''
SMS_FROM = ''
SMS_TO = %w{}
# Globals
@redis = Redis.new
@twilio = Twilio::REST::Client.new(TWILIO_SID, TWILIO_TOKEN)
doc = Nokogiri::HTML(open(URL))
element_contents = doc.css(ELEMENT).to_s
previous_element_contents = @redis.get(REDIS_KEY)
if element_contents != previous_element_contents
message = "page-element-watcher: Page #{URL} element #{ELEMENT} has changed!"
@twilio.messages.create(
from: SMS_FROM,
to: SMS_TO,
body: message
)
@redis.set(REDIS_KEY, element_contents)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment