socialImport.twitter.TwitterImport.java Source code

Java tutorial

Introduction

Here is the source code for socialImport.twitter.TwitterImport.java

Source

/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 *  Copyright 2013
 * Jana Asmussen, Julian Bau, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package socialImport.twitter;

import java.util.HashMap;
import java.util.Map;

import data.IData;

import twitter4j.FilterQuery;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import types.StreamDescriptor;

class TwitterImport implements ITwitterImport {

    private final String DUPLICATE_ID_ERROR_MESSAGE = "Duplicate StreamDescriptor. Please choose another one.";

    private TwitterStreamFactory twitterStreamFactory;
    private Map<StreamDescriptor, TwitterStream> streams;
    private IData dataModule;

    TwitterImport(TwitterStreamFactory twitterStreamFactory, IData dataModule) {
        this.twitterStreamFactory = twitterStreamFactory;
        this.streams = new HashMap<StreamDescriptor, TwitterStream>();
        this.dataModule = dataModule;
    }

    @Override
    public void openSampleStream(StreamDescriptor streamDescriptor) {
        //If the stream ID is not in use yet
        if (!streams.keySet().contains(streamDescriptor)) {
            TwitterStream twitterStream = twitterStreamFactory.getInstance();
            twitterStream.addListener(new TweetToDatabaseImportListener(streamDescriptor, dataModule));
            streams.put(streamDescriptor, twitterStream);
            twitterStream.sample();
        } else
            throw new IllegalArgumentException(DUPLICATE_ID_ERROR_MESSAGE);
    }

    @Override
    public void openFilterStream(StreamDescriptor streamDescriptor, long[] follow) {
        openFilterStream(streamDescriptor, 0, follow, null);
    }

    @Override
    public void openFilterStream(StreamDescriptor streamDescriptor) {
        openFilterStream(streamDescriptor, 0, null, null);
    }

    @Override
    public void openFilterStream(StreamDescriptor streamDescriptor, int count, long[] follow) {
        openFilterStream(streamDescriptor, count, follow, null);
    }

    @Override
    public void openFilterStream(StreamDescriptor streamDescriptor, int count, long[] follow,
            double[][] locations) {
        //If the stream ID is not in use yet
        if (!streams.keySet().contains(streamDescriptor)) {
            TwitterStream twitterStream = twitterStreamFactory.getInstance();

            twitterStream.addListener(new TweetToDatabaseImportListener(streamDescriptor, dataModule));
            FilterQuery filterQuery = new FilterQuery(count, follow, streamDescriptor.getTrackedTags(), locations);
            streams.put(streamDescriptor, twitterStream);
            twitterStream.filter(filterQuery);
        } else
            throw new IllegalArgumentException(DUPLICATE_ID_ERROR_MESSAGE);
    }

    @Override
    public void closeStream(StreamDescriptor streamDescriptor) {
        TwitterStream twitterStream = streams.remove(streamDescriptor);

        if (twitterStream != null) {
            twitterStream.cleanUp();
        }
    }

}