Skip to content

Instantly share code, notes, and snippets.

@rynr
Last active December 22, 2015 02:48
Show Gist options
  • Save rynr/6405708 to your computer and use it in GitHub Desktop.
Save rynr/6405708 to your computer and use it in GitHub Desktop.
Send SMS-Alerts while in holiday to warn when auroras get possible
#!/usr/bin/env ruby
# Encoding: utf-8
#
# change all occurences of XXX to adapt
require 'rubygems'
require 'bundler/setup'
require 'pstore'
require 'xmlrpc/client'
require 'uri'
require 'logger'
Bundler.require
class SMS
API_URL = URI("https://samurai.sipgate.net/RPC2")
API_URL.user = 'XXX'
API_URL.password = 'XXX'
def initialize(logger)
@logger = logger
end
def deliver(phonenumber, text)
@logger.debug("SMS deliver(#{phonenumber}, #{text})")
client.call('samurai.SessionInitiate',
{'RemoteUri' => "sip:#{phonenumber}@sipgate.net",
'TOS' => 'text',
'Content' => text})
end
private
def client
@logger.debug("#{self.class.name} client")
unless @client
@client = XMLRPC::Client.new2(API_URL)
@client.call('samurai.ClientIdentify',
{'ClientName' => "Ruby #{self.class.name}",
'ClientVersion' => '0.0.0a',
'ClientVendor' => 'rjung'})
end
@client
end
end
class Check
CHECK_URL = 'http://aurora.fmi.fi/public_service/magforecast_en.html'
USER_AGENT = 'Linux Firefox'
CLOSE_FIELDS = [
'Sodankylä (SOD)',
'Pello (PEL)',
'Oulujärvi (OUJ)',
]
ORDER = [ '#00FF00', '#FFFF00', '#FFA500', '#FF0000', '#800000' ]
TEXTS = [ 'niedrig', 'erhöht', 'mäßig', 'hoch', 'sehr hoch' ]
DATA_STORE = '/tmp/aurora.db'
RECIPIENT = 'XXX'
def run
logger.debug " -> run"
if([previous_result, this_result].max >= 3 && [previous_result, this_result].min < 3)
logger.info "Send Message (#{previous_result},#{this_result})"
text = previous_result < 3 ? 'ALERT' : 'COOLDOWN'
text += ": neuer Status #{TEXTS[this_result]}, vorher #{TEXTS[previous_result]}"
SMS.new(logger).deliver(RECIPIENT, text)
else
logger.info "Send no Message (#{previous_result},#{this_result})"
end
end
private
def logger
unless @logger
@logger = Logger.new(STDOUT)
@logger.level = Logger::DEBUG
end
@logger
end
def previous_result
logger.debug " -> previous_result"
unless @previous_result
@previous_result, @this_result = update
end
@previous_result
end
def this_result
logger.debug " -> this_result"
unless @this_result
@previous_result, @this_result = update
end
@this_result
end
def update
logger.debug " -> update"
result = []
store.transaction do
result.push(store[:last_result] || 0)
result.push(store[:last_result] = current_status)
store[Time.now.strftime('%Y%d%m%H')] = current_status
end
result
end
def store
logger.debug " -> store"
@store ||= PStore.new(DATA_STORE)
end
def current_status
logger.debug " -> current_status"
@current_status ||= highest_color(extract_status(extract_close_rows(fetch_site.search('table')[2])))
end
def highest_color(colors)
logger.debug " -> highest_color(#{colors.inspect})"
colors.inject(0) do |result, color|
[result, ORDER.index(color)].max
end
end
def extract_status(rows)
logger.debug " -> extract_status(#{rows.inspect})"
rows.map do |row|
row.children[6].attribute('bgcolor').value
end
end
def extract_close_rows(table)
logger.debug " -> extract_close_rows(#{table.inspect})"
table.children.select do |tr|
CLOSE_FIELDS.include?(tr.children[0].text)
end
end
def fetch_site
logger.debug " -> fetch_site"
mechanize.get(CHECK_URL)
end
def mechanize
logger.debug " -> mechanize"
@mechanize ||= Mechanize.new do |m|
m.user_agent_alias = USER_AGENT
end
end
end
Check.new.run
source 'https://rubygems.org/'
gem 'mechanize'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment