xXxKanemanxXx

twitch follower

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