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 * 020 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 021 * 022 */ 023public class PicSlurperUtils { 024 private static String TWEET_FILE_NAME = "tweets.json"; 025 026 /** 027 * Update a specific file with statistics of URLs being consumed 028 * 029 * @param statsFile 030 * @param statusConsumption 031 * @throws IOException 032 */ 033 public static synchronized void updateStats(File statsFile, StatusConsumption statusConsumption) throws IOException { 034 updateStats(statsFile, statusConsumption, false); 035 } 036 037 /** 038 * Update a specific file with statistics of URLs being consumed 039 * 040 * @param statsFile 041 * @param statusConsumption 042 * @param forgetImageURLs 043 * whether the actual image urls collected should be saved. On 044 * the global stats file this should be true 045 * @throws IOException 046 */ 047 public static synchronized void updateStats(File statsFile, StatusConsumption statusConsumption, 048 boolean forgetImageURLs) throws IOException 049 { 050 StatusConsumption current = new StatusConsumption(); 051 if (statsFile.exists()) 052 current = IOUtils.read(statsFile, current); 053 current.incr(statusConsumption); 054 if (forgetImageURLs) { 055 // emtpy the imageURLs before you save 056 current.imageURLs.clear(); 057 } 058 IOUtils.writeASCII(statsFile, current); // initialise the output file 059 } 060 061 private static transient Gson gson = new Gson(); 062 063 /** 064 * Updated a tweets.json file in the specified location with the given 065 * {@link ReadableWritableJSON} instance 066 * 067 * @param outRoot 068 * @param status 069 * @throws IOException 070 */ 071 public static synchronized void updateTweets(File outRoot, Status status) throws IOException { 072 if (status == null) 073 return; 074 final File outFile = new File(outRoot, TWEET_FILE_NAME); 075 final FileOutputStream fstream = new FileOutputStream(outFile, true); 076 final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fstream, "UTF-8"))); 077 pwriter.println(gson.toJson(status)); 078 pwriter.println(); 079 pwriter.flush(); 080 pwriter.close(); 081 } 082}