kd2bwzgen

TwitchAPI - fixed for client ID requirement

Jul 2nd, 2017
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. --=========--
  2. --Changelog--
  3. --=========--
  4. --Version 1.1.1
  5. --1. Twitch API now requires client ID. Call init() with your client ID before calling any other functions (i.e; "twitchapi.init('GkdfA0S9f83F0')"
  6. --2. Refactored some functions
  7. --3. API errors if used without client ID set.
  8. --Edits by MineRobber
  9. --Version 1.1.0
  10. --1. Fixed live() crash bug!
  11. --2. Made file smaller, to save on disk space using init()!
  12. --=========--
  13. --Changelog--
  14. --=========--
  15. local clientID = "NONEGIVEN"
  16. function init(clientid) clientID = clientid end
  17. function _init(streamid)
  18. if clientID=="NONEGIVEN" then error("TwitchAPI: requires client ID",0) end
  19. isjsoninstalled()
  20. os.loadAPI("json")
  21. str = http.get("https://api.twitch.tv/kraken/channels/"..streamid.."/follows?limit=1&client_id="..clientID).readAll()
  22. obj = json.decode(str)
  23. if obj.error then error("TwitchAPI: "..obj.error..": "..obj.message,0) end
  24. follower = obj.follows[1].user.name
  25. followcount = #obj.follows
  26. str = http.get("https://api.twitch.tv/kraken/streams/"..streamid.."?client_id="..clientID).readAll()
  27. obj = json.decode(str)
  28. stream = obj.stream
  29. return follower, followcount, stream
  30. end
  31. function isjsoninstalled()
  32. if fs.exists("json") then
  33. return true
  34. else
  35. print("JSON API Not found! Downloading...")
  36. resp = http.get("http://pastebin.com/raw.php?i=HAve6asF")
  37. if resp then
  38. handler = io.open("json","w")
  39. handler:write(resp.readAll())
  40. handler:close()
  41. end
  42. print("Finished downloading JSON API!")
  43. return true
  44. end
  45. end
  46.  
  47. function live(streamid,colorLive,colorNotLive)
  48. isjsoninstalled()
  49. os.loadAPI("json")
  50. if checkLive(streamid) then
  51. term.setTextColor(colorLive)
  52. print(streamid.." is live!")
  53. term.setTextColor(colors.white)
  54. else
  55. term.setTextColor(colorNotLive)
  56. print(streamid.." is not live!")
  57. term.setTextColor(colors.white)
  58. end
  59. end
  60. function checkLive(streamid)
  61. lastfollow, fc, stream = _init(streamid)
  62. if stream ~= null then
  63. return true
  64. else
  65. return false
  66. end
  67. end
  68. function lastFollower(streamid)
  69. lastfollow, fc, stream = _init(streamid)
  70. return lastfollow
  71. end
  72. function followerCount(streamid)
  73. isjsoninstalled()
  74. os.loadAPI("json")
  75. lastfollow, fc, stream = _init(streamid)
  76. return fc
  77. end
  78. function viewerCount(streamid)
  79. lastfollow, fc, stream = _init(streamid)
  80. if not stream then return 0 end
  81. return stream.viewers
  82. end
  83. function viewCount(streamid)
  84. lastfollow, stream = _init(streamid)
  85. return stream.channel.views
  86. end
Advertisement
Add Comment
Please, Sign In to add comment