Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local util = {}
- json = require("json")
- local FIRST_DATE = os.time({ year = 2020, month = 1, day = 22 })
- function util.cachedJSONRequest(url, filename)
- local h, err = http.get(url)
- if not h then
- if not fs.exists(filename) then
- error("Error contacting API: " .. err, 0)
- end
- local cache = fs.open(filename, "r")
- local data = json.decode(cache.readAll())
- cache.close()
- return data
- end
- local rawData = h.readAll()
- local cache = fs.open(filename, "w")
- cache.write(rawData)
- cache.close()
- return json.decode(rawData)
- end
- function util.formatCommas(number)
- return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
- end
- function util.sortedCloneByKey(tbl, key)
- local out = {}
- for _, v in pairs(tbl) do
- table.insert(out, v)
- end
- table.sort(out, function(a, b) return a[key] > b[key] end)
- return out
- end
- function util.colourScore(i, val)
- if val == 0 then return nil
- elseif i <= 3 then return colours.red
- elseif i <= 6 then return colours.orange
- else return colours.yellow end
- end
- function util.recoveredColourScore(i, val)
- if val == 0 then return nil
- elseif i <= 3 then return colours.lime
- else return colours.cyan end -- used as a pastel green
- end
- function util.scoreData(data)
- table.sort(data, function(a, b) return a.cases > b.cases end)
- for k, v in pairs(data) do
- v.originalIndex = k
- end
- local sortedTodayCases = util.sortedCloneByKey(data, "todayCases")
- local sortedTodayDeaths = util.sortedCloneByKey(data, "todayDeaths")
- for i = 1, 10 do
- local todayCasesDatum = data[sortedTodayCases[i].originalIndex]
- todayCasesDatum.todayCasesColour = util.colourScore(i, todayCasesDatum.todayCases)
- local todayDeathsDatum = data[sortedTodayDeaths[i].originalIndex]
- todayDeathsDatum.todayDeathsColour = util.colourScore(i, todayDeathsDatum.todayDeaths)
- end
- if data[1].recovered ~= nil then
- local sortedRecoveries = util.sortedCloneByKey(data, "recovered")
- for i = 1, 6 do
- local recoveredDatum = data[sortedRecoveries[i].originalIndex]
- recoveredDatum.recoveredColour = util.recoveredColourScore(i, recoveredDatum.recoveredColour)
- end
- end
- end
- function util.processCumulativeHistoricalData(data)
- local out = {}
- for k, v in pairs(data) do
- -- local month, day, year = table.unpack(_.map({ k:match("(%d+)/(%d+)/(%d+)") }, tonumber))
- local month, day, year = k:match("(%d+)/(%d+)/(%d+)")
- month, day, year = tonumber(month), tonumber(day), tonumber(year)
- local datum = {
- year = 2000 + year,
- month = month,
- day = day
- }
- datum.time = os.time(datum)
- datum.formatted = os.date("%m/%d", datum.time)
- datum.value = v
- table.insert(out, datum)
- end
- table.sort(out, function(a, b) return a.time < b.time end)
- local lastValue = nil
- for k, datum in pairs(out) do
- local v = datum.value
- if lastValue ~= nil then
- datum.value = v - lastValue
- end
- lastValue = v
- end
- return out
- end
- function util.handleMonitorArg(...)
- local args = { ... }
- if #args ~= 1 then error("Usage: " .. shell.getRunningProgram() .. " <monitor>", 0) end
- local mon = peripheral.wrap(args[1])
- if not mon or not mon.setTextScale then error("Monitor not found", 0) end
- mon.setTextScale(0.5)
- return mon, mon.getSize()
- end
- function util.mainLoop(mainFn, sleepTime)
- while true do
- local ok, err = pcall(mainFn)
- if err == "Terminated" then error(err, 0) end
- if not ok then printError(err) end
- sleep(sleepTime or 900)
- end
- end
- return util
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement