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

Commit

Permalink
++Added | --Removed | *Extra
Browse files Browse the repository at this point in the history
------------------------------------------
++Json
++GetViewers
++isOp
--Bugs
  • Loading branch information
CavariuX committed Apr 19, 2015
1 parent 2f231d0 commit b51b45d
Show file tree
Hide file tree
Showing 12 changed files with 2,836 additions and 39 deletions.
47 changes: 47 additions & 0 deletions src/tk/cavariux/twitchirc/Chat/Channel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package tk.cavariux.twitchirc.Chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import tk.cavariux.twitchirc.Core.TwitchBot;
import tk.cavariux.twitchirc.Json.JsonArray;
import tk.cavariux.twitchirc.Json.JsonObject;
import tk.cavariux.twitchirc.Json.JsonValue;

public class Channel {

private String urln = "http://tmi.twitch.tv/group/user/$channel$/chatters";
private String channel;
private TwitchBot bot;

Expand Down Expand Up @@ -82,4 +92,41 @@ public final void unhost()
{
this.bot.sendMessage("/unhost", this);
}

public final String[] getViewers()
{
URL url;
try {
url = new URL(urln.replace("$channel$", channel.toString().substring(1)));
System.out.println(url);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = "";
String str = "";
while ((str = br.readLine()) != null)
{
inputLine = inputLine + str;
}
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine);
JsonArray array = jsonObj.get("chatters").asObject().get("viewers").asArray();
JsonArray array2 = jsonObj.get("chatters").asObject().get("moderators").asArray();
String[] viewers = new String[array.size() + array2.size()];
int i = 0;
for (JsonValue value : array)
{
viewers[i] = value.toString().substring(1, value.toString().length() - 1);
i++;
}
for (JsonValue value : array2)
{
viewers[i] = value.toString().substring(1, value.toString().length() - 1);
i++;
}
return viewers;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
44 changes: 43 additions & 1 deletion src/tk/cavariux/twitchirc/Chat/User.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package tk.cavariux.twitchirc.Chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import tk.cavariux.twitchirc.Json.JsonArray;
import tk.cavariux.twitchirc.Json.JsonObject;
import tk.cavariux.twitchirc.Json.JsonValue;

public class User
{

private String urln = "http://tmi.twitch.tv/group/user/$channel$/chatters";
private String user;

public User(String user)
Expand All @@ -14,4 +25,35 @@ public String toString()
{
return user;
}

public final boolean isOp(Channel channel)
{
URL url;
try {
url = new URL(urln.replace("$channel$", channel.toString().substring(1)));
System.out.println(url);
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = "";
String str = "";
while ((str = br.readLine()) != null)
{
inputLine = inputLine + str;
}
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine);
JsonArray array = jsonObj.get("chatters").asObject().get("moderators").asArray();
ArrayList<String> mods = new ArrayList<String>();
for (JsonValue value : array)
{
mods.add(value.toString().substring(1, value.toString().length() - 1));
}
if (mods.contains(this.toString()))
return true;
} catch (IOException e) {
e.printStackTrace();
}

return false;
}
}
78 changes: 40 additions & 38 deletions src/tk/cavariux/twitchirc/Core/TwitchBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;

import tk.cavariux.twitchirc.Chat.Channel;
import tk.cavariux.twitchirc.Chat.User;
Expand All @@ -19,45 +18,50 @@ public class TwitchBot {
private BufferedWriter writer;
private BufferedReader reader;
private ArrayList<String> channels = new ArrayList<String>();
private double version = 0.01;
private String version = "v1.0-alpha";

public TwitchBot(){}

public void connect() throws IOException
public void connect()
{
if (user == null || user == "")
{
System.err.println("Please select a valid Username");
System.exit(1);
return;
}
if (oauth_key == null || oauth_key == "")
{
System.err.println("Please select a valid Oauth_Key");
System.exit(2);
return;
}


@SuppressWarnings("resource")
Socket socket = new Socket("irc.twitch.tv", 6667);
this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

this.writer.write("PASS " + oauth_key + "\r\n");
this.writer.write("NICK " + user + "\r\n");
this.writer.write("USER " + "TwitchIRC v0.01 \r\n");
this.writer.flush();

String line = "";
while ((line = this.reader.readLine()) != null)
try{
if (user == null || user == "")
{
System.err.println("Please select a valid Username");
System.exit(1);
return;
}
if (oauth_key == null || oauth_key == "")
{
System.err.println("Please select a valid Oauth_Key");
System.exit(2);
return;
}


@SuppressWarnings("resource")
Socket socket = new Socket("irc.twitch.tv", 6667);
this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

this.writer.write("PASS " + oauth_key + "\r\n");
this.writer.write("NICK " + user + "\r\n");
this.writer.write("USER " + this.getVersion() + " \r\n");
this.writer.flush();

String line = "";
while ((line = this.reader.readLine()) != null)
{
if (line.indexOf("004") >= 0) {
System.out.println("Connected >> " + user + " ~ irc.twitch.tv");
break;
}else {
System.out.println(line);
}
}
} catch (IOException e)
{
if (line.indexOf("004") >= 0) {
System.out.println("Connected >> " + user + " ~ irc.twitch.tv");
break;
}else {
System.out.println(line);
}
e.printStackTrace();
}
}

Expand Down Expand Up @@ -151,8 +155,6 @@ public final void start()
this.writer.flush();
} else if (line.contains("PRIVMSG"))
{
/* Send The Messages to The onMessage Method */
//System.out.println(line);
String str[];
str = line.split("!");
final User msg_user = new User(str[0].substring(1, str[0].length()));
Expand All @@ -178,6 +180,6 @@ public final void start()

public final String getVersion()
{
return "TwitchIRC v"+version;
return "TwitchIRC "+version;
}
}
Loading

0 comments on commit b51b45d

Please sign in to comment.