Advertisement
Guest User

Untitled

a guest
May 31st, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.71 KB | None | 0 0
  1. package lettercounter;
  2.  
  3. import java.util.*;
  4. import java.net.*;
  5. import java.io.*;
  6. import javax.imageio.*;
  7. import javax.swing.*;
  8.  
  9. /*
  10.  * To change this template, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /*
  15.  * LetterCounterForm.java
  16.  *
  17.  * Created on May 31, 2011, 4:34:17 PM
  18.  */
  19. /**
  20.  *
  21.  * @author SteveDesktop
  22.  */
  23. public class LetterCounterForm extends javax.swing.JFrame {
  24.  
  25.     private String token;
  26.     private ArrayList<String> friends = new ArrayList<String>();
  27.     private ArrayList<String> words = new ArrayList<String>();
  28.     private ArrayList<String> wordsCount = new ArrayList<String>();
  29.     private DefaultListModel sampleModel;
  30.  
  31.     /** Creates new form LetterCounterForm */
  32.     public LetterCounterForm() {
  33.         initComponents();
  34.     }
  35.  
  36.     /** This method is called from within the constructor to
  37.      * initialize the form.
  38.      * WARNING: Do NOT modify this code. The content of this method is
  39.      * always regenerated by the Form Editor.
  40.      */
  41.     @SuppressWarnings("unchecked")
  42.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  43.     private void initComponents() {
  44.  
  45.         lblAccessToken = new javax.swing.JLabel();
  46.         txtToken = new javax.swing.JTextField();
  47.         btnGo = new javax.swing.JButton();
  48.         jScrollPane1 = new javax.swing.JScrollPane();
  49.         lstLetters = new javax.swing.JList();
  50.  
  51.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  52.  
  53.         lblAccessToken.setText("Access Token");
  54.  
  55.         btnGo.setText("Go");
  56.         btnGo.addActionListener(new java.awt.event.ActionListener() {
  57.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  58.                 btnGoActionPerformed(evt);
  59.             }
  60.         });
  61.  
  62.         jScrollPane1.setViewportView(lstLetters);
  63.  
  64.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  65.         getContentPane().setLayout(layout);
  66.         layout.setHorizontalGroup(
  67.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  68.             .addGroup(layout.createSequentialGroup()
  69.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.                     .addGroup(layout.createSequentialGroup()
  71.                         .addContainerGap()
  72.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
  73.                             .addComponent(btnGo)
  74.                             .addComponent(txtToken, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
  75.                     .addGroup(layout.createSequentialGroup()
  76.                         .addContainerGap()
  77.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
  78.                     .addGroup(layout.createSequentialGroup()
  79.                         .addGap(10, 10, 10)
  80.                         .addComponent(lblAccessToken)))
  81.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  82.         );
  83.         layout.setVerticalGroup(
  84.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  85.             .addGroup(layout.createSequentialGroup()
  86.                 .addContainerGap()
  87.                 .addComponent(lblAccessToken)
  88.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  89.                 .addComponent(txtToken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  90.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  91.                 .addComponent(btnGo)
  92.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  93.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
  94.                 .addContainerGap())
  95.         );
  96.  
  97.         pack();
  98.     }// </editor-fold>                        
  99.  
  100.     private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {                                      
  101.         // TODO add your handling code here:
  102.         if (txtToken.getText().indexOf("?access_token") > -1) {
  103.             btnGo.setEnabled(false);
  104.  
  105.             token = txtToken.getText().substring(txtToken.getText().indexOf("?access_token"));
  106.  
  107.             Thread letterThread = new Thread() {
  108.  
  109.                 @Override
  110.                 public void run() {
  111.                     getFriends();
  112.                     scanComments();
  113.                 }
  114.             };
  115.             letterThread.start();
  116.         }
  117.     }                                    
  118.  
  119.     public void getFriends() {
  120.         String[] pgTxt = readFromURL("https://graph.facebook.com/me/friends" + token).split("\\{");
  121.  
  122.         String ct;
  123.         String curName;
  124.         String curID;
  125.  
  126.         for (int i = 0; i < pgTxt.length - 1; i++) {
  127.             if (pgTxt[i].startsWith("\"name\"")) {
  128.                 ct = pgTxt[i];
  129.  
  130.                 curName = ct.substring(8, ct.indexOf(",") - 1);
  131.                 curID = ct.substring(ct.indexOf(":", 9) + 2, ct.length() - 3);
  132.  
  133.                 friends.add(curName + "," + curID);
  134.             }
  135.  
  136.             try {
  137.                 Thread.sleep(15);
  138.             } catch (Exception e) {
  139.             }
  140.         }
  141.     }
  142.  
  143.     public void scanComments() {
  144.         for (int i = 0; i < friends.size(); i++) {
  145.             String name = friends.get(i).split(",")[0];
  146.             String id = friends.get(i).split(",")[1];
  147.  
  148.  
  149.             System.out.println("-----------");
  150.             System.out.println(name);
  151.             System.out.println("-----------");
  152.  
  153.  
  154.             String currentPage = "https://graph.facebook.com/" + id + "/statuses" + token;
  155.  
  156.             String page = readFromURL(currentPage);
  157.             String[] pageMessages = page.split("message");
  158.  
  159.             for (int j = 1; j < pageMessages.length; j++) {
  160.                 String cutMsg = pageMessages[j].split("\",\"")[0].substring(3);
  161.  
  162.                 cutMsg = cutMsg.replace("~", " ").replace(".", " ").replace("!", " ").replace("?", " ").replace("/", " ").replace("\\", " ").replace(",", " ").replace("@", " ").replace("#", " ").replace("$", " ").replace("%", " ").replace("^", " ").replace("&", " ").replace("*", " ").replace("(", " ").replace(")", " ").replace("'", " ").replace("\\n", " ").replace("-", " ").replace(":", " ").replace("\"", " ").toLowerCase();
  163.  
  164.                 System.out.println(cutMsg);
  165.  
  166.                 String[] tw = cutMsg.split(" ");
  167.                 System.out.println(tw);
  168.                 for (int k = 0; k < tw.length; k++) {
  169.                     if (tw[k] != "" && !(tw[k].startsWith("u0")))  {
  170.                         tw[k] = tw[k].replace(" ", "");
  171.                         boolean found = false;
  172.                         for (int m = 0; m < words.size(); m++) {
  173.                             //System.out.println("======");
  174.                             //System.out.println(words.get(m) + "," + tw[k]);
  175.                             if (tw[k] == words.get(m)) {
  176.                                 wordsCount.set(m, "" + (Integer.parseInt(wordsCount.get(m)) + 1));
  177.                                 System.out.println("FOUND A FUCKING MATCH!" + tw[k]);
  178.                                 found = true;
  179.                                 bubbleSort(words, wordsCount);
  180.                                 break;
  181.                             }
  182.                            
  183.                             try {
  184.                                 Thread.sleep(5);
  185.                             } catch(Exception e) {
  186.                             }
  187.                         }
  188.  
  189.                         if (!found) {
  190.                             words.add(tw[k]);
  191.                             wordsCount.add("1");
  192.                         }
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.     }
  198.  
  199.     public int letterToIndex(char letter) {
  200.         if ((((int) (letter) - 97) >= 0) && (((int) (letter) - 97) < lstLetters.getModel().getSize())) {
  201.             return (int) (letter) - 97;
  202.         } else {
  203.             return -1;
  204.         }
  205.     }
  206.  
  207.     public static void bubbleSort(ArrayList<String> x, ArrayList<String> xc) {
  208.         boolean doMore = true;
  209.         while (doMore) {
  210.             doMore = false;  // assume this is last pass over array
  211.             for (int i = 0; i < x.size() - 1; i++) {
  212.                 if (Integer.parseInt(xc.get(i).toString()) > Integer.parseInt(xc.get(i + 1).toString())) {
  213.                     // exchange elements
  214.                     String temp = x.get(i).toString();
  215.                     x.set(i, x.get(i + 1));
  216.                     x.set(i + 1, temp);
  217.  
  218.                     temp = xc.get(i).toString();
  219.                     xc.set(i, xc.get(i + 1));
  220.                     xc.set(i + 1, temp);
  221.  
  222.                     doMore = true;  // after an exchange, must look again
  223.                 }
  224.             }
  225.         }
  226.     }
  227.  
  228.     public String readFromURL(String in_url) {
  229.         URL url;
  230.         URLConnection conn;
  231.         InputStreamReader inStream;
  232.         BufferedReader buff;
  233.  
  234.         try {
  235.             url = new URL(in_url);
  236.             conn = url.openConnection();
  237.             inStream = new InputStreamReader(conn.getInputStream());
  238.             buff = new BufferedReader(inStream);
  239.  
  240.             String curText = null;
  241.             String allText = null;
  242.             while (true) {
  243.                 curText = buff.readLine();
  244.  
  245.                 if (curText != null) {
  246.                     allText += curText;
  247.                 } else {
  248.                     break;
  249.                 }
  250.  
  251.                 try {
  252.                     Thread.sleep(5);
  253.                 } catch (Exception e) {
  254.                 }
  255.             }
  256.  
  257.             return allText;
  258.         } catch (MalformedURLException e1) {
  259.             System.out.println("Please Check the URL (" + in_url + ") " + e1.toString());
  260.         } catch (IOException e2) {
  261.             System.out.println("Can't read from the internet! " + e2.toString());
  262.         }
  263.  
  264.         return null;
  265.     }
  266.  
  267.     /**
  268.      * @param args the command line arguments
  269.      */
  270.     public static void main(String args[]) {
  271.         java.awt.EventQueue.invokeLater(new Runnable() {
  272.  
  273.             public void run() {
  274.                 new LetterCounterForm().setVisible(true);
  275.             }
  276.         });
  277.     }
  278.     // Variables declaration - do not modify                    
  279.     private javax.swing.JButton btnGo;
  280.     private javax.swing.JScrollPane jScrollPane1;
  281.     private javax.swing.JLabel lblAccessToken;
  282.     private javax.swing.JList lstLetters;
  283.     private javax.swing.JTextField txtToken;
  284.     // End of variables declaration                  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement