001/**
002 * Copyright (c) 2011, 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.demos.sandbox.ml.linear.learner.stream.twitter;
031
032import java.net.UnknownHostException;
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Map;
036
037import org.openimaj.demos.sandbox.ml.linear.learner.stream.MongoDBQueryStream;
038import org.openimaj.twitter.USMFStatus;
039import org.openimaj.util.data.Context;
040
041import com.mongodb.BasicDBObject;
042import com.mongodb.DBObject;
043import com.mongodb.ServerAddress;
044import com.mongodb.util.JSON;
045
046/**
047 * Get USMF statuses and financial ticks from a mongodb
048 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
049 *
050 */
051public final class USMFTickMongoDBQueryStream extends MongoDBQueryStream<Context> {
052        private String collectionName;
053        private String dbName;
054
055        /**
056         * The query stream from the seeds (default collection and db)
057         * @param seeds
058         * @throws UnknownHostException
059         */
060        public USMFTickMongoDBQueryStream(List<ServerAddress> seeds) throws UnknownHostException {
061                super(seeds);
062                this.collectionName = "streamapi_yahoo";
063                this.dbName = "twitterticker";
064        }
065
066        /**
067         * The query stream from the seeds and a specific collection
068         * @param seeds
069         * @param collectionName
070         * @throws UnknownHostException
071         */
072        public USMFTickMongoDBQueryStream(List<ServerAddress> seeds,String collectionName) throws UnknownHostException {
073                super(seeds);
074                this.collectionName = collectionName;
075                this.dbName = "twitterticker";
076        }
077
078        @Override
079        public String getCollectionName() {
080                return this.collectionName;
081        }
082
083        @Override
084        public DBObject getSort() {
085                BasicDBObject sort = new BasicDBObject();
086                sort.put("timestamp", 1);
087                return sort;
088        }
089
090        @Override
091        public String getDBName() {
092                return this.dbName;
093        }
094
095        @Override
096        @SuppressWarnings("unchecked")
097        public Context constructObjects(DBObject next) {
098                Context context = new Context();
099                List<Map<String, Double>> ticks = (List<Map<String, Double>>) next.get("tickers");
100                List<USMFStatus> tweets = new ArrayList<USMFStatus>();
101                List<Object> objl = (List<Object>) next.get("tweets");
102                for (Object object : objl) {
103                        USMFStatus status = new USMFStatus();
104                        status.fillFromString(JSON.serialize(object));
105                        tweets.add(status);
106                }
107                Long timestamp = (Long) next.get("timestamp");
108                context.put("timestamp", timestamp);
109                context.put("usmfstatuses",tweets);
110                context.put("ticks",ticks);
111                return context;
112        }
113}