Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

Commit

Permalink
Added the request ClientID header for the use of the api
Browse files Browse the repository at this point in the history
  • Loading branch information
cavariux committed Apr 30, 2017
1 parent 4685ec2 commit c06252f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
40 changes: 17 additions & 23 deletions src/com/cavariux/twitchirc/Chat/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ public final List<User> 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();
Expand All @@ -212,18 +211,16 @@ public final List<User> 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<User> getMods()
{
public final List<User> getMods() {
URL url;
try {
url = new URL(urln.replace("$channel$", channel.toString().substring(1)));
URLConnection conn = url.openConnection();
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);
Expand All @@ -243,8 +240,7 @@ public final List<User> 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);
}

Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
31 changes: 26 additions & 5 deletions src/com/cavariux/twitchirc/Core/TwitchBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(){}

Expand Down Expand Up @@ -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 <a href="http://www.twitch.tv">Twitch</a> 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 <a href="connect">connect</a> method will use.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit c06252f

Please sign in to comment.