Permalink
Cannot retrieve contributors at this time
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?
mHealth-Facebook-Group-Scraper/MHealth_GroupFBScraper/src/Main.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (79 sloc)
3.74 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
import com.opencsv.CSVWriter; | |
import com.restfb.*; | |
import com.restfb.types.Group; | |
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 = "EAALA0FZC8s14BAHoSr3OBZBh5OZBmtx5AZCTWyeLP8kKR3gNsaK6YWS1gWCcaKZAQNU7GWh3b8iQIEwcld0nQFIF7azfEw0ZChMxP05idWBhd2BQNZAuY9eLKxDuhjmEboTv4ZBz7hwQDe13CeXajnNOMU4A8zNZB5niZBiDHtMuapwdRXYAvwa9mLcM5KAXWYIG8Oo4QWMD6hogZDZD"; | |
// 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) { | |
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")); | |
// 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 | |
* | |
*/ | |
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<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) { | |
//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()}); | |
} | |
} | |
writer.writeAll(chat_data); | |
writer.close(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |