Created
September 14, 2015 09:15
-
-
Save codeincontext/1979a8f13d1ef47a8b76 to your computer and use it in GitHub Desktop.
Page element watcher with scheduler and logging
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' | |
| require 'rufus-scheduler' | |
| # Constants | |
| URL = 'http://www.sammlung-boros.de/visit/book-tour.html?L=1#item_0' | |
| ELEMENT = '.accordion_item:first-child .block_empty' | |
| 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) | |
| Rufus::Scheduler.s.every('5m') do | |
| 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 | |
| puts "no change" | |
| else | |
| puts "changed" | |
| 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 | |
| end | |
| Rufus::Scheduler.s.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment