001/** 002 * Copyright (c) 2012, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.tools.reddit; 031 032import java.util.List; 033import org.joda.time.DateTime; 034import org.joda.time.DateTimeConstants; 035import org.joda.time.Period; 036import org.kohsuke.args4j.Option; 037import org.openimaj.twitter.collection.StreamJSONStatusList.ReadableWritableJSON; 038import org.openimaj.util.pair.LongLongPair; 039 040/** 041 * The time split attempts to read times from the json items provided and splits 042 * by WEEK,DAY,HOUR or MINUTE 043 * @author Jon Hare (jsh2@ecs.soton.ac.uk), Sina Samangooei (ss@ecs.soton.ac.uk) 044 * 045 */ 046public class TimeSplitMode extends SplitMode { 047 private static final String outputFormat = "%s.%s.s%s.e%s.json"; 048 049 enum TimeSplitPeriod{ 050 WEEK { 051 @Override 052 public LongLongPair startEnd(long time) { 053 DateTime dt = new DateTime(time); 054 DateTime start = dt.withDayOfWeek(DateTimeConstants.MONDAY); 055 DateTime end = dt.plus(weekPeriod).withDayOfWeek(DateTimeConstants.MONDAY); 056 057 return LongLongPair.pair(start.getMillis(), end.getMillis()); 058 } 059 },DAY { 060 @Override 061 public LongLongPair startEnd(long time) { 062 DateTime dt = new DateTime(time); 063 DateTime start = dt.withHourOfDay(0); 064 DateTime end = dt.plus(dayPeriod).withHourOfDay(0); 065 066 return LongLongPair.pair(start.getMillis(), end.getMillis()); 067 } 068 },HOUR { 069 @Override 070 public LongLongPair startEnd(long time) { 071 DateTime dt = new DateTime(time); 072 DateTime start = dt.withMinuteOfHour(0); 073 DateTime end = dt.plus(hourPeriod).withMinuteOfHour(0); 074 075 return LongLongPair.pair(start.getMillis(), end.getMillis()); 076 } 077 },MINUTE { 078 @Override 079 public LongLongPair startEnd(long time) { 080 DateTime dt = new DateTime(time); 081 DateTime start = dt.withSecondOfMinute(0); 082 DateTime end = dt.plus(minutePeriod).withSecondOfMinute(0); 083 084 return LongLongPair.pair(start.getMillis(), end.getMillis()); 085 } 086 }; 087 Period weekPeriod = new Period(0, 0, 1, 0, 0, 0, 0, 0); 088 Period dayPeriod = new Period(0, 0, 0, 1, 0, 0, 0, 0); 089 Period hourPeriod = new Period(0, 0, 0, 0, 1, 0, 0, 0); 090 Period minutePeriod = new Period(0, 0, 0, 0, 0, 1, 0, 0); 091 public abstract LongLongPair startEnd(long time); 092 } 093 094 @Option(name="--split-period", aliases="-sp", required=false, usage="the file split period.") 095 protected TimeSplitPeriod period = TimeSplitPeriod.MINUTE; 096 097 @Override 098 public void output(List<ReadableWritableJSON> read) { 099// Map<String,List<ReadableWritableJSON>> fileOutputs = new HashMap<String, List<ReadableWritableJSON>>(); 100// for (ReadableWritableJSON readableWritableJSON : read) { 101// @SuppressWarnings("unchecked") 102// List<Map<String,Object>> items = (List<Map<String, Object>>) ((Map<String,Object>)readableWritableJSON.get("data")).get("children"); 103// 104// } 105// return null; 106 } 107 108}