001package org.openimaj.picslurper;
002
003import java.io.BufferedWriter;
004import java.io.File;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.io.OutputStreamWriter;
008import java.io.PrintWriter;
009
010import org.openimaj.io.IOUtils;
011import org.openimaj.twitter.collection.StreamJSONStatusList.ReadableWritableJSON;
012
013import twitter4j.Status;
014
015import com.google.gson.Gson;
016
017/**
018 * Functions for writing various parts of PicSlurper
019 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
020 *
021 */
022public class PicSlurperUtils {
023        private static String TWEET_FILE_NAME = "tweets.json";
024        /**
025         * Update a specific file with statistics of URLs being consumed
026         *
027         * @param statsFile
028         * @param statusConsumption
029         * @throws IOException
030         */
031        public static synchronized void updateStats(File statsFile, StatusConsumption statusConsumption) throws IOException {
032                updateStats(statsFile, statusConsumption, false);
033        }
034
035        /**
036         * Update a specific file with statistics of URLs being consumed
037         *
038         * @param statsFile
039         * @param statusConsumption
040         * @param forgetImageURLs whether the actual image urls collected should be saved. On the global stats file this should be true
041         * @throws IOException
042         */
043        public static synchronized void updateStats(File statsFile, StatusConsumption statusConsumption, boolean forgetImageURLs) throws IOException {
044                StatusConsumption current = new StatusConsumption();
045                if (statsFile.exists())
046                        current = IOUtils.read(statsFile, current);
047                current.incr(statusConsumption);
048                if(forgetImageURLs){
049                        // emtpy the imageURLs before you save
050                        current.imageURLs.clear();
051                }
052                IOUtils.writeASCII(statsFile, current); // initialise the output file
053        }
054
055        private static transient Gson gson = new Gson();
056
057        /**
058         * Updated a tweets.json file in the specified location with the given
059         * {@link ReadableWritableJSON} instance
060         *
061         * @param outRoot
062         * @param status
063         * @throws IOException
064         */
065        public static synchronized void updateTweets(File outRoot, Status status) throws IOException {
066                if(status==null)return;
067                File outFile = new File(outRoot, TWEET_FILE_NAME);
068                FileOutputStream fstream = new FileOutputStream(outFile, true);
069                PrintWriter pwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fstream, "UTF-8")));
070                pwriter.println(gson.toJson(status));
071                pwriter.println();
072                pwriter.flush();
073                pwriter.close();
074        }
075}