Advertisement
kreezxil

bdonut_twitch_reader

Feb 25th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. streamid = "Bacon_Donut"
  2. SleepTime = 60
  3.  
  4. if not fs.exists('json') then
  5. write("JSON API not found - Downloading")
  6. shell.run("pastebin get 4nRg9CHU json")
  7. end
  8.  
  9. os.loadAPI("json")
  10. local m = peripheral.wrap("right")
  11. m.setCursorPos(1,1)
  12.  
  13. function getFollowers()
  14.  
  15. str = http.get("https://api.twitch.tv/kraken/channels/" .. streamid .. "/follows?limit=1").readAll()
  16. obj = json.decode(str)
  17. follows = json.encodePretty(obj._total)
  18.  
  19. m.setCursorPos(1,3)
  20. m.write("Twitch Followers: ")
  21. m.write(follows)
  22.  
  23. return follows
  24. end
  25.  
  26. function getFollower()
  27.  
  28. str = http.get("https://api.twitch.tv/kraken/channels/" .. streamid .. "/follows?limit=1").readAll()
  29. obj = json.decode(str)
  30. follower = json.encodePretty(obj.follows[1].user.name)
  31.  
  32. m.setCursorPos(1,5)
  33. m.write("Follower: ")
  34. m.write(follower)
  35.  
  36. return follows
  37. end
  38.  
  39. function getViewerCount()
  40. lstr = http.get("https://api.twitch.tv/kraken/streams/" .. streamid).readAll()
  41. lobj = json.decode(lstr)
  42. m.setCursorPos(1,1)
  43.  
  44.  
  45. if lobj.stream == nil then
  46. m.write(streamid)
  47. m.setCursorPos(1,4)
  48. m.write("Live Viewers: Offline")
  49. else
  50. live = json.encodePretty(lobj.stream.viewers)
  51. m.setBackgroundColor(colors.yellow)
  52. m.clear()
  53. m.write(streamid)
  54. m.setCursorPos(1,4)
  55. m.write("Live Viewers: ")
  56. m.write(live)
  57. end
  58.  
  59. return live
  60. end
  61.  
  62. while true do
  63. m.setCursorPos(1,1)
  64. m.setBackgroundColor(colors.white)
  65. m.setTextColor(colors.blue)
  66. m.setTextScale(1)
  67. m.clear()
  68.  
  69. m.write(streamid)
  70. m.setCursorPos(1,4)
  71.  
  72. local status, live = pcall(function () getViewerCount() end)
  73.  
  74. if status then
  75. -- do nothing
  76. else
  77. m.write("Live Viewers: Loading...")
  78. end
  79.  
  80. local status, followsCount = pcall(function () getFollowers() end)
  81.  
  82. m.setCursorPos(1,3)
  83.  
  84. if status then
  85. -- do nothing
  86. else
  87. m.write("Twitch Follows: Loading...")
  88. end
  89.  
  90. m.setCursorPos(1,5)
  91.  
  92. local status, live = pcall(function () getFollower() end)
  93.  
  94. if status then
  95. -- do nothing
  96. else
  97. m.write("Follower: Loading...")
  98. end
  99.  
  100. sleep(SleepTime)
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement