Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed Null Date error
  • Loading branch information
Jackie committed Jan 18, 2019
1 parent 5b9255c commit 07de8bb
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions 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;

/**
Expand All @@ -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<Group> userGroups = fbClient.fetchConnection("me/groups", Group.class);
for(List<Group> page : userGroups) {
//get the iterator
Iterator<List<Group>> groupIt = userGroups.iterator();
while(groupIt.hasNext()) {
List<Group> 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<Post> postFeed = fbClient.fetchConnection(currGroup.getId()+"/feed", Post.class, Parameter.with("fields","from,actions,message,likes,reactions, story"));
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);
}
Expand All @@ -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<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 writer = new CSVWriter(outputfile);
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"});

for(List<Post> getPosts : postFeed) {
while(it.hasNext()) {
List<Post> 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
Expand Down

0 comments on commit 07de8bb

Please sign in to comment.