Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lettercounter;
- import java.util.*;
- import java.net.*;
- import java.io.*;
- import javax.imageio.*;
- import javax.swing.*;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * LetterCounterForm.java
- *
- * Created on May 31, 2011, 4:34:17 PM
- */
- /**
- *
- * @author SteveDesktop
- */
- public class LetterCounterForm extends javax.swing.JFrame {
- private String token;
- private ArrayList<String> friends = new ArrayList<String>();
- private ArrayList<String> words = new ArrayList<String>();
- private ArrayList<String> wordsCount = new ArrayList<String>();
- private DefaultListModel sampleModel;
- /** Creates new form LetterCounterForm */
- public LetterCounterForm() {
- initComponents();
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- lblAccessToken = new javax.swing.JLabel();
- txtToken = new javax.swing.JTextField();
- btnGo = new javax.swing.JButton();
- jScrollPane1 = new javax.swing.JScrollPane();
- lstLetters = new javax.swing.JList();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- lblAccessToken.setText("Access Token");
- btnGo.setText("Go");
- btnGo.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnGoActionPerformed(evt);
- }
- });
- jScrollPane1.setViewportView(lstLetters);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
- .addComponent(btnGo)
- .addComponent(txtToken, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(10, 10, 10)
- .addComponent(lblAccessToken)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(lblAccessToken)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(txtToken, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(btnGo)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- if (txtToken.getText().indexOf("?access_token") > -1) {
- btnGo.setEnabled(false);
- token = txtToken.getText().substring(txtToken.getText().indexOf("?access_token"));
- Thread letterThread = new Thread() {
- @Override
- public void run() {
- getFriends();
- scanComments();
- }
- };
- letterThread.start();
- }
- }
- public void getFriends() {
- String[] pgTxt = readFromURL("https://graph.facebook.com/me/friends" + token).split("\\{");
- String ct;
- String curName;
- String curID;
- for (int i = 0; i < pgTxt.length - 1; i++) {
- if (pgTxt[i].startsWith("\"name\"")) {
- ct = pgTxt[i];
- curName = ct.substring(8, ct.indexOf(",") - 1);
- curID = ct.substring(ct.indexOf(":", 9) + 2, ct.length() - 3);
- friends.add(curName + "," + curID);
- }
- try {
- Thread.sleep(15);
- } catch (Exception e) {
- }
- }
- }
- public void scanComments() {
- for (int i = 0; i < friends.size(); i++) {
- String name = friends.get(i).split(",")[0];
- String id = friends.get(i).split(",")[1];
- System.out.println("-----------");
- System.out.println(name);
- System.out.println("-----------");
- String currentPage = "https://graph.facebook.com/" + id + "/statuses" + token;
- String page = readFromURL(currentPage);
- String[] pageMessages = page.split("message");
- for (int j = 1; j < pageMessages.length; j++) {
- String cutMsg = pageMessages[j].split("\",\"")[0].substring(3);
- 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();
- System.out.println(cutMsg);
- String[] tw = cutMsg.split(" ");
- System.out.println(tw);
- for (int k = 0; k < tw.length; k++) {
- if (tw[k] != "" && !(tw[k].startsWith("u0"))) {
- tw[k] = tw[k].replace(" ", "");
- boolean found = false;
- for (int m = 0; m < words.size(); m++) {
- //System.out.println("======");
- //System.out.println(words.get(m) + "," + tw[k]);
- if (tw[k] == words.get(m)) {
- wordsCount.set(m, "" + (Integer.parseInt(wordsCount.get(m)) + 1));
- System.out.println("FOUND A FUCKING MATCH!" + tw[k]);
- found = true;
- bubbleSort(words, wordsCount);
- break;
- }
- try {
- Thread.sleep(5);
- } catch(Exception e) {
- }
- }
- if (!found) {
- words.add(tw[k]);
- wordsCount.add("1");
- }
- }
- }
- }
- }
- }
- public int letterToIndex(char letter) {
- if ((((int) (letter) - 97) >= 0) && (((int) (letter) - 97) < lstLetters.getModel().getSize())) {
- return (int) (letter) - 97;
- } else {
- return -1;
- }
- }
- public static void bubbleSort(ArrayList<String> x, ArrayList<String> xc) {
- boolean doMore = true;
- while (doMore) {
- doMore = false; // assume this is last pass over array
- for (int i = 0; i < x.size() - 1; i++) {
- if (Integer.parseInt(xc.get(i).toString()) > Integer.parseInt(xc.get(i + 1).toString())) {
- // exchange elements
- String temp = x.get(i).toString();
- x.set(i, x.get(i + 1));
- x.set(i + 1, temp);
- temp = xc.get(i).toString();
- xc.set(i, xc.get(i + 1));
- xc.set(i + 1, temp);
- doMore = true; // after an exchange, must look again
- }
- }
- }
- }
- public String readFromURL(String in_url) {
- URL url;
- URLConnection conn;
- InputStreamReader inStream;
- BufferedReader buff;
- try {
- url = new URL(in_url);
- conn = url.openConnection();
- inStream = new InputStreamReader(conn.getInputStream());
- buff = new BufferedReader(inStream);
- String curText = null;
- String allText = null;
- while (true) {
- curText = buff.readLine();
- if (curText != null) {
- allText += curText;
- } else {
- break;
- }
- try {
- Thread.sleep(5);
- } catch (Exception e) {
- }
- }
- return allText;
- } catch (MalformedURLException e1) {
- System.out.println("Please Check the URL (" + in_url + ") " + e1.toString());
- } catch (IOException e2) {
- System.out.println("Can't read from the internet! " + e2.toString());
- }
- return null;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new LetterCounterForm().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnGo;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JLabel lblAccessToken;
- private javax.swing.JList lstLetters;
- private javax.swing.JTextField txtToken;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement