From 07de8bbcdb67421f6637ab71b1005ad06d7c3d09 Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 17 Jan 2019 22:37:17 -0500 Subject: [PATCH] Fixed Null Date error --- MHealth_GroupFBScraper/src/Main.java | 55 ++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/MHealth_GroupFBScraper/src/Main.java b/MHealth_GroupFBScraper/src/Main.java index c2df402..2a84a62 100644 --- a/MHealth_GroupFBScraper/src/Main.java +++ b/MHealth_GroupFBScraper/src/Main.java @@ -1,15 +1,19 @@ 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; /** @@ -25,23 +29,25 @@ public class Main { */ public static void main(String[] args) { - String accessToken = "EAALA0FZC8s14BAHoSr3OBZBh5OZBmtx5AZCTWyeLP8kKR3gNsaK6YWS1gWCcaKZAQNU7GWh3b8iQIEwcld0nQFIF7azfEw0ZChMxP05idWBhd2BQNZAuY9eLKxDuhjmEboTv4ZBz7hwQDe13CeXajnNOMU4A8zNZB5niZBiDHtMuapwdRXYAvwa9mLcM5KAXWYIG8Oo4QWMD6hogZDZD"; - + 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 userGroups = fbClient.fetchConnection("me/groups", Group.class); - for(List page : userGroups) { + //get the iterator + Iterator> groupIt = userGroups.iterator(); + while(groupIt.hasNext()) { + List page = groupIt.next(); 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, story")); + Connection 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); } @@ -63,24 +69,49 @@ public class Main { * shares - headers: Id, UserId, UserName, CreatedTime, StatusType, Message, Story, Link, Picture * */ - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + Iterator> 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 writer = new CSVWriter(outputfile); + CSVWriter chatWriter = 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) { + while(it.hasNext()) { + List feedPage = it.next(); //Create a list of posts from the post feed Y - for(Post currPost : getPosts) { - chat_data.add(new String[] {currPost.getId(), currPost.getFrom().getId(), currPost.getFrom().getName(), "Date", currPost.getType(), currPost.getMessage(), currPost.getStory(), currPost.getLink(), currPost.getPicture()}); + 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 + } } } - writer.writeAll(chat_data); - writer.close(); + chatWriter.writeAll(chat_data); + chatWriter.close(); } catch (IOException e) { // TODO Auto-generated catch block