Last active
August 29, 2015 14:04
-
-
Save ik5/4021fb88016278abee6a to your computer and use it in GitHub Desktop.
log alarms of rocket attacks on Israel using Ruby
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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice