Bmorr

Untitled

Nov 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. -- Written By CoolAcid
  2. -- https://github.com/coolacid/ComputerCraft
  3.  
  4. -- Based on the original work by bacon_donut
  5. -- http://pastebin.com/vhn1z23v
  6. -- http://twitch.tv/bacon_donut
  7.  
  8. -- This is formatted to fit on a 1x3 wall of Advanced Monitors with an Advanced Computer connected to a side
  9. -- To get this to work you need to edit the streamid variable then run these six commands:
  10.  
  11. -- label set SomeKindOfNameHere
  12. -- pastebin get WdiT6sR5 bootstrap
  13. -- bootstrap
  14. -- github get coolacid/ComputerCraft/master/twitchdisplay.lua startup
  15. -- edit startup
  16. -- startup
  17.  
  18. -- Twitch Name of the Streamer
  19. streamid = "coolacid"
  20.  
  21. -- Set the Y line for where you want the different bits to go.
  22. line_streamer = 1
  23. line_followers = 3
  24. line_follower = 4
  25. line_viewers = 5
  26.  
  27. -- Set Justification
  28. -- 1 - Left
  29. -- 2 - Center
  30. -- 3 - Right
  31. -- 4 - Split
  32.  
  33. justify_streamer = 1
  34. justify_followers = 1
  35. justify_follower = 1
  36. justify_viewers = 1
  37.  
  38. -- SleepTime is how often to grab new data. Set here to one minute.
  39. -- Set it too fast and twitch will flag you for spam
  40. -- and stop giving you data
  41. SleepTime = 60
  42.  
  43. -- Check to see if the JSON api exists. Otherwise, download it.
  44. if not fs.exists('json.lua') then
  45.     print("JSON API not found - Downloading")
  46.     shell.run("github get coolacid/ComputerCraft/master/json.lua json.lua")
  47. end
  48.  
  49. if not fs.exists('functions') then
  50.     print("internal functions not found - Downloading")
  51.     shell.run("github get coolacid/ComputerCraft/master/functions.lua functions")
  52. end
  53.  
  54. print ("Starting up!")
  55. print ("To Exit press and hold CTRL-T")
  56. print ("To Reboot press and hold CTRL-R")
  57.  
  58. JSON = (loadfile "json.lua")()
  59. os.loadAPI("functions")
  60.  
  61. local m = peripheral.find("monitor")
  62.  
  63. m.setTextColor(colors.blue)
  64. m.setTextScale(1)
  65.  
  66. function getFollowers()
  67.   str = http.get("https://api.twitch.tv/kraken/channels/" .. streamid .. "/follows?limit=1").readAll()
  68.   obj = JSON:decode(str)
  69.   follows = obj._total
  70.   follower = obj.follows[1].user.name
  71.   follower = follower:gsub('"', '')
  72.   return follows, follower
  73. end
  74.  
  75. function getViewerCount()
  76.   str = http.get("https://api.twitch.tv/kraken/streams/" .. streamid).readAll()
  77.   obj = JSON:decode(str)
  78.   if obj.stream == nil then
  79.     return nil
  80.   else
  81.     return obj.stream.viewers
  82.   end
  83. end
  84.  
  85. function localwrite(text, justify, line)
  86.     if justify == 1 then
  87.       -- Right
  88.       m.setCursorPos(1,line)
  89.       m.write(text)
  90.     elseif justify == 2 then
  91.       functions.centerText(m, text, line)
  92.       m.write(text)
  93.     elseif justify == 3 then
  94.       functions.rightJustify(m, text, line)
  95.       m.write(text)
  96.     elseif justify == 4 then
  97.       functions.splitText(m, text, ":", line)
  98.       -- Split Text HAS to write, so we don't need too
  99.     end
  100. end
  101.  
  102. while true do
  103.   local status, live = pcall(getViewerCount)
  104.  
  105.   if status then
  106.     if live == nil then
  107.       m.setBackgroundColor(colors.white)
  108.       m.clear()
  109.       localwrite(streamid, justify_streamer, line_streamer)
  110.       localwrite("Live Viewers: Offline", justify_viewers, line_viewers)
  111.     else
  112.       m.setBackgroundColor(colors.yellow)
  113.       m.clear()
  114.       localwrite(streamid, justify_streamer, line_streamer)
  115.       localwrite("Live Viewers: " .. live, justify_viewers, line_viewers)
  116.     end
  117.   else
  118.       m.setBackgroundColor(colors.white)
  119.       m.clear()
  120.       localwrite(streamid, justify_streamer, line_streamer)
  121.       localwrite("Live Viewers: ERROR", justify_viewers, line_viewers)
  122.   end
  123.  
  124.   local status, followers, follower = pcall(getFollowers)
  125.  
  126.   if status then
  127.     localwrite("Twitch Followers: " .. followers, justify_followers, line_followers)
  128.  
  129.     m.setCursorPos(1,line_follower)
  130.     localwrite("Last Follower: " .. follower, justify_follower, line_follower)
  131.   else
  132.     m.setCursorPos(1,line_followers)  
  133.     localwrite("Twitch Followers: ERROR", justify_followers, line_followers)
  134.  
  135.     m.setCursorPos(1,line_follower)
  136.     localwrite("Last Follower: ERROR", justify_follower, line_follower)
  137.   end
  138.  
  139.   sleep(SleepTime)
  140. end
Add Comment
Please, Sign In to add comment