Advertisement
Guest User

twitch

a guest
Mar 3rd, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. -- Program made by MCTVMessy
  2. -- Youtube, Twitch, Twitter - /MCTVMessy
  3.  
  4. -- StreamID - Change to needed channel
  5. streamid = "OfficialMCTVmessy"
  6.  
  7. -- Set the Y line on the monitor
  8. line_streamer = 1
  9. line_followers = 3
  10. line_follower = 4
  11. line_viewers = 5
  12.  
  13. -- SleepTime to set how often to grab new info from
  14. -- the api, if it is too fast, then twitch will
  15. -- flag you for spam, and stop sending data!
  16. SleepTime = 60
  17.  
  18. -- Check to see if the JSON api exists, download it
  19. if not fs.exists('json') then
  20.   print("JSON API not found - Downloading...")
  21.   shell.run("pastebin get 4nRg9CHU json")
  22. end
  23.  
  24. os.loadAPI("json")
  25.  
  26. local m = peripheral.find("monitor")
  27.  
  28. m.setTextColor(colors.white)
  29. m.setTextScale(1)
  30.  
  31. function getFollowers()
  32.   str = http.get("https://api.twitch.tv/kraken/channels" .. streamid .. "/followers?limit=1").readAll()
  33.   obj = json.decode(str)
  34.   follows = json.encodePretty(obj._total)
  35.   follower = json.encodePretty(obj.follows[1].user.name)
  36.   follower = follower:gsub('"', '')
  37.   return follows, follower
  38. end
  39.  
  40. function getViewerCount()
  41.   str = http.get("https://api.twitch.tv/kraken/streams/" .. streamid).readAll()
  42.   obj = json.decode(str)
  43.   if obj.stream == nil then
  44.     return nil
  45.   else
  46.     return json.encodePretty(obj.stream.viewers)
  47.   end
  48. end
  49.  
  50. while true do
  51.   local status, live = pcall(getViewerCount)
  52.  
  53.   if status then
  54.     m.setCursorPos(1,line_streamer)
  55.     if live == nil then
  56.       m.setBackgroundColor(colors.white)
  57.       m.clear()
  58.       m.write(streamid)
  59.       m.setCursorPos(1,line_viewers)
  60.       m.write("Live Viewers: Offline")
  61.     else
  62.       m.setBackgroundColor(colors.red)
  63.       m.clear()
  64.       m.write(streamid)
  65.       m.setCursorPos(1,line_viewers)
  66.       m.write("Live Viewers: " .. live)
  67.     end
  68.   else
  69.       m.setBackgroundColor(colors.orange)
  70.       m.clear()
  71.       m.write(streamid)
  72.       m.setCursorPos(1,line_viewers)
  73.   end
  74.  
  75.   local status, followers, follower = pcall(getFollowers)
  76.  
  77.   if status then
  78.     m.setCursorPos(1,line_followers)
  79.     m.write("Twitch Followers: " .. followers)
  80.    
  81.     m.setCursorPos(1,line_follower)
  82.     m.write("Last Follower: " .. follower)
  83.   else
  84.     m.setCursorPos(1,line_followers)
  85.     m.write("Twitch Followers: ERROR")
  86.    
  87.     m.setCursorPos(1,line_follower)
  88.     m.write("Last Follower: ERROR")
  89.   end
  90.    
  91.   sleep(SleepTime)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement