Skip to content
Permalink
07de8bbcdb
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
122 lines (109 sloc) 4.83 KB
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import com.opencsv.CSVWriter;
import com.restfb.*;
import com.restfb.types.Group;
import com.restfb.types.Likes;
import com.restfb.types.Post;
/**
* TODO:
* Facebook authentication (no more temporary access codes)
* GUI
*
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String accessToken = "EAALA0FZC8s14BACP7C9wRohgnBbgGbMmmCnZBjPDcQbGf6ZA1oro7vyZB9ABBYyjKEABrOZA8UNT7rhTW9CDzEZANKzxPdD3JKkbCcL6KpiUmke0gXLIxbE23ZCn91hECSF8VH0oWDPtGeA2Hxmcp6erocnSiQMCFYiY5LHPvJQefNPci5m13QHKE00z2GybB4cIu4mnpZBOJwZDZD";
// Token expires daily - facebook auth needed for permanent one or app needs to login with facebook
Scanner input = new Scanner(System.in);
FacebookClient fbClient = new DefaultFacebookClient(accessToken, Version.VERSION_3_1);
Connection<Group> userGroups = fbClient.fetchConnection("me/groups", Group.class);
//get the iterator
Iterator<List<Group>> groupIt = userGroups.iterator();
while(groupIt.hasNext()) {
List<Group> page = groupIt.next();
for(Group currGroup : page) {
//System.out.println("Do you want to get posts from "+currGroup.getName()+" ? Yes/NO"); // Eventually switch to GUI
//String response = input.nextLine();
//if(response.equals("Yes")) {
// Removed above lines for testing (will need them in the future but not now since I'm only testing on one group)
if(currGroup.getId().equals("250530099176185")){
Connection<Post> postFeed = fbClient.fetchConnection(currGroup.getId()+"/feed", Post.class, Parameter.with("fields","from,actions,message,likes,reactions,story,type,link,picture,created_time"));
// Note - ask Jess if they want to be able to specify a start date / end date when pulling data
writeCSVData(currGroup, postFeed);
}
}
}
input.close();
}
public static void writeCSVData(Group currGroup, Connection<Post> postFeed)
{
// TODO - Finish
// File Name should be of format: Health Chat_Data Type_Group Name_Date Pulled.csv
/* Files needed:
* comments - headers: PostId, Id, UserId, UserName, CreatedTime, Message
* chat_feed - headers: Id, UserId, UserName, CreatedTime, Status Type, Message, Story, Link, Picture
* likes - headers: ObjectId, UserId, UserName
* reactions - headers: ObjectId, UserId, UserName, Type
* shares - headers: Id, UserId, UserName, CreatedTime, StatusType, Message, Story, Link, Picture
*
*/
Iterator<List<Post>> it = postFeed.iterator();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy hh.mm.ss");
LocalDateTime now = LocalDateTime.now();
DateFormat csvDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
try {
// Should first create a folder then put all of these files in that folder with the date
File file = new File("Health Chat_chat_feed_"+currGroup.getName()+"_"+dtf.format(now)+".csv"); // Filepath needs to be more specific in final
FileWriter outputfile = new FileWriter(file);
// create CSVWriter object filewriter object as parameter
CSVWriter chatWriter = new CSVWriter(outputfile);
List<String[]> chat_data = new ArrayList<String[]>();
chat_data.add(new String[] {"Id", "UserId", "UserName", "CreatedTime", "StatusType", "Message", "Story", "Link", "Picture"});
while(it.hasNext()) {
List<Post> feedPage = it.next();
//Create a list of posts from the post feed Y
for(Post currPost : feedPage) {
String id = currPost.getId();
String UserId = currPost.getFrom().getId();
String UserName = currPost.getFrom().getName();
String CreatedTime = csvDateFormat.format(currPost.getCreatedTime());
String type = currPost.getType();
String message = currPost.getMessage();
String story = currPost.getStory();
String link = "fb.com/"+id;
String pic = currPost.getPicture();
//IF post isnt shared, add to chat_data file
chat_data.add(new String[] {id,UserId,UserName,CreatedTime,type,message,story,link,pic});
//else add to shares file
if(currPost.getLikesCount() > 0) {
//add to the likes.csv file
}
if (currPost.getReactionsCount() > 0) {
//add to the reactions.csv file
}
if (currPost.getCommentsCount() > 0) {
//add to the comments.csv file
}
}
}
chatWriter.writeAll(chat_data);
chatWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}