[Code] Checking Status of Twitch Stream

Discussion in 'Resources' started by chasechocolate, May 4, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    chasechocolate

    So I recently released a plugin that will broadcast a message if your Twitch stream is online or offline. Quite a few people have asked me how I did it, so here's what I did. I used the Justin.tv API to check the status. After understanding the basics from the API, I created a TwitchStream class that I would use to "contact" the website to retrieve the status:
    Code:java
    1. package com.chasechocolate.streamnotify.stream;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.IOException;
    5. import java.io.InputStreamReader;
    6. import java.net.MalformedURLException;
    7. import java.net.URL;
    8.  
    9. /**
    10. * You are welcome to use, redistribute and modify your own copies of this class.
    11. * If you use this code, please provide credit to my original plugin
    12. *
    13. * Example usage:
    14. * TwitchStream stream = new TwitchStream("mychannel");
    15. * if(stream.isOnline){
    16. * Bukkit.broadcastMessage("Our stream is online!");
    17. * }
    18. *
    19. * @author chasechocolate
    20. */
    21.  
    22. public class TwitchStream {
    23. private String channel;
    24.  
    25. private URL url;
    26. private BufferedReader reader;
    27.  
    28. private boolean online = false;
    29.  
    30. public TwitchStream(String channel){
    31. this.channel = channel;
    32.  
    33. refresh();
    34. }
    35.  
    36. public void refresh(){
    37. try {
    38. this.url = new URL("[url]http://api.justin.tv/api/stream/list.json?jsonp=&channel=[/url]" + channel); //Bukkit automatically adds the URL tags, remove them when you copy the class
    39. this.reader = new BufferedReader(new InputStreamReader(url.openStream()));
    40.  
    41. if(!(reader.readLine().equals("[]"))){
    42. online = true;
    43. } else {
    44. online = false;
    45. }
    46. } catch (MalformedURLException e) {
    47. e.printStackTrace();
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52.  
    53. public URL getUrl(){
    54. return this.url;
    55. }
    56.  
    57. public boolean isOnline(){
    58. return this.online;
    59. }
    60. }

    So there you go! Hope it is helpful :)
     
    xWatermelon and mncat77 like this.
  2. Offline

    ZeusAllMighty11

    I created a Twitch plugin, not public (yet?).

    You can also retrieve viewer count, follower count, view count, chat users, chat messages, etc. :)
     
  3. Offline

    xWatermelon

    chasechocolate THANK YOU! :) I have been wondering how to do this!
    How?!? I know it returns an array, but how can I format that? PM me, please :)
     
  4. Offline

    ZeusAllMighty11

    xWatermelon

    I will post my own thread in this section in a few days about it.
     
  5. Offline

    xWatermelon

    k thanks
     
  6. Offline

    macguy8

    Just a small thing:
    In your example, it should be isOnline() instead of isOnline, as its a method, not a public boolean.
     
  7. Offline

    AstramG

    It actually is a public boolean because that is what it returns.
     
  8. Offline

    macguy8

    AstramG
    The method is a public boolean, but it is being referenced as a field in the code example, where it should be referenced as a method.
     
  9. Offline

    AstramG

    It is a method, so its working correctly.
    :3
     
  10. Offline

    macguy8

    AstramG
    I know, but it is written as .isOnline, not .isOnline(). It not having the parenthesis is implying isOnline is a field, instead of a method
     
  11. Offline

    AstramG

    It does have parenthesis.
    EDIT: Oh you meant in the example, I see now.
     
  12. Offline

    iTidez

    It has been a bit since I have worked with the API, but to my knowledge it returns a JSON array. You can use Google's GSON library to simply parse the array into a JSON array based on a class file for the fields. Then simply reading the result as an object array.
     
Thread Status:
Not open for further replies.

Share This Page