Twitch API Get Real Name Status Title Bitrate and more

Discussion in 'Resources' started by CreepyEye, Jul 2, 2014.

Thread Status:
Not open for further replies.
  1. Hey i was bored and wrote a little twitch api you can get the Viewers Count the online status and much more!


    How i can use it?

    Code:java
    1. Twitch_Stream stream = Twitch_API.getStream("USERNAME");
    2. stream.getUserName();
    3. steram.getChannelURL();
    4. stream.getTags();
    5. stream.isOnline();
    6. ...




    How you can i implement it ? You copy the code into your plugin!

    Twitch_Stream Class:

    For Lombok Users ( I Love It :D)
    Code:java
    1. package me.CreepyEye.Twitch;
    2.  
    3. import com.google.gson.JsonArray;
    4. import com.google.gson.JsonObject;
    5.  
    6. import lombok.Data;
    7. import lombok.Getter;
    8. import lombok.Setter;
    9.  
    10. @ Data
    11. public class Twitch_Stream {
    12.  
    13. boolean online;
    14.  
    15. int broadcast_part;
    16.  
    17. boolean featured;
    18.  
    19. boolean channel_subscription;
    20.  
    21. String id;
    22.  
    23. String category;
    24.  
    25. String title;
    26.  
    27. int channel_count;
    28.  
    29. int video_height;
    30.  
    31. int site_count;
    32.  
    33. boolean embed_enabled;
    34.  
    35. String up_time;
    36.  
    37. String meta_game;
    38.  
    39. String format;
    40.  
    41. int embed_count;
    42.  
    43. String stream_type;
    44.  
    45. boolean abuse_reported;
    46.  
    47. int video_width;
    48.  
    49. String geo;
    50.  
    51. String name;
    52.  
    53. String language;
    54.  
    55. int stream_count;
    56.  
    57. double video_bitrate;
    58.  
    59. String broadcaster;
    60.  
    61. int channel_view_count;
    62.  
    63.  
    64. // Other Infos
    65.  
    66. String username;
    67.  
    68. String status;
    69.  
    70. String channel_url;
    71.  
    72. boolean producer;
    73.  
    74. String subcategory_title;
    75.  
    76. String screen_cap_url_large;
    77.  
    78. String screen_cap_url_small;
    79.  
    80. String screen_cap_url_medium;
    81.  
    82. String screen_cap_url_huge;
    83.  
    84. String timezone;
    85.  
    86. String category_title;
    87.  
    88. int views_count;
    89.  
    90. public void load(JsonObject job) {
    91. setBroadcast_part(job.get("broadcast_part").getAsInt());
    92. setFeatured(job.get("featured").getAsBoolean());
    93. setChannel_subscription(job.get("channel_subscription").getAsBoolean());
    94. setId(job.get("id").getAsString());
    95. setCategory(job.get("category").getAsString());
    96. setTitle(job.get("title").getAsString());
    97. setChannel_count(job.get("channel_count").getAsInt());
    98. setVideo_height(job.get("video_height").getAsInt());
    99. setSite_count(job.get("site_count").getAsInt());
    100. setEmbed_enabled(job.get("embed_enabled").getAsBoolean());
    101. setUp_time(job.get("up_time").getAsString());
    102. setMeta_game(job.get("meta_game").getAsString());
    103. setFormat(job.get("format").getAsString());
    104. setEmbed_count(job.get("embed_count").getAsInt());
    105. setStream_type(job.get("stream_type").getAsString());
    106. setAbuse_reported(job.get("abuse_reported").getAsBoolean());
    107. setVideo_width(job.get("video_width").getAsInt());
    108. setGeo(job.get("geo").getAsString());
    109. setName(job.get("name").getAsString());
    110. setLanguage(job.get("language").getAsString());
    111. setStream_count(job.get("stream_count").getAsInt());
    112. setVideo_bitrate(job.get("video_bitrate").getAsDouble());
    113. setBroadcaster(job.get("broadcaster").getAsString());
    114. setChannel_view_count(job.get("channel_view_count").getAsInt());
    115. // Other Infos
    116. setUsername(job.get("channel").getAsJsonObject().get("login")
    117. .getAsString());
    118. setTitle(job.get("channel").getAsJsonObject().get("status")
    119. .getAsString());
    120. setChannel_url(job.get("channel").getAsJsonObject().get("channel_url")
    121. .getAsString());
    122. setProducer(job.get("channel").getAsJsonObject().get("producer")
    123. .getAsBoolean());
    124. // setTags(job.get("channel").getAsJsonObject().get("tags").getAsString());
    125. setSubcategory_title(job.get("channel").getAsJsonObject()
    126. .get("subcategory_title").getAsString());
    127. setScreen_cap_url_large(job.get("channel").getAsJsonObject()
    128. .get("screen_cap_url_large").getAsString());
    129. setScreen_cap_url_small(job.get("channel").getAsJsonObject()
    130. .get("screen_cap_url_small").getAsString());
    131. setScreen_cap_url_medium(job.get("channel").getAsJsonObject()
    132. .get("screen_cap_url_medium").getAsString());
    133. setScreen_cap_url_huge(job.get("channel").getAsJsonObject()
    134. .get("screen_cap_url_huge").getAsString());
    135. setTimezone(job.get("channel").getAsJsonObject().get("timezone")
    136. .getAsString());
    137. setCategory_title(job.get("channel").getAsJsonObject()
    138. .get("category_title").getAsString());
    139. setViews_count(job.get("channel").getAsJsonObject().get("views_count")
    140. .getAsInt());
    141.  
    142. }
    143.  
    144. }




    For Users Without Lomobk:

    Code:java
    1. package me.CreepyEye.Twitch;
    2.  
    3. import com.google.gson.JsonElement;
    4. import com.google.gson.JsonObject;
    5.  
    6. public class Twitch_Stream
    7. {
    8. boolean online;
    9. int broadcast_part;
    10. boolean featured;
    11. boolean channel_subscription;
    12. String id;
    13. String category;
    14. String title;
    15. int channel_count;
    16. int video_height;
    17. int site_count;
    18. boolean embed_enabled;
    19. String up_time;
    20. String meta_game;
    21. String format;
    22. int embed_count;
    23. String stream_type;
    24. boolean abuse_reported;
    25. int video_width;
    26. String geo;
    27. String name;
    28. String language;
    29. int stream_count;
    30. double video_bitrate;
    31. String broadcaster;
    32. int channel_view_count;
    33. String username;
    34. String status;
    35. String channel_url;
    36. boolean producer;
    37. String subcategory_title;
    38. String screen_cap_url_large;
    39. String screen_cap_url_small;
    40. String screen_cap_url_medium;
    41. String screen_cap_url_huge;
    42. String timezone;
    43. String category_title;
    44. int views_count;
    45.  
    46. public void load(JsonObject job)
    47. {
    48. setBroadcast_part(job.get("broadcast_part").getAsInt());
    49. setFeatured(job.get("featured").getAsBoolean());
    50. setChannel_subscription(job.get("channel_subscription").getAsBoolean());
    51. setId(job.get("id").getAsString());
    52. setCategory(job.get("category").getAsString());
    53. setTitle(job.get("title").getAsString());
    54. setChannel_count(job.get("channel_count").getAsInt());
    55. setVideo_height(job.get("video_height").getAsInt());
    56. setSite_count(job.get("site_count").getAsInt());
    57. setEmbed_enabled(job.get("embed_enabled").getAsBoolean());
    58. setUp_time(job.get("up_time").getAsString());
    59. setMeta_game(job.get("meta_game").getAsString());
    60. setFormat(job.get("format").getAsString());
    61. setEmbed_count(job.get("embed_count").getAsInt());
    62. setStream_type(job.get("stream_type").getAsString());
    63. setAbuse_reported(job.get("abuse_reported").getAsBoolean());
    64. setVideo_width(job.get("video_width").getAsInt());
    65. setGeo(job.get("geo").getAsString());
    66. setName(job.get("name").getAsString());
    67. setLanguage(job.get("language").getAsString());
    68. setStream_count(job.get("stream_count").getAsInt());
    69. setVideo_bitrate(job.get("video_bitrate").getAsDouble());
    70. setBroadcaster(job.get("broadcaster").getAsString());
    71. setChannel_view_count(job.get("channel_view_count").getAsInt());
    72.  
    73. setUsername(job.get("channel").getAsJsonObject().get("login").getAsString());
    74. setTitle(job.get("channel").getAsJsonObject().get("status").getAsString());
    75. setChannel_url(job.get("channel").getAsJsonObject().get("channel_url").getAsString());
    76. setProducer(job.get("channel").getAsJsonObject().get("producer").getAsBoolean());
    77.  
    78. setSubcategory_title(job.get("channel").getAsJsonObject().get("subcategory_title").getAsString());
    79. setScreen_cap_url_large(job.get("channel").getAsJsonObject().get("screen_cap_url_large").getAsString());
    80. setScreen_cap_url_small(job.get("channel").getAsJsonObject().get("screen_cap_url_small").getAsString());
    81. setScreen_cap_url_medium(job.get("channel").getAsJsonObject().get("screen_cap_url_medium").getAsString());
    82. setScreen_cap_url_huge(job.get("channel").getAsJsonObject().get("screen_cap_url_huge").getAsString());
    83. setTimezone(job.get("channel").getAsJsonObject().get("timezone").getAsString());
    84. setCategory_title(job.get("channel").getAsJsonObject().get("category_title").getAsString());
    85. setViews_count(job.get("channel").getAsJsonObject().get("views_count").getAsInt());
    86. }
    87.  
    88. public boolean isOnline()
    89. {
    90. return this.online; }
    91. public void setOnline(boolean online) { this.online = online; }
    92.  
    93. public int getBroadcast_part() {
    94. return this.broadcast_part; }
    95. public void setBroadcast_part(int broadcast_part) { this.broadcast_part = broadcast_part; }
    96. public boolean isFeatured() {
    97. return this.featured; }
    98. public void setFeatured(boolean featured) { this.featured = featured; }
    99. public boolean isChannel_subscription() {
    100. return this.channel_subscription; }
    101. public void setChannel_subscription(boolean channel_subscription) { this.channel_subscription = channel_subscription; }
    102. public String getId() {
    103. return this.id; }
    104. public void setId(String id) { this.id = id; }
    105. public String getCategory() {
    106. return this.category; }
    107. public void setCategory(String category) { this.category = category; }
    108. public String getTitle() {
    109. return this.title; }
    110. public void setTitle(String title) { this.title = title; }
    111. public int getChannel_count() {
    112. return this.channel_count; }
    113. public void setChannel_count(int channel_count) { this.channel_count = channel_count; }
    114. public int getVideo_height() {
    115. return this.video_height; }
    116. public void setVideo_height(int video_height) { this.video_height = video_height; }
    117. public int getSite_count() {
    118. return this.site_count; }
    119. public void setSite_count(int site_count) { this.site_count = site_count; }
    120. public boolean isEmbed_enabled() {
    121. return this.embed_enabled; }
    122. public void setEmbed_enabled(boolean embed_enabled) { this.embed_enabled = embed_enabled;
    123. }
    124.  
    125. public String getUp_time()
    126. {
    127. return this.up_time; }
    128. public void setUp_time(String up_time) { this.up_time = up_time; }
    129. public String getMeta_game() {
    130. return this.meta_game; }
    131. public void setMeta_game(String meta_game) { this.meta_game = meta_game; }
    132. public String getFormat() {
    133. return this.format; }
    134. public void setFormat(String format) { this.format = format; }
    135. public int getEmbed_count() {
    136. return this.embed_count; }
    137. public void setEmbed_count(int embed_count) { this.embed_count = embed_count; }
    138. public String getStream_type() {
    139. return this.stream_type; }
    140. public void setStream_type(String stream_type) { this.stream_type = stream_type; }
    141. public boolean isAbuse_reported() {
    142. return this.abuse_reported; }
    143. public void setAbuse_reported(boolean abuse_reported) { this.abuse_reported = abuse_reported; }
    144. public int getVideo_width() {
    145. return this.video_width; }
    146. public void setVideo_width(int video_width) { this.video_width = video_width; }
    147. public String getGeo() {
    148. return this.geo; }
    149. public void setGeo(String geo) { this.geo = geo; }
    150. public String getName() {
    151. return this.name; }
    152. public void setName(String name) { this.name = name; }
    153. public String getLanguage() {
    154. return this.language; }
    155. public void setLanguage(String language) { this.language = language; }
    156. public int getStream_count() {
    157. return this.stream_count; }
    158. public void setStream_count(int stream_count) { this.stream_count = stream_count; }
    159. public double getVideo_bitrate() {
    160. return this.video_bitrate; }
    161. public void setVideo_bitrate(double video_bitrate) { this.video_bitrate = video_bitrate; }
    162. public String getBroadcaster() {
    163. return this.broadcaster; }
    164. public void setBroadcaster(String broadcaster) { this.broadcaster = broadcaster; }
    165. public int getChannel_view_count() {
    166. return this.channel_view_count; }
    167. public void setChannel_view_count(int channel_view_count) { this.channel_view_count = channel_view_count; }
    168.  
    169. public String getUsername() {
    170. return this.username; }
    171. public void setUsername(String username) { this.username = username; }
    172. public String getStatus() {
    173. return this.status; }
    174. public void setStatus(String status) { this.status = status; }
    175. public String getChannel_url() {
    176. return this.channel_url; }
    177. public void setChannel_url(String channel_url) { this.channel_url = channel_url; }
    178. public boolean isProducer() {
    179. return this.producer; }
    180. public void setProducer(boolean producer) { this.producer = producer;
    181. }
    182.  
    183. public String getSubcategory_title()
    184. {
    185. return this.subcategory_title; }
    186. public void setSubcategory_title(String subcategory_title) { this.subcategory_title = subcategory_title; }
    187. public String getScreen_cap_url_large() {
    188. return this.screen_cap_url_large; }
    189. public void setScreen_cap_url_large(String screen_cap_url_large) { this.screen_cap_url_large = screen_cap_url_large; }
    190. public String getScreen_cap_url_small() {
    191. return this.screen_cap_url_small; }
    192. public void setScreen_cap_url_small(String screen_cap_url_small) { this.screen_cap_url_small = screen_cap_url_small; }
    193. public String getScreen_cap_url_medium() {
    194. return this.screen_cap_url_medium; }
    195. public void setScreen_cap_url_medium(String screen_cap_url_medium) { this.screen_cap_url_medium = screen_cap_url_medium; }
    196. public String getScreen_cap_url_huge() {
    197. return this.screen_cap_url_huge; }
    198. public void setScreen_cap_url_huge(String screen_cap_url_huge) { this.screen_cap_url_huge = screen_cap_url_huge; }
    199. public String getTimezone() {
    200. return this.timezone; }
    201. public void setTimezone(String timezone) { this.timezone = timezone; }
    202. public String getCategory_title() {
    203. return this.category_title; }
    204. public void setCategory_title(String category_title) { this.category_title = category_title; }
    205. public int getViews_count() {
    206. return this.views_count; }
    207. public void setViews_count(int views_count) { this.views_count = views_count;
    208. }
    209. }





    Twitch_API class:

    Code:java
    1. package me.CreepyEye.Twitch;
    2.  
    3. import com.google.gson.Gson;
    4. import com.google.gson.JsonArray;
    5. import com.google.gson.JsonObject;
    6.  
    7. public class Twitch_API {
    8. public static Gson gson = new Gson();
    9.  
    10.  
    11. public static Twitch_Stream getStream(String channelname){
    12. try{
    13. String json = API.readJsonFromUrl("[url]http://api.justin.tv/api/stream/list.json?channel=[/url]"+channelname);
    14.  
    15. Twitch_Stream stream = new Twitch_Stream();
    16. if(json.equalsIgnoreCase("[]")){
    17. stream.setOnline(false);
    18. return stream;
    19. }
    20. JsonArray jb = gson.fromJson(json, JsonArray.class);
    21. JsonObject jo = (JsonObject) jb.get(0);
    22. stream.setOnline(true);
    23. stream.load(jo);
    24. return stream;
    25. } catch (Exception error){
    26. error.printStackTrace();
    27. }
    28.  
    29.  
    30. return null;
    31.  
    32.  
    33. }
    34. }
    35.  




    API.class:
    Code:java
    1. package me.CreepyEye.Twitch;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.InputStreamReader;
    5. import java.net.URL;
    6.  
    7. public class API {
    8. public static String readJsonFromUrl(String urlString) throws Exception {
    9. BufferedReader reader = null;
    10. try {
    11. URL url = new URL(urlString);
    12. reader = new BufferedReader(new InputStreamReader(url.openStream()));
    13. StringBuffer buffer = new StringBuffer();
    14. int read;
    15. char[] chars = new char[1024];
    16. while ((read = reader.read(chars)) != -1)
    17. buffer.append(chars, 0, read);
    18.  
    19. return buffer.toString();
    20. } finally {
    21. if (reader != null)
    22. reader.close();
    23. }
    24. }
    25. }
    26.  



    i hope you have fun with this api you need google gson for it but bukkit has implement it already! i coded it without the bukkit api you need to change the imports to the bukkit or net.minecraft imports for eclipse users can you press CTRL + SHIFT + O the eclipse makes it automaticly for you
     
  2. Offline

    Not2EXceL

    You use lombok, but don't even use it properly. Just DaTa the class, it'll gen the getters and setters for you. No need for all those annotations.
     
  3. Oh rly xD? Ty i change it when i'm back on my pc
     
  4. Offline

    Skyost

  5. Any feedbacks?
     
  6. Offline

    Scrayos

    Pretty cool, even though it's just a wrapper-class for the REAL Twitch API. But it makes handling with it way easier. I think I'll implement this into my Plugin, to allow User's authenticating a Twitch-Account to their Minecraft-Account on my Server. (Cause you can't get the globally bound Twitch-Account (From the new Twitch-Features of 1.7)).
     
  7. Ty for your feedback i think there are a lot things what you can do with the api
     
  8. Offline

    CeramicTitan

    Please fix the formatting of the class files. For instance, Indentation, etc. :)
     
  9. Offline

    SoThatsIt

    does this still work? i am getting a MalformedURLException when using it

    Code:
    java.net.MalformedURLException: no protocol: [url]http://api.justin.tv/api/stream/list.json?channel=[/url]ThatGamingRiot
    [21:16:19 WARN]:        at java.net.URL.<init>(URL.java:585)
    [21:16:19 WARN]:        at java.net.URL.<init>(URL.java:482)
    [21:16:19 WARN]:        at java.net.URL.<init>(URL.java:431)
    [21:16:19 WARN]:        at me.sothatsit.joltcore.twitch.TwitchAPI.readJsonFromUrl(TwitchAPI.java:47)
    [21:16:19 WARN]:        at me.sothatsit.joltcore.twitch.TwitchAPI.getStream(TwitchAPI.java:19)
    [21:16:19 WARN]:        at me.sothatsit.joltcore.twitch.Twitcher.updateFromTwitch(Twitcher.java:101)
    [21:16:19 WARN]:        at me.sothatsit.joltcore.JoltCore$3.run(JoltCore.java:214)
    [21:16:19 WARN]:        at org.bukkit.craftbukkit.v1_7_R2.scheduler.CraftTask.run(CraftTask.java:67)
    [21:16:19 WARN]:        at org.bukkit.craftbukkit.v1_7_R2.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
    [21:16:19 WARN]:        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    [21:16:19 WARN]:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    [21:16:19 WARN]:        at java.lang.Thread.run(Thread.java:744)
     
  10. Offline

    lzravanger

    Change
    Code:java
    1. API.readJsonFromUrl("[url]http://api.justin.tv/api/stream/list.json?channel=[/url]"+channelname);
    to
    Code:java
    1. API.readJsonFromUrl("[url]http://api.justin.tv/api/stream/list.json?channel="+channelname+"[/url]");
     
Thread Status:
Not open for further replies.

Share This Page