Advertisement
infiniteblock

util

Apr 23rd, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. local util = {}
  2. json = require("json")
  3. local FIRST_DATE = os.time({ year = 2020, month = 1, day = 22 })
  4.  
  5. function util.cachedJSONRequest(url, filename)
  6. local h, err = http.get(url)
  7.  
  8. if not h then
  9. if not fs.exists(filename) then
  10. error("Error contacting API: " .. err, 0)
  11. end
  12.  
  13. local cache = fs.open(filename, "r")
  14. local data = json.decode(cache.readAll())
  15. cache.close()
  16. return data
  17. end
  18.  
  19. local rawData = h.readAll()
  20.  
  21. local cache = fs.open(filename, "w")
  22. cache.write(rawData)
  23. cache.close()
  24.  
  25. return json.decode(rawData)
  26. end
  27.  
  28. function util.formatCommas(number)
  29. return tostring(number):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
  30. end
  31.  
  32. function util.sortedCloneByKey(tbl, key)
  33. local out = {}
  34. for _, v in pairs(tbl) do
  35. table.insert(out, v)
  36. end
  37. table.sort(out, function(a, b) return a[key] > b[key] end)
  38. return out
  39. end
  40.  
  41. function util.colourScore(i, val)
  42. if val == 0 then return nil
  43. elseif i <= 3 then return colours.red
  44. elseif i <= 6 then return colours.orange
  45. else return colours.yellow end
  46. end
  47.  
  48. function util.recoveredColourScore(i, val)
  49. if val == 0 then return nil
  50. elseif i <= 3 then return colours.lime
  51. else return colours.cyan end -- used as a pastel green
  52. end
  53.  
  54. function util.scoreData(data)
  55. table.sort(data, function(a, b) return a.cases > b.cases end)
  56. for k, v in pairs(data) do
  57. v.originalIndex = k
  58. end
  59.  
  60. local sortedTodayCases = util.sortedCloneByKey(data, "todayCases")
  61. local sortedTodayDeaths = util.sortedCloneByKey(data, "todayDeaths")
  62.  
  63. for i = 1, 10 do
  64. local todayCasesDatum = data[sortedTodayCases[i].originalIndex]
  65. todayCasesDatum.todayCasesColour = util.colourScore(i, todayCasesDatum.todayCases)
  66.  
  67. local todayDeathsDatum = data[sortedTodayDeaths[i].originalIndex]
  68. todayDeathsDatum.todayDeathsColour = util.colourScore(i, todayDeathsDatum.todayDeaths)
  69. end
  70.  
  71. if data[1].recovered ~= nil then
  72. local sortedRecoveries = util.sortedCloneByKey(data, "recovered")
  73.  
  74. for i = 1, 6 do
  75. local recoveredDatum = data[sortedRecoveries[i].originalIndex]
  76. recoveredDatum.recoveredColour = util.recoveredColourScore(i, recoveredDatum.recoveredColour)
  77. end
  78. end
  79. end
  80.  
  81. function util.processCumulativeHistoricalData(data)
  82. local out = {}
  83. for k, v in pairs(data) do
  84. -- local month, day, year = table.unpack(_.map({ k:match("(%d+)/(%d+)/(%d+)") }, tonumber))
  85. local month, day, year = k:match("(%d+)/(%d+)/(%d+)")
  86. month, day, year = tonumber(month), tonumber(day), tonumber(year)
  87. local datum = {
  88. year = 2000 + year,
  89. month = month,
  90. day = day
  91. }
  92. datum.time = os.time(datum)
  93. datum.formatted = os.date("%m/%d", datum.time)
  94. datum.value = v
  95.  
  96. table.insert(out, datum)
  97. end
  98.  
  99. table.sort(out, function(a, b) return a.time < b.time end)
  100.  
  101. local lastValue = nil
  102. for k, datum in pairs(out) do
  103. local v = datum.value
  104.  
  105. if lastValue ~= nil then
  106. datum.value = v - lastValue
  107. end
  108.  
  109. lastValue = v
  110. end
  111.  
  112. return out
  113. end
  114.  
  115. function util.handleMonitorArg(...)
  116. local args = { ... }
  117. if #args ~= 1 then error("Usage: " .. shell.getRunningProgram() .. " <monitor>", 0) end
  118.  
  119. local mon = peripheral.wrap(args[1])
  120. if not mon or not mon.setTextScale then error("Monitor not found", 0) end
  121.  
  122. mon.setTextScale(0.5)
  123. return mon, mon.getSize()
  124. end
  125.  
  126. function util.mainLoop(mainFn, sleepTime)
  127. while true do
  128. local ok, err = pcall(mainFn)
  129. if err == "Terminated" then error(err, 0) end
  130. if not ok then printError(err) end
  131. sleep(sleepTime or 900)
  132. end
  133. end
  134.  
  135. return util
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement