Skip to content

Instantly share code, notes, and snippets.

@ik5

ik5/redalarm.rb

Last active Aug 29, 2015
Embed
What would you like to do?
log alarms of rocket attacks on Israel using Ruby
#!/usr/bin/env ruby
require 'open-uri'
require 'json'
PKR_URL = 'http://www.oref.org.il/WarningMessages/alerts.json'
AREA_FMT = 'מרחב: %s, קוד מרחב: %s'
LOG_FMT = '[%s] id: %s, %s'
def to_json
#request = Time.now
data = open(PKR_URL).read.force_encoding('utf-16').encode('utf-8')
#answer = Time.now
#print "Took #{answer - request} time to retrive data "
JSON.parse(data)
end
def exec
log = open(File.expand_path(File.dirname(__FILE__), 'alarm.log'), 'a+')
while true do
json = to_json
next if json['title'].strip == 'בדיקה'
#p json
unless json['data'].empty? || json['data'].include?('בדיקה')
# sometimes they break things :(
if json['data'].length == 1 && json['data'].include?(',')
json['data'] = json['data'][0].split(/,\s{0,}/)
end
json['data'].map! do |x|
x =~ /^([\p{hebrew}\s]+[^\s])\s(\d+)$/
x = AREA_FMT % [$1, $2] rescue x
end
list = json['data'].join(' | ')
line = LOG_FMT % [Time.now.strftime('%D %T'), json['id'], list]
#puts line
log.puts line
end
log.flush
sleep 1
end
ensure
log.close
end
exec
#job = fork do
# exec
#end
#
#Process.detach(job)
@guss77

This comment has been minimized.

Copy link

@guss77 guss77 commented Jul 21, 2014

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment