Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zogzer.logic;
- import java.awt.Color;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.Socket;
- import java.net.URL;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.StyleConstants;
- import zogzer.Settings;
- import zogzer.gui.Chatwindow;
- import zogzer.gui.Mainwindow;
- import zogzer.gui.Popout;
- public class Twitch implements Runnable {
- Thread thread = new Thread();
- public static boolean running;
- private Socket irc;
- public static BufferedWriter writer;
- private BufferedReader reader;
- private String host = "irc.twitch.tv";
- private int port = 6667;
- private String line;
- private String[] parts;
- private String user;
- public static String lastuser;
- private String message;
- private static String mods;
- private static String staff;
- private static String admins;
- private static String name = Settings.username;
- public void start() {
- thread = new Thread(this, "Twitch Thread");
- thread.start();
- }
- // phask #7878ff
- // karumu #ac62ff
- private void update() {
- try {
- URL twitchAPI = new URL("http://tmi.twitch.tv/group/user/" + name + "/chatters");
- BufferedReader in = new BufferedReader(new InputStreamReader(twitchAPI.openStream()));
- String inputLine;
- mods = "";
- staff = "";
- admins = "";
- while ((inputLine = in.readLine()) != null) {
- mods = mods + inputLine;
- staff = staff + inputLine;
- admins = admins + inputLine;
- }
- in.close();
- mods = mods.replace(" ", "");
- mods = mods.split("\"moderators\":")[1].substring(1);
- mods = mods.split("],")[0];
- staff = staff.replace(" ", "");
- staff = staff.split("\"staff\":")[1].substring(1);
- staff = staff.split("],")[0];
- admins = admins.replace(" ", "");
- admins = admins.split("\"admins\":")[1].substring(1);
- admins = admins.split("],")[0];
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void printMessage(String user, String colour, String message) {
- try {
- if (user.equals(lastuser)) {
- if (message.startsWith("")) {
- StyleConstants.setForeground(Popout.style, Color.WHITE);
- Popout.doc.insertString(Popout.doc.getLength(), "» * " + message.substring(8).substring(0, message.length() - 9) + "\r\n", Popout.style);
- } else {
- StyleConstants.setForeground(Popout.style, Color.WHITE);
- Popout.doc.insertString(Popout.doc.getLength(), "» " + message + "\r\n", Popout.style);
- }
- } else {
- if (user.equals("zogzer")) {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- } else {
- if (user.equals(name)) {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- } else {
- if (mods.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- } else {
- if (staff.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- } else {
- if (admins.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- } else {
- StyleConstants.setForeground(Popout.style, Color.decode(colour));
- Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
- }
- }
- }
- }
- }
- StyleConstants.setForeground(Popout.style, Color.WHITE);
- Popout.doc.insertString(Popout.doc.getLength(), ": " + message + "\r\n", Popout.style);
- lastuser = user;
- }
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
- }
- public static String getColour(String user) {
- if (user.equals("zogzer")) return Settings.mycolour;
- else if (user.equals(name)) return "#34FAF7";
- else if (mods.contains("\"" + user + "\"")) return "#00E700";
- else if (staff.contains("\"" + user + "\"")) return "#40A0FF";
- else if (admins.contains("\"" + user + "\"")) return "#EE2C2C";
- else return "#787878";
- }
- public static String time() {
- Calendar cal = Calendar.getInstance();
- cal.getTime();
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
- String time = sdf.format(cal.getTime());
- return time;
- }
- public void run() {
- running = true;
- try {
- irc = new Socket(host, port);
- writer = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream(), "UTF8"));
- reader = new BufferedReader(new InputStreamReader(irc.getInputStream(), "UTF8"));
- writer.write("PASS " + Settings.ouathkey + "\r\n");
- writer.write("NICK " + Settings.username + "\r\n");
- writer.write("USER " + Settings.username + " " + host + " " + Settings.username + "\r\n");
- writer.flush();
- writer.write("JOIN #" + name + "\r\n");
- writer.flush();
- writer.flush();
- Thread updatethread = new Thread() {
- public void run() {
- while (true) {
- update();
- try {
- Thread.sleep(30000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- };
- mods = "";
- staff = "";
- admins = "";
- updatethread.start();
- while ((line = reader.readLine()) != null) {
- if (line.startsWith("PING ")) {
- writer.write("PONG " + line.substring(5) + "\r\n");
- writer.flush();
- } else {
- parts = line.split(" ");
- System.out.println(line);
- if (parts[1].equals("PRIVMSG")) {
- if (parts[2].startsWith("#")) {
- user = ((parts[0].split("!"))[0]).substring(1);
- message = line.substring((parts[0] + parts[1] + parts[2]).length() + 4);
- if (Popout.open) printMessage(user, getColour(user), message);
- if (Mainwindow.chatdrawn) {
- StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), "[" + time() + "] ", Chatwindow.style);
- if (user.equals("zogzer")) {
- StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.mycolourdark));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- } else {
- if (user.equals(name)) {
- StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.host));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- } else {
- if (mods.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.mod));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- } else {
- if (staff.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.staff));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- } else {
- if (admins.contains("\"" + user + "\"")) {
- StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.admin));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- } else {
- StyleConstants.setForeground(Chatwindow.style, Color.decode("#515151"));
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
- }
- }
- }
- }
- }
- if (message.startsWith("")) {
- StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), ": * " + message.substring(8).substring(0, message.length() - 9) + "\r\n", Chatwindow.style);
- } else {
- StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
- Chatwindow.doc.insertString(Chatwindow.doc.getLength(), ": " + message + "\r\n", Chatwindow.style);
- }
- Chatwindow.cutChat();
- }
- lastuser = user;
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement