Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.79 KB | None | 0 0
  1. package zogzer.logic;
  2.  
  3. import java.awt.Color;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.net.Socket;
  10. import java.net.URL;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Calendar;
  13.  
  14. import javax.swing.text.BadLocationException;
  15. import javax.swing.text.StyleConstants;
  16.  
  17. import zogzer.Settings;
  18. import zogzer.gui.Chatwindow;
  19. import zogzer.gui.Mainwindow;
  20. import zogzer.gui.Popout;
  21.  
  22. public class Twitch implements Runnable {
  23.  
  24.     Thread thread = new Thread();
  25.     public static boolean running;
  26.  
  27.     private Socket irc;
  28.     public static BufferedWriter writer;
  29.     private BufferedReader reader;
  30.  
  31.     private String host = "irc.twitch.tv";
  32.     private int port = 6667;
  33.  
  34.     private String line;
  35.     private String[] parts;
  36.  
  37.     private String user;
  38.     public static String lastuser;
  39.     private String message;
  40.  
  41.     private static String mods;
  42.     private static String staff;
  43.     private static String admins;
  44.  
  45.     private static String name = Settings.username;
  46.  
  47.     public void start() {
  48.         thread = new Thread(this, "Twitch Thread");
  49.         thread.start();
  50.     }
  51.  
  52.     // phask #7878ff
  53.     // karumu #ac62ff
  54.  
  55.     private void update() {
  56.         try {
  57.             URL twitchAPI = new URL("http://tmi.twitch.tv/group/user/" + name + "/chatters");
  58.             BufferedReader in = new BufferedReader(new InputStreamReader(twitchAPI.openStream()));
  59.  
  60.             String inputLine;
  61.             mods = "";
  62.             staff = "";
  63.             admins = "";
  64.             while ((inputLine = in.readLine()) != null) {
  65.                 mods = mods + inputLine;
  66.                 staff = staff + inputLine;
  67.                 admins = admins + inputLine;
  68.             }
  69.             in.close();
  70.             mods = mods.replace(" ", "");
  71.             mods = mods.split("\"moderators\":")[1].substring(1);
  72.             mods = mods.split("],")[0];
  73.             staff = staff.replace(" ", "");
  74.             staff = staff.split("\"staff\":")[1].substring(1);
  75.             staff = staff.split("],")[0];
  76.             admins = admins.replace(" ", "");
  77.             admins = admins.split("\"admins\":")[1].substring(1);
  78.             admins = admins.split("],")[0];
  79.         } catch (IOException e) {
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.  
  84.     public static void printMessage(String user, String colour, String message) {
  85.         try {
  86.             if (user.equals(lastuser)) {
  87.                 if (message.startsWith("")) {
  88.                     StyleConstants.setForeground(Popout.style, Color.WHITE);
  89.                     Popout.doc.insertString(Popout.doc.getLength(), "» * " + message.substring(8).substring(0, message.length() - 9) + "\r\n", Popout.style);
  90.                 } else {
  91.                     StyleConstants.setForeground(Popout.style, Color.WHITE);
  92.                     Popout.doc.insertString(Popout.doc.getLength(), "» " + message + "\r\n", Popout.style);
  93.                 }
  94.             } else {
  95.                 if (user.equals("zogzer")) {
  96.                     StyleConstants.setForeground(Popout.style, Color.decode(colour));
  97.                     Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  98.                 } else {
  99.                     if (user.equals(name)) {
  100.                         StyleConstants.setForeground(Popout.style, Color.decode(colour));
  101.                         Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  102.                     } else {
  103.                         if (mods.contains("\"" + user + "\"")) {
  104.                             StyleConstants.setForeground(Popout.style, Color.decode(colour));
  105.                             Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  106.                         } else {
  107.                             if (staff.contains("\"" + user + "\"")) {
  108.                                 StyleConstants.setForeground(Popout.style, Color.decode(colour));
  109.                                 Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  110.                             } else {
  111.                                 if (admins.contains("\"" + user + "\"")) {
  112.                                     StyleConstants.setForeground(Popout.style, Color.decode(colour));
  113.                                     Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  114.                                 } else {
  115.                                     StyleConstants.setForeground(Popout.style, Color.decode(colour));
  116.                                     Popout.doc.insertString(Popout.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Popout.style);
  117.                                 }
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.                 StyleConstants.setForeground(Popout.style, Color.WHITE);
  123.                 Popout.doc.insertString(Popout.doc.getLength(), ": " + message + "\r\n", Popout.style);
  124.                 lastuser = user;
  125.             }
  126.         } catch (BadLocationException e) {
  127.             e.printStackTrace();
  128.         }
  129.     }
  130.  
  131.     public static String getColour(String user) {
  132.         if (user.equals("zogzer")) return Settings.mycolour;
  133.         else if (user.equals(name)) return "#34FAF7";
  134.         else if (mods.contains("\"" + user + "\"")) return "#00E700";
  135.         else if (staff.contains("\"" + user + "\"")) return "#40A0FF";
  136.         else if (admins.contains("\"" + user + "\"")) return "#EE2C2C";
  137.         else return "#787878";
  138.     }
  139.  
  140.     public static String time() {
  141.         Calendar cal = Calendar.getInstance();
  142.         cal.getTime();
  143.         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  144.         String time = sdf.format(cal.getTime());
  145.         return time;
  146.     }
  147.  
  148.     public void run() {
  149.         running = true;
  150.  
  151.         try {
  152.             irc = new Socket(host, port);
  153.             writer = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream(), "UTF8"));
  154.             reader = new BufferedReader(new InputStreamReader(irc.getInputStream(), "UTF8"));
  155.             writer.write("PASS " + Settings.ouathkey + "\r\n");
  156.             writer.write("NICK " + Settings.username + "\r\n");
  157.             writer.write("USER " + Settings.username + " " + host + " " + Settings.username + "\r\n");
  158.             writer.flush();
  159.             writer.write("JOIN #" + name + "\r\n");
  160.             writer.flush();
  161.  
  162.             writer.flush();
  163.  
  164.             Thread updatethread = new Thread() {
  165.                 public void run() {
  166.                     while (true) {
  167.                         update();
  168.                         try {
  169.                             Thread.sleep(30000);
  170.                         } catch (InterruptedException e) {
  171.                             e.printStackTrace();
  172.                         }
  173.                     }
  174.                 }
  175.             };
  176.  
  177.             mods = "";
  178.             staff = "";
  179.             admins = "";
  180.             updatethread.start();
  181.  
  182.             while ((line = reader.readLine()) != null) {
  183.                 if (line.startsWith("PING ")) {
  184.                     writer.write("PONG " + line.substring(5) + "\r\n");
  185.                     writer.flush();
  186.                 } else {
  187.                     parts = line.split(" ");
  188.                     System.out.println(line);
  189.                     if (parts[1].equals("PRIVMSG")) {
  190.                         if (parts[2].startsWith("#")) {
  191.                             user = ((parts[0].split("!"))[0]).substring(1);
  192.                             message = line.substring((parts[0] + parts[1] + parts[2]).length() + 4);
  193.                             if (Popout.open) printMessage(user, getColour(user), message);
  194.                             if (Mainwindow.chatdrawn) {
  195.                                 StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
  196.                                 Chatwindow.doc.insertString(Chatwindow.doc.getLength(), "[" + time() + "] ", Chatwindow.style);
  197.                                 if (user.equals("zogzer")) {
  198.                                     StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.mycolourdark));
  199.                                     Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  200.                                 } else {
  201.                                     if (user.equals(name)) {
  202.                                         StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.host));
  203.                                         Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  204.                                     } else {
  205.                                         if (mods.contains("\"" + user + "\"")) {
  206.                                             StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.mod));
  207.                                             Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  208.                                         } else {
  209.                                             if (staff.contains("\"" + user + "\"")) {
  210.                                                 StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.staff));
  211.                                                 Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  212.                                             } else {
  213.                                                 if (admins.contains("\"" + user + "\"")) {
  214.                                                     StyleConstants.setForeground(Chatwindow.style, Color.decode(Settings.admin));
  215.                                                     Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  216.                                                 } else {
  217.                                                     StyleConstants.setForeground(Chatwindow.style, Color.decode("#515151"));
  218.                                                     Chatwindow.doc.insertString(Chatwindow.doc.getLength(), (Character.toUpperCase(user.charAt(0)) + user.substring(1)), Chatwindow.style);
  219.                                                 }
  220.                                             }
  221.                                         }
  222.                                     }
  223.                                 }
  224.  
  225.                                 if (message.startsWith("")) {
  226.                                     StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
  227.                                     Chatwindow.doc.insertString(Chatwindow.doc.getLength(), ": * " + message.substring(8).substring(0, message.length() - 9) + "\r\n", Chatwindow.style);
  228.                                 } else {
  229.                                     StyleConstants.setForeground(Chatwindow.style, Color.BLACK);
  230.                                     Chatwindow.doc.insertString(Chatwindow.doc.getLength(), ": " + message + "\r\n", Chatwindow.style);
  231.                                 }
  232.                                 Chatwindow.cutChat();
  233.                             }
  234.                             lastuser = user;
  235.                         }
  236.                     }
  237.  
  238.                 }
  239.             }
  240.         } catch (Exception e) {
  241.             e.printStackTrace();
  242.         }
  243.  
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement