twitter.metrics.TwitterMetrics.java Source code

Java tutorial

Introduction

Here is the source code for twitter.metrics.TwitterMetrics.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package twitter.metrics;

import twitter4j.Paging;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author aaron
 */
public class TwitterMetrics {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {

            /*Parte que guarda en un archivo*/
            // Se crea el libro
            HSSFWorkbook libro = new HSSFWorkbook();

            // Se crea una hoja dentro del libro
            HSSFSheet hoja = libro.createSheet();

            // Se crea una fila dentro de la hoja
            HSSFRow fila = hoja.createRow(0);

            // Se crea una celda dentro de la fila
            HSSFCell celda = fila.createCell(1);

            // Se crea el contenido de la celda y se mete en ella.
            HSSFRichTextString texto = new HSSFRichTextString("Metricas de Twitter");
            celda.setCellValue(texto);
            /************************************/

            /*Conexion con Mongo DB*/
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            DB db = mongoClient.getDB("JavaMongoTwitter");

            DBCollection datos = db.getCollection("Datos");

            /***********************/

            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true);

            cb.setOAuthConsumerKey("TobUISZXWUhDda04ZBtFGQ");
            cb.setOAuthConsumerSecret("7xurVN3iP6VDcBfKdFJxVuNsJjExERFYNmQIDgtg");

            cb.setOAuthAccessToken("849956971-GJBiORhLIuWK4i3MJ2YCd4vidh65N1GzPIb6duXk");
            cb.setOAuthAccessTokenSecret("4MJgPS9grxVuKbczrPCdSjNnumhcWs7t7OLy2F4kkpOdu");

            TwitterFactory tf = new TwitterFactory(cb.build());
            Twitter twitter = tf.getInstance();

            User u = twitter.showUser("Pringles");

            System.out.println("Nombre: " + u.getName());
            System.out.println("Seguidores: " + u.getFollowersCount());
            System.out.println("Favoritos: " + u.getFavouritesCount());
            System.out.println("Seguidos: " + u.getFriendsCount());
            System.out.println("Ubicacion: " + u.getLocation());
            System.out.println("Descripcin: " + u.getDescription());
            System.out.println("");

            fila = hoja.createRow(1);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Nombre:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getName()));

            fila = hoja.createRow(2);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Seguidores:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getFollowersCount() + ""));

            fila = hoja.createRow(3);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Favoritos:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getFavouritesCount() + ""));

            fila = hoja.createRow(4);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Seguidos:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getFriendsCount() + ""));

            fila = hoja.createRow(5);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Ubicacin:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getLocation() + ""));

            fila = hoja.createRow(6);
            celda = fila.createCell(0);
            celda.setCellValue(new HSSFRichTextString("Descripcin:"));
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(u.getDescription() + ""));

            fila = hoja.createRow(7);
            celda = fila.createCell(3);
            celda.setCellValue(new HSSFRichTextString("Tweets!!!"));

            fila = hoja.createRow(9);
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString("IdTweet"));
            celda = fila.createCell(2);
            celda.setCellValue(new HSSFRichTextString("Cuenta"));
            celda = fila.createCell(3);
            celda.setCellValue(new HSSFRichTextString("Tweet"));
            celda = fila.createCell(4);
            celda.setCellValue(new HSSFRichTextString("Geolocation"));
            celda = fila.createCell(5);
            celda.setCellValue(new HSSFRichTextString("Place"));
            celda = fila.createCell(6);
            celda.setCellValue(new HSSFRichTextString("Retweets"));
            celda = fila.createCell(7);
            celda.setCellValue(new HSSFRichTextString("Favoritos"));

            Paging paging = new Paging(1, 1000);
            ResponseList<Status> s = twitter.getUserTimeline(u.getId(), paging);
            int i = 0;
            int filaNum = 10;
            for (Status st : s) {

                BasicDBObject obj = new BasicDBObject();

                obj.append("idTweet", s.get(i).getId() + "");
                obj.append("Cuenta", u.getName());
                obj.append("Tweet", s.get(i).getText());
                obj.append("Geolocation", s.get(i).getGeoLocation());
                obj.append("Place", s.get(i).getPlace());
                obj.append("Reteews", s.get(i).getRetweetCount());
                obj.append("Favoritos", s.get(i).getFavoriteCount());

                fila = hoja.createRow(filaNum);
                celda = fila.createCell(1);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getId() + ""));
                celda = fila.createCell(2);
                celda.setCellValue(new HSSFRichTextString(u.getName()));
                celda = fila.createCell(3);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getText()));
                celda = fila.createCell(4);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getGeoLocation() + ""));
                celda = fila.createCell(5);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getPlace() + ""));
                celda = fila.createCell(6);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getRetweetCount() + ""));
                celda = fila.createCell(7);
                celda.setCellValue(new HSSFRichTextString(s.get(i).getFavoriteCount() + ""));

                i++;
                filaNum++;

                datos.insert(obj);
            }

            FileOutputStream elFichero = new FileOutputStream("Metricas_Twitter.xls");
            libro.write(elFichero);
            elFichero.close();

            /********************************/

            System.out.println(i);

            //User usuario = twitter.showUser("@aaron21007");
            //    List<Status> statuses = twitter.getHomeTimeline();
            //    System.out.println("Showing home timeline.");
            //    for (Status status : statuses) {
            //        System.out.println(status.getUser().getName() + ":" +
            //                           status.getText());
            //    }

            //        Twitter unauthenticatedTwitter = new TwitterFactory(cb.build()).getInstance();
            //        
            //          List<String> listaTweets = new ArrayList();
            //          List<Long> ListaRettewts = new ArrayList();
            //          List<Integer> ListaFavoritos = new ArrayList();
            //          List<Integer> ListaMenciones = new ArrayList();
            //          List<Date> ListaFecha = new ArrayList();
            //          List<Long> ListaIds = new ArrayList();
            ////
            //          User usuario = unauthenticatedTwitter.showUser("@aaron21007");
            //          List<Status> ret = unauthenticatedTwitter.getRetweetsOfMe();
            //          List<Status> favoritos = unauthenticatedTwitter.getFavorites();
            //          Paging paging = new Paging(1, 1000);
            //          ResponseList<Status> statuses = unauthenticatedTwitter.getUserTimeline(usuario.getId(), paging);
            //          
            //          
            //          for (Status sta : statuses) {
            //              
            //            ListaIds.add(Long.valueOf(sta.getId()));
            //            listaTweets.add(sta.getText());
            //            ListaRettewts.add(Long.valueOf(Long.parseLong(sta.getRetweetCount() + "")));
            //            ListaMenciones.add(Integer.valueOf(sta.getUserMentionEntities().length));
            //            ListaFecha.add(sta.getCreatedAt());
            //            ListaFavoritos.add(Integer.valueOf(sta.getFavoriteCount()));
            //              System.out.println(sta.getText());
            //              
            //          }
            //          
            //          
            //        
            //       
            //        
        } catch (Exception e) {
            System.err.println("Fallo................." + e);
        }
    }

}