Created
August 13, 2015 12:54
-
-
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
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
| # 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