facebook.metrics.FacebookMetricsPage.java Source code

Java tutorial

Introduction

Here is the source code for facebook.metrics.FacebookMetricsPage.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 facebook.metrics;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookFactory;
import facebook4j.Like;
import facebook4j.Paging;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
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 FacebookMetricsPage {

    public static void main(String[] args) throws IOException {
        int varFeed = 0;
        int filasX = 0;
        HSSFRow fila = null;
        HSSFRow filam = null;
        HSSFCell celda = null;
        List<ResponseList<Post>> X = null;
        int j = 0;
        int i = 0;
        HSSFSheet hoja = null;
        Facebook facebook = null;
        HSSFWorkbook libro = null;
        try {
            // Se crea el libro
            libro = new HSSFWorkbook();

            hoja = libro.createSheet();

            fila = hoja.createRow(0);

            celda = fila.createCell(1);

            HSSFRichTextString texto = new HSSFRichTextString("Metricas de Infinitum");
            celda.setCellValue(texto);
            /************************************/
            /*Mongo DB Conection*/
            MongoClient mongoClient = new MongoClient("localhost", 27017);

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

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

            /********************/
            facebook = new FacebookFactory().getInstance();

            facebook.setOAuthAppId("603320016402931", "202694064e7a4e77f0c0042b1a16ebd4");

            System.out.println(facebook.getOAuthAppAccessToken().getToken());

            ResponseList<Post> feedX = facebook.getFeed("216786388331757", new Reading().limit(1000));

            X = new ArrayList<ResponseList<Post>>();
            int ui = 0;
            do {

                X.add(feedX);
                Paging<Post> pag1 = feedX.getPaging();
                feedX = facebook.fetchNext(pag1);
                System.out.println("" + ui);
                ui++;
            } while (feedX.getPaging() != null && ui < 25);

            fila = hoja.createRow(2);

            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString("Id Post:"));
            celda = fila.createCell(2);
            celda.setCellValue(new HSSFRichTextString("Fecha del Post:"));
            celda = fila.createCell(3);
            celda.setCellValue(new HSSFRichTextString("Usuario que posteo:"));
            celda = fila.createCell(4);
            celda.setCellValue(new HSSFRichTextString("Post:"));
            celda = fila.createCell(5);
            celda.setCellValue(new HSSFRichTextString("# de Likes del Post:"));
            celda = fila.createCell(6);
            celda.setCellValue(new HSSFRichTextString("# de Comentarios del Post:"));
            celda = fila.createCell(7);
            celda.setCellValue(new HSSFRichTextString("Comentarios del Post:"));
            celda = fila.createCell(8);
            celda.setCellValue(new HSSFRichTextString("Tipo de Post:"));

            filasX = 4;

            for (j = 0; j < X.size(); j++) {

                ResponseList<Post> feed = X.get(j);

                System.out.println(feed.size());

                for (i = 0; i < feed.size(); i++) {

                    System.out.println("Feed:  " + i);

                    BasicDBObject obj = new BasicDBObject();
                    ResponseList<Like> likes = facebook.getPostLikes(feed.get(i).getId(),
                            new Reading().limit(1135));
                    ResponseList<Comment> comments = facebook.getPostComments(feed.get(i).getId(),
                            new Reading().limit(1135));

                    obj.append("idPost", feed.get(i).getId() + "");

                    if (feed.get(i).getMessage() == null) {
                        obj.append("Post", "   ");
                        //System.out.println("Null");
                    } else {
                        obj.append("Post", feed.get(i).getMessage());
                    }

                    obj.append("Likes", likes.size() + "");
                    obj.append("Comments", comments.size() + "");

                    datos.insert(obj);

                    filam = hoja.createRow(filasX);
                    celda = filam.createCell(1);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getId() + ""));
                    celda = filam.createCell(2);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getCreatedTime() + ""));
                    celda = filam.createCell(3);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getFrom().getName()));
                    celda = filam.createCell(4);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getMessage()));
                    celda = filam.createCell(5);
                    celda.setCellValue(new HSSFRichTextString(likes.size() + ""));
                    celda = filam.createCell(6);
                    celda.setCellValue(new HSSFRichTextString(comments.size() + ""));
                    celda = filam.createCell(8);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getType() + ""));

                    filasX++;

                    for (int y = 0; y < comments.size(); y++) {
                        filam = hoja.createRow(filasX);
                        celda = filam.createCell(7);
                        celda.setCellValue(new HSSFRichTextString(comments.get(y).getMessage() + ""));
                        filasX++;
                    }

                }
            }

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

        } catch (Exception e) {

            System.err.println("Fatal Error:   " + e);
            FacebookMetricsPage s = new FacebookMetricsPage();

            s.porsi(i, j, filasX + 1, filam, celda, facebook, X, hoja, libro);

        }

    }

    public void porsi(int ii, int jj, int filasX, HSSFRow fila, HSSFCell celda, Facebook facebook,
            List<ResponseList<Post>> X, HSSFSheet hoja, HSSFWorkbook libro) throws IOException {

        try {

            for (int j = jj; j < X.size(); j++) {

                ResponseList<Post> feed = X.get(j);

                System.out.println(feed.size());

                for (int i = ii; i < feed.size(); i++) {

                    System.out.println("Feed:  " + i);

                    BasicDBObject obj = new BasicDBObject();
                    ResponseList<Like> likes = facebook.getPostLikes(feed.get(i).getId(),
                            new Reading().limit(1135));
                    ResponseList<Comment> comments = facebook.getPostComments(feed.get(i).getId(),
                            new Reading().limit(1135));

                    //                            obj.append("idPost",feed.get(i).getId()+"");
                    //
                    //                            if(feed.get(i).getMessage()==null){
                    //                                obj.append("Post","   ");
                    //                                //System.out.println("Null");
                    //                            }else{
                    //                                obj.append("Post",feed.get(i).getMessage());
                    //                            }
                    //
                    //                            obj.append("Likes",likes.size()+"");
                    //                            obj.append("Comments",comments.size()+"");
                    //
                    //                            datos.insert(obj);

                    fila = hoja.createRow(filasX);
                    celda = fila.createCell(1);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getId() + ""));
                    celda = fila.createCell(2);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getCreatedTime() + ""));
                    celda = fila.createCell(3);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getFrom().getName()));
                    celda = fila.createCell(4);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getMessage()));
                    celda = fila.createCell(5);
                    celda.setCellValue(new HSSFRichTextString(likes.size() + ""));
                    celda = fila.createCell(6);
                    celda.setCellValue(new HSSFRichTextString(comments.size() + ""));
                    celda = fila.createCell(8);
                    celda.setCellValue(new HSSFRichTextString(feed.get(i).getType() + ""));

                    filasX++;

                    for (int y = 0; y < comments.size(); y++) {
                        fila = hoja.createRow(filasX);
                        celda = fila.createCell(7);
                        celda.setCellValue(new HSSFRichTextString(comments.get(y).getMessage() + ""));
                        filasX++;
                    }

                }
            }

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

        } catch (Exception e) {
            System.err.println("Fatal Error 2:   " + e);
            FileOutputStream elFichero;
            try {
                elFichero = new FileOutputStream("Metricas_Infinitum.xls");
                libro.write(elFichero);
                elFichero.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FacebookMetricsPage.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

}