Skip to content
Snippets Groups Projects
Commit 31fdd0d7 authored by Danny Allen's avatar Danny Allen
Browse files

Update ParseFBMessages

parent 35c35dd2
No related branches found
No related tags found
No related merge requests found
package p2.wordsuggestor;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Stack;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import cse332.interfaces.worklists.LIFOWorkList;
import datastructures.worklists.ArrayStack;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.json.simple.JSONArray;
public final class ParseFBMessages {
private ParseFBMessages() {
......@@ -30,37 +29,35 @@ public final class ParseFBMessages {
// You may be able to use a relative path like "./MyArchiveName", but results can
// vary from machine to machine.
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("USAGE: ParseFBMessages <Your FB Name> <Your FB Archive>");
System.exit(1);
}
// Note: you can replace these with your FB Name and Archive instead of
// using the command line if you'd like.
String name = args[0];
String archive = args[1];
String name = "<Your FB Name>"; // e.g. "Ruth Anderson"
String archive = "<Your FB Archive>"; // e.g. "/Users/rea/workspace/332/messages"
LIFOWorkList<String> messages = new ArrayStack<String>();
File[] listOfFiles = (new File(archive + File.separator + "messages")).listFiles();
Stack<String> corpus = new Stack<>();
File[] listOfFiles = (new File(archive + File.separator + "inbox")).listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
Document doc = Jsoup
.parse(listOfFiles[i], "UTF-8");
Elements messagesElements = doc.getElementsByTag("p");
for (Element content : messagesElements) {
if (content.previousElementSibling().getElementsByClass("user").text()
.equals(name)) {
messages.add(content.text());
}
}
}
File conversation = new File(listOfFiles[i], "message.json");
if (conversation.isFile()) {
try {
JSONObject obj = (JSONObject) new JSONParser().parse(new FileReader(conversation));
JSONArray messages = (JSONArray) obj.get("messages");
for (Object m: messages) {
JSONObject msg = (JSONObject) m;
String sender = (String) msg.get("sender_name");
if(sender != null && sender.equals(name)) {
corpus.push((String) msg.get("content"));
}
}
} catch (ParseException e) {
System.err.println("Could not parse: " + conversation.toString());
}
}
}
PrintWriter out = new PrintWriter("me.txt", "UTF-8");
while (messages.hasWork()) {
out.println(messages.next());
while (!corpus.isEmpty()) {
out.println(corpus.pop());
}
out.close();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment