Advertisement
MRtecno98

OpenComputers Plug_And_Cree follower counter

Feb 10th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. local component = require("component")
  2. local net = require("internet")
  3. local term = require("term")
  4. local os = require("os")
  5. local io = require("io")
  6. local gpu = component.gpu
  7.  
  8. JSON_LIB = "https://raw.githubusercontent.com/sziberov/OpenComputers/master/lib/json.lua"
  9. BIGFONT_LIB = "https://raw.githubusercontent.com/iesika/iesika-OCPrograms/master/OC-bigfont/lib/bigfont.lua"
  10.  
  11. function getFontURL(size)
  12.     return "https://raw.githubusercontent.com/iesika/iesika-OCPrograms/master/OC-bigfont/lib/bigfont-size" .. tostring(size) .. ".hex"
  13. end
  14.  
  15. function download(url, path)
  16.   local r = net.request(url)
  17.   local f = io.open(path, "w")
  18.  
  19.   local chunk = r()
  20.   while not (chunk == nil) do
  21.     f:write(chunk)
  22.     chunk = r()
  23.   end
  24.  
  25.   f:close()
  26. end
  27.  
  28. if not component.isAvailable("internet") then
  29.   io.stderr:write("This program requires an internet card to run.")
  30.   return
  31. end
  32.  
  33. local status, json = pcall(require, "json")
  34.  
  35. if (not status) or json == nil then
  36.   print("Json parser not found, downloading")
  37.  
  38.   download(JSON_LIB, "/lib/json.lua")
  39.  
  40.   status, json = pcall(require, "json")
  41.  
  42.   if (not status) or json == nil then
  43.     print("Couldn't install json parser, aborting.")
  44.     os.exit()
  45.   end
  46.  
  47.   print("Json parser installed")
  48. end
  49.  
  50. local status, bigfont = pcall(require, "bigfont")
  51.  
  52. if (not status) or bigfont == nil then
  53.   print("BigFont library not found, downloading")
  54.  
  55.   download(BIGFONT_LIB, "/lib/bigfont.lua")
  56.  
  57.   print("Lib download complete, downlading fonts")
  58.  
  59.   for i=2,8 do download(getFontURL(i), "/lib/bigfont-size" .. tostring(i) .. ".hex") end
  60.  
  61.   status, bigfont = pcall(require, "bigfont")
  62.  
  63.   if (not status) or bigfont == nil then
  64.     print("Couldn't install bigfont library, aborting.")
  65.     os.exit()
  66.   end
  67.  
  68.   print("BigFont library installed")
  69. end
  70.  
  71. REFRESH_RATE = 5
  72.  
  73. function get_followers()
  74.   local r = net.request("https://api.twitch.tv/helix/users/follows?to_id=167165508&first=1", {}, {["Client-ID"]="74jgk3923ql6fw2cbb5835qcnb6jrt"}, "GET")
  75.  
  76.   res = ""
  77.   for chunk in r do
  78.     res = res .. chunk
  79.   end
  80.  
  81.   local code, message, _ = getmetatable(r).__index.response()
  82.  
  83.   return json:decode(res)["total"], code, message
  84. end
  85.  
  86. function time() return math.floor(os.time() / 100) end
  87.  
  88. local h, w = gpu.getResolution()
  89.  
  90. bigfont.load(8)
  91.  
  92. local timestamp = time()
  93.  
  94. local followers, code, message = get_followers()
  95. local start_followers = followers
  96.  
  97. while true do
  98.   if time() - timestamp >= REFRESH_RATE then
  99.     timestamp = time()
  100.     followers, code, message = get_followers()
  101.   end
  102.  
  103.   term.clear() -- clear terminal(only works on print statements, resets cursor)
  104.   gpu.fill(1, 1, w, h, " ") -- clear screen(actual complete clear)
  105.  
  106.   print("code: " .. tostring(code))
  107.   print("msg: " .. tostring(message))
  108.  
  109.   print("Starting followers: " .. tostring(start_followers))
  110.  
  111.   bigfont.set(2, 14, "Gained Followers: " .. tostring(followers - start_followers), 8)
  112.  
  113.   print("Next refresh: " .. tostring(REFRESH_RATE - (time() - timestamp)))
  114.  
  115.   os.sleep(0.05)
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement