facebook.insights.MetricaX.java Source code

Java tutorial

Introduction

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

import com.mongodb.BasicDBObject;
import facebook.metrics.Metricas;
import facebook4j.Comment;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONArray;
import org.json.JSONObject;
import facebook4j.Facebook;
import facebook4j.FacebookFactory;
import facebook4j.Like;
import facebook4j.Paging;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;

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

    public static void main(String[] args) {

        MetricaX obj = new MetricaX();
        Facebook facebook = null;
        List<ResponseList<Post>> X = null;

        try {

            // Se crea el libro
            Workbook libro = new XSSFWorkbook();
            // Se crea una hoja dentro del libro
            Sheet hoja = libro.createSheet("Metricas 2014  by Flock");
            // Se crea una fila dentro de la hoja
            Row fila = hoja.createRow(0);
            // Se crea una celda dentro de la fila
            Cell celda = fila.createCell(1);
            // Se crea el contenido de la celda y se mete en ella.
            celda.setCellValue("Insights Infinitum por Post");

            List<Row> filas = new ArrayList<>();

            for (int e = 2; e < 1048576; e++) {
                Row filad = hoja.createRow(e);
                filas.add(filad);
            }

            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 < 23);

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

                int recorrete = 4;

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

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

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

                    System.out.println("Feed:  " + i);
                    String posttext = feed.get(i).getMessage() + "";
                    String fecha = feed.get(i).getCreatedTime() + "";
                    obj.bloque(obj, feed.get(i).getId() + "", recorrete, hoja, filas, fia, posttext, fecha);

                    fia += 6;

                }
            }

            FileOutputStream elFichero = new FileOutputStream("MetricasAaron2.xlsx");
            libro.write(elFichero);
            elFichero.close();

        } catch (Exception e) {
            System.err.println("En Main: " + e);
        }
    }

    private void recorre(int posicionColumna, String insight, Sheet hoja, JSONArray y, List<Row> fi, int fila,
            String texto, String id, String fecha) {

        int columna = posicionColumna;

        Row filaT = fi.get(fila);

        Cell celdaD = filaT.createCell(columna);
        celdaD.setCellValue(insight + "");

        Cell celdae = filaT.createCell(0);
        celdae.setCellValue(id + "");

        Cell celdaf = filaT.createCell(1);
        celdaf.setCellValue(texto + "");

        Cell celdag = filaT.createCell(2);
        celdag.setCellValue(fecha + "");

        for (int i = (fila + 1), h = 0; h < y.length(); h++) {

            Row filaF = fi.get(i);

            Cell celdaE = filaF.createCell(columna);
            celdaE.setCellValue("Post");

            JSONObject cd = new JSONObject();
            try {
                cd = (JSONObject) y.getJSONObject(h).get("value");

            } catch (Exception e) {
                System.err.println("No hay nada");
            }

            if (cd.length() > 1) {

                for (int si = 0; si < cd.length(); si++) {

                    filaF = fi.get(i + 1);

                    Cell celdaF = filaF.createCell(columna + 1);
                    celdaF.setCellValue("" + cd.names().getString(si));

                    celdaF = filaF.createCell(columna + 2);
                    celdaF.setCellValue("" + cd.get(cd.names().getString(si)));
                    i++;
                }

            } else {
                Cell celdaF = filaF.createCell(columna + 1);
                celdaF.setCellValue("" + y.getJSONObject(h).get("value"));
            }

            i++;

        }
    }

    private void bloque(MetricaX obj, String idPost, int recorrete, Sheet hoja, List<Row> filas, int fila,
            String textopost, String fecha) {

        Metricas s = new Metricas(
                "CAAImAGMzn20BAF8yhGFZBNXP8LEnZCElzzSoX0Ll6uY1APzZBnAT9LVZCQ0dP1AKPxL8C1qybyyz7xfGCOLo3ew3W3IpJkZC8XNKvEsywyCCZBOaicIA1tQZCFWknQ1MdREu2BZCAoK3M0Y6yvm0whZCKgWrNHF4AuA8eSm8saoxIiUtqNFf3cq4F");
        //                           Metrica                           //
        try {

            JSONObject ob = s.metricaX("post_story_adds_unique/lifetime", idPost);
            JSONArray x = ob.getJSONArray("data");
            JSONArray y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_story_adds_unique/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_story_adds/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_story_adds/lifetime", hoja, y, filas, fila, textopost, idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_story_adds_by_action_type_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_story_adds_by_action_type_unique/lifetime", hoja, y, filas, fila,
                    textopost, idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_story_adds_by_action_type/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_story_adds_by_action_type/lifetime", hoja, y, filas, fila, textopost,
                    idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_unique/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions/lifetime", hoja, y, filas, fila, textopost, idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_paid_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_paid_unique/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_paid/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_paid/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_organic_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_organic_unique/lifetime", hoja, y, filas, fila, textopost,
                    idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_organic/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_organic/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_viral_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_viral_unique/lifetime", hoja, y, filas, fila, textopost,
                    idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_viral/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_viral/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_impressions_by_paid_non_paid_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_impressions_by_paid_non_paid_unique/lifetime", hoja, y, filas, fila,
                    textopost, idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_consumptions_by_type_unique/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_consumptions_by_type_unique/lifetime", hoja, y, filas, fila, textopost,
                    idPost, fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_consumptions_by_type/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_consumptions_by_type/lifetime", hoja, y, filas, fila, textopost, idPost,
                    fecha);
            recorrete += 3;

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

            ob = s.metricaX("post_engaged_users/lifetime", idPost);
            x = ob.getJSONArray("data");
            y = (JSONArray) x.getJSONObject(0).get("values");

            obj.recorre(recorrete, "post_engaged_users/lifetime", hoja, y, filas, fila, textopost, idPost, fecha);
            recorrete += 3;

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

        } catch (Exception e) {
            System.err.println("En bloque:" + e);
        }

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

    }
}