Java tutorial
/* * 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.FileOutputStream; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; 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 FacebookMetricsGroup { public static void main(String[] args) { try { // 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 Grupo Cinepolitos"); celda.setCellValue(texto); /************************************/ /*Mongo DB Conection*/ MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("JavaMongoFacebookGroup"); DBCollection datos = db.getCollection("Datos"); /********************/ Facebook facebook = new FacebookFactory().getInstance(); facebook.setOAuthAppId("603320016402931", "202694064e7a4e77f0c0042b1a16ebd4"); System.out.println(facebook.getOAuthAppAccessToken().getToken()); ResponseList<Post> feedX = facebook.getGroupFeed("17761155026", new Reading().limit(1135)); List<ResponseList<Post>> X = new ArrayList<ResponseList<Post>>(); do { X.add(feedX); Paging<Post> pag1 = feedX.getPaging(); feedX = facebook.fetchNext(pag1); } while (feedX.getPaging() != null); 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:")); int filasX = 3; for (int j = 0; j < X.size(); j++) { ResponseList<Post> feed = X.get(j); System.out.println(feed.size()); for (int 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); 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() + "")); 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++; } } System.out.println(); } FileOutputStream elFichero = new FileOutputStream("Metricas_Cinepolitos.xls"); libro.write(elFichero); elFichero.close(); } catch (Exception e) { System.err.println("Fatal Error: " + e); } } }