FBMsgExtractor.MessageFormatting.java Source code

Java tutorial

Introduction

Here is the source code for FBMsgExtractor.MessageFormatting.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor. 5
 */

package FBMsgExtractor;

import java.io.*;
import java.util.HashMap;
import java.util.Date;
import org.apache.commons.lang3.StringEscapeUtils;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

public class MessageFormatting {

    BufferedReader br;
    PrintWriter outPut;
    HashMap<String, String> leagueIDs;
    HashMap<String, String> messageDetails;

    public static void main(String[] args) {
        new MessageFormatting();
    }

    public MessageFormatting() {
        File inFile = new File(
                "J:\\Uni Work Backup\\Dropbox\\Alex Facebook Thread\\After Parsing\\TrimmedMessages.txt");
        try {
            leagueIDs = new HashMap<>();

            leagueIDs.put("36819082", "Ben Beowulf Reid");
            leagueIDs.put("514098273", "Reginald Amukoshi Emvula");
            leagueIDs.put("36818470", "Alex Holehouse");
            leagueIDs.put("61416976", "Gregory van der Donk");
            leagueIDs.put("503074462", "James Rees");
            leagueIDs.put("505420379", "Jonathan Cain");
            leagueIDs.put("286302858", "Nick Cool Swallow");
            leagueIDs.put("502555542", "Ben Morgan");
            leagueIDs.put("500535605", "Vernon Silson");

            Date date = new Date();

            messageDetails = new HashMap<>();

            int counter = 0;

            outPut = new PrintWriter(
                    "J:\\Uni Work Backup\\Dropbox\\Alex Facebook Thread\\After Parsing\\CleanedMessages.txt");

            br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
            String line;

            while (((line = br.readLine()) != null)) //&& (count < 100))
            {

                if (line.trim().equals(""))
                    continue;

                if (line.startsWith("***LEAGUETHREAD***")) {
                    messageDetails.put("*ThreadNumber", line.replace("***LEAGUETHREAD***", "").trim());
                    continue;
                }

                if (line.startsWith("***TITLE***")) {
                    messageDetails.put("*Title", line.replace("***TITLE***", "").trim());
                    continue;
                }

                if (line.startsWith("***MARKER***")) {
                    messageDetails.put("*Marker", line.replace("***MARKER***", "").trim());
                    continue;
                }

                if (line.startsWith("MessageID:")) {
                    messageDetails.put("*MessageID", line.replace("MessageID:", "").trim());
                    continue;
                }

                if (line.startsWith("Time:")) {
                    line = line.replace("Time: ", "");
                    long tempTime = Long.parseLong(line.trim());
                    date.setTime(tempTime);
                    messageDetails.put("*Time", date.toString());
                    continue;
                }

                if (line.startsWith("Author:")) {
                    line = line.replace("Author: ", "").trim();
                    messageDetails.put("*Author", leagueIDs.get(line));
                    continue;
                }

                if (line.startsWith("Message:")) {
                    line = line.replace("Message:", "");
                    String tempMessage = new String(StringEscapeUtils.unescapeHtml4(line).getBytes("ISO-8859-1"))
                            .trim();

                    messageDetails.put("*Message", tempMessage);
                }

                if (line.startsWith("Link:")) {
                    line = line.replace("Link: ", "");
                    line = line.replace("***LINK: \"", "***LINK: ");
                    line = line.replace(" ImageShack</span>", "");
                    line = line.replace("</span>", "").trim();

                    line = StringEscapeUtils.unescapeHtml4(line);

                    line = line.replace("http://lm.facebook.com/l.php?u=", "");
                    line = line.replace("https://m.facebook.com/l.php?u=", "");

                    String tempLink = "";
                    char[] linkArray = line.toCharArray();

                    for (int i = (line.indexOf("***LINK:") + 8); i < line.indexOf("||"); i++) {
                        tempLink += linkArray[i];

                    }
                    tempLink = tempLink.trim();

                    line = line.replace(tempLink, "***URL***");

                    tempLink = URLDecoder.decode(tempLink, "ISO-8859-1");

                    line = line.replace("***URL***", tempLink);

                    messageDetails.put("*Link", line);
                    messageDetails.put("*LinkURL", tempLink);
                    continue;
                }

                if (line.startsWith("Image:")) {
                    line = line.replace("Image:", "");
                    line = line.replace("***IMAGE: \"", "***IMAGE: ").trim();

                    line = StringEscapeUtils.unescapeHtml4(line);

                    String tempLink = "";
                    char[] linkArray = line.toCharArray();

                    for (int i = (line.indexOf("***IMAGE:") + 9); i < line.indexOf("\""); i++) {
                        tempLink += linkArray[i];

                    }
                    tempLink = tempLink.trim();

                    char[] cutLine = line.toCharArray();
                    String tempLine = "";

                    for (int i = 0; i < line.indexOf(tempLink); i++) {
                        tempLine += cutLine[i];
                    }

                    tempLink = URLDecoder.decode(tempLink, "ISO-8859-1");
                    tempLink = tempLink.replaceAll("s100x100/", "");
                    tempLink = tempLink.replaceAll("s75x225/", "");
                    tempLink = tempLink.replaceAll("p50x50/", "");
                    tempLink = tempLink.replaceAll("&preview=1&width=194&height=194", "");

                    tempLine += tempLink;
                    tempLine = tempLine.trim();

                    messageDetails.put("*Image", tempLine);
                    messageDetails.put("*ImageURL", tempLink);
                    continue;
                }

                if (line.startsWith("***END***")) {
                    if (line.contains(""))
                        System.out.println(messageDetails.get("*Marker"));

                    if (messageDetails.containsKey("*ThreadNumber")) {
                        outPut.println("League Thread " + messageDetails.get("*ThreadNumber"));
                        System.out.println(messageDetails.get("*ThreadNumber") + "-" + messageDetails.get("*Time")
                                + "-" + messageDetails.get("*Title"));
                        outPut.println();
                    }

                    if (messageDetails.containsKey("*Title")) {
                        outPut.println("Title: " + messageDetails.get("*Title"));
                        outPut.println();
                    }

                    if (messageDetails.containsKey("*Marker")) {
                        outPut.println("*************");
                        outPut.println("Marker: " + messageDetails.get("*Marker"));
                    }

                    if (messageDetails.containsKey("*MessageID")) {
                        outPut.println("MessageID: " + messageDetails.get("*MessageID"));
                    }

                    if (messageDetails.containsKey("*Author")) {
                        outPut.println("Author: " + messageDetails.get("*Author"));
                    }

                    if (messageDetails.containsKey("*Time")) {
                        outPut.println("Time: " + messageDetails.get("*Time"));
                        outPut.println();
                    }

                    //if(messageDetails.containsKey("*Image") && messageDetails.containsKey("*Link"))
                    //System.out.println(messageDetails.get("*Marker"));

                    if (messageDetails.containsKey("*Image")) {
                        String tempImage = messageDetails.get("*Image");

                        if (messageDetails.containsKey("*Message")) {
                            tempImage = tempImage.replace(messageDetails.get("*Message"), "").trim();
                        }

                        outPut.println("Image: " + tempImage);
                        outPut.println("ImageURL: " + messageDetails.get("*ImageURL"));
                        outPut.println();
                    }

                    if (messageDetails.containsKey("*Link")) {
                        String tempLink = messageDetails.get("*Link");

                        if (messageDetails.containsKey("*Message")) {
                            tempLink = tempLink.replace(messageDetails.get("*Message"), "").trim();
                        }

                        outPut.println("Link: " + tempLink);
                        outPut.println("LinkURL: " + messageDetails.get("*LinkURL"));
                        outPut.println();
                    }

                    if (messageDetails.containsKey("*Message")) {
                        outPut.println("Message: " + messageDetails.get("*Message"));
                        outPut.println();
                    }

                    messageDetails.clear();
                    outPut.println("***END***");
                    outPut.println();
                }

                outPut.flush();
            }
            outPut.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }

}