Skip to content

Instantly share code, notes, and snippets.

@callumj
Created November 5, 2011 16:36
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 callumj/1341746 to your computer and use it in GitHub Desktop.
Save callumj/1341746 to your computer and use it in GitHub Desktop.
UWA PC availability
require 'net/http'
require 'json'
require 'sqlite3'
DB_LOC = "#{File.dirname(__FILE__)}/data.db"
URL_PARSE = "http://www.is.uwa.edu.au/site_elements/labstats-remote.json/_nocache"
SUCCESS_SLEEP = 30 # 30 min
FAIL_SLEEP = 1 # 1 min
db = SQLite3::Database.new(DB_LOC)
db.execute("CREATE TABLE IF NOT EXISTS comp_status (id INTEGER PRIMARY KEY, location TEXT, available INTEGER, inuse INTEGER, unknown1 INTEGER, unknown2 INTEGER, time INTEGER)")
while true do
success = true
uri = URI(URL_PARSE)
begin
contents = Net::HTTP.get(uri)
data = JSON.parse(contents)
# got the data, insert
current_time = Time.now.utc.to_i
data.keys.each do |key|
available = 0
inuse = 0
un1 = 0
un2 = 0
if (data[key].length >= 4)
available = data[key][3]
inuse = data[key][1]
un1 = data[key][0]
un2 = data[key][2]
elsif (data[key].length > 0)
available = data[key][0]
end
stmt = "INSERT INTO comp_status (time, location, available, inuse, unknown1, unknown2) VALUES (#{current_time}, \"#{key}\", #{available}, #{inuse}, #{un1}, #{un2})"
db.execute(stmt)
end
puts "Processed for #{current_time}"
rescue
success = false
end
sleep((success ? SUCCESS_SLEEP : FAIL_SLEEP) * 60)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment