Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/4021fb88016278abee6a to your computer and use it in GitHub Desktop.
Save ik5/4021fb88016278abee6a to your computer and use it in GitHub Desktop.
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
Copy link

guss77 commented Jul 21, 2014

nice

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