diff --git a/MHealth_GroupFBScraper/src/Main.java b/MHealth_GroupFBScraper/src/Main.java index 9e63a0c..c2df402 100644 --- a/MHealth_GroupFBScraper/src/Main.java +++ b/MHealth_GroupFBScraper/src/Main.java @@ -1,6 +1,8 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -33,14 +35,15 @@ public class Main { Connection userGroups = fbClient.fetchConnection("me/groups", Group.class); for(List page : userGroups) { for(Group currGroup : page) { + // This loop doesn't make any sense I should replace it //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 postFeed = fbClient.fetchConnection(currGroup.getId()+"/feed", Post.class, Parameter.with("fields","from,actions,message,likes,reactions")); - - writeCSVData(postFeed); + Connection postFeed = fbClient.fetchConnection(currGroup.getId()+"/feed", Post.class, Parameter.with("fields","from,actions,message,likes,reactions, story")); + // Note - ask Jess if they want to be able to specify a start date / end date when pulling data + writeCSVData(currGroup, postFeed); } } } @@ -48,7 +51,7 @@ public class Main { } - public static void writeCSVData(Connection postFeed) + public static void writeCSVData(Group currGroup, Connection postFeed) { // TODO - Finish // File Name should be of format: Health Chat_Data Type_Group Name_Date Pulled.csv @@ -60,34 +63,25 @@ public class Main { * shares - headers: Id, UserId, UserName, CreatedTime, StatusType, Message, Story, Link, Picture * */ + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + LocalDateTime now = LocalDateTime.now(); try { + 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 writer = new CSVWriter(outputfile); + List chat_data = new ArrayList(); + chat_data.add(new String[] {"Id", "UserId", "UserName", "CreatedTime", "StatusType", "Message", "Story", "Link", "Picture"}); + for(List getPosts : postFeed) { //Create a list of posts from the post feed Y for(Post currPost : getPosts) { - // Individual posts - // System.out.println(currPost.getFrom().getName()); - // System.out.println(currPost.getReactions().getData()); - // System.out.println(currPost.getLikes()); - // System.out.println("--> "+currPost.getMessage()); - // System.out.println("fb.com/"+currPost.getId()); + chat_data.add(new String[] {currPost.getId(), currPost.getFrom().getId(), currPost.getFrom().getName(), "Date", currPost.getType(), currPost.getMessage(), currPost.getStory(), currPost.getLink(), currPost.getPicture()}); } } - File file = new File(); - FileWriter outputfile = new FileWriter(file); + writer.writeAll(chat_data); + writer.close(); - // create CSVWriter object filewriter object as parameter - CSVWriter writer = new CSVWriter(outputfile); - - // create a List which contains String array - List data = new ArrayList(); - data.add(new String[] { "Name", "Class", "Marks" }); - data.add(new String[] { "Aman", "10", "620" }); - data.add(new String[] { "Suraj", "10", "630" }); - writer.writeAll(data); - - // closing writer connection - - writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();