diff --git a/src/com/cavariux/twitchirc/Chat/Channel.java b/src/com/cavariux/twitchirc/Chat/Channel.java index 1e765d9..49e4cd0 100644 --- a/src/com/cavariux/twitchirc/Chat/Channel.java +++ b/src/com/cavariux/twitchirc/Chat/Channel.java @@ -188,8 +188,7 @@ public final List getViewers() BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String inputLine = ""; String str = ""; - while ((str = br.readLine()) != null) - { + while ((str = br.readLine()) != null) { inputLine = inputLine + str; } br.close(); @@ -212,8 +211,7 @@ public final List getViewers() * Get the currently viewers (This method is on beta so it may not be optimized) * @return A String[] with all the current viewers */ - public final List getMods() - { + public final List getMods() { URL url; try { url = new URL(urln.replace("$channel$", channel.toString().substring(1))); @@ -221,9 +219,8 @@ public final List getMods() BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String inputLine = ""; String str = ""; - while ((str = br.readLine()) != null) - { - inputLine = inputLine + str; + while ((str = br.readLine()) != null) { + inputLine = inputLine + str; } br.close(); JsonObject jsonObj = JsonObject.readFrom(inputLine); @@ -243,8 +240,7 @@ public final List getMods() * @param user The user * @return true : false */ - public final boolean isMod(User user) - { + public final boolean isMod(User user) { return this.getMods().contains(user); } @@ -259,6 +255,7 @@ public final boolean isFollowing(User user) try { URL url = new URL("https://api.twitch.tv/kraken/users/" + user.toString().toLowerCase() + "/follows/channels/" + channel.substring(1).toLowerCase()); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); String str = jsonObj.get("channel").asObject().get("status").toString(); @@ -280,6 +277,7 @@ public final boolean isSubscribed(User user, String oauth_token) try { URL url = new URL("https://api.twitch.tv/kraken/channels/" + channel.substring(1).toLowerCase() + "/subscriptions/" + user.toString().toLowerCase() + "?oauth_token=" + oauth_token); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); String str = jsonObj.get("_id").asString(); @@ -295,18 +293,8 @@ public final boolean isSubscribed(User user, String oauth_token) * Check if the channel is in Stream * @return Returns the state */ - public final boolean isLive() - { - try { - URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); - URLConnection conn = url.openConnection(); - BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); - JsonObject jsonObj = JsonObject.readFrom(br.readLine()); - String str = jsonObj.get("stream").toString(); - return !str.equals("null"); - } catch (IOException ex) { - return false; - } + public final boolean isLive() { + return Channel.isLive(this, bot); } /** @@ -318,10 +306,10 @@ public final String getGame() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String strs = br.readLine(); JsonObject jsonObj = JsonObject.readFrom(strs); - System.out.println(strs); String str = jsonObj.get("stream").asObject().get("game").asString(); return str; } catch (IOException ex) { @@ -339,6 +327,7 @@ public final String getTitle() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); String str = jsonObj.get("stream").asObject().get("channel").asObject().get("status").asString(); @@ -357,6 +346,7 @@ public final int getViewersNum() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); int i = jsonObj.get("stream").asObject().get("viewers").asInt(); @@ -375,6 +365,7 @@ public final String getLanguange() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); String str = jsonObj.get("stream").asObject().get("channel").asObject().get("language").asString(); @@ -393,6 +384,7 @@ public final int getFollowersNum() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); int str = jsonObj.get("stream").asObject().get("channel").asObject().get("followers").asInt(); @@ -411,6 +403,7 @@ public final int getTotalViews() try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); int str = jsonObj.get("stream").asObject().get("channel").asObject().get("views").asInt(); @@ -425,11 +418,12 @@ public final int getTotalViews() * @param channel the channel to check * @return Returns the state */ - public static final boolean isLive(Channel channel) + public static final boolean isLive(Channel channel, TwitchBot bot) { try { URL url = new URL("https://api.twitch.tv/kraken/streams/" + channel.toString().substring(1)); URLConnection conn = url.openConnection(); + conn.setRequestProperty("Client-ID", bot.getClientID()); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); JsonObject jsonObj = JsonObject.readFrom(br.readLine()); String str = jsonObj.get("stream").toString(); diff --git a/src/com/cavariux/twitchirc/Core/TwitchBot.java b/src/com/cavariux/twitchirc/Core/TwitchBot.java index e121e98..55b14b0 100644 --- a/src/com/cavariux/twitchirc/Core/TwitchBot.java +++ b/src/com/cavariux/twitchirc/Core/TwitchBot.java @@ -29,6 +29,7 @@ public class TwitchBot { private String version = "v1.0-Beta"; private boolean stopped = true; private String commandTrigger = "!"; + private String clientID = ""; public TwitchBot(){} @@ -99,10 +100,30 @@ protected void onSub(User user, Channel channel, String message) * Set the username that the connect method will use * @param username Needs your Twitch Username to connect */ - public final void setUsername(String username) - { + public final void setUsername(String username) { this.user = username; } + + /** + * Sets the required ClientID for the use of the api. + * @param clientID + */ + public final void setClientID(String clientID) { + this.clientID = clientID; + } + + /** + * Method to return the clientid. + * @return The clientid + */ + public final String getClientID() { + if (this.clientID != null) + return this.clientID; + else { + System.out.println("You need to give a clientID to use the TwitchAPI"); + return ""; + } + } /** * Set the "password" that the connect method will use. @@ -220,8 +241,8 @@ public void sendMessage(Object message, Channel channel) */ public final Channel joinChannel (String channel) { - Channel cnl = Channel.getChannel(channel, this); - sendRawMessage("JOIN " + cnl + "\r\n"); + Channel cnl = Channel.getChannel(channel.toLowerCase(), this); + sendRawMessage("JOIN " + cnl.toString().toLowerCase() + "\r\n"); this.channels.add(cnl.toString()); System.out.println("> JOIN " + cnl); return cnl; @@ -233,7 +254,7 @@ public final Channel joinChannel (String channel) */ public final void partChannel (String channel) { - Channel cnl = Channel.getChannel(channel, this); + Channel cnl = Channel.getChannel(channel.toLowerCase(), this); this.sendRawMessage("PART " + cnl); this.channels.remove(cnl); System.out.println("> PART " + channel);