socialImport.twitter.TwitterImportFactory.java Source code

Java tutorial

Introduction

Here is the source code for socialImport.twitter.TwitterImportFactory.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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import data.IData;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;
import types.StreamDescriptor;
import types.TweetPost;

public class TwitterImportFactory {

    /**
     * Initializes the TwitterImport-Module:
     *    - Reads the needed API-Keys from a config file
     *    - Starts the streams defined in a specified config file and all streams saved in the database
     * 
     * @param apiKeyConfigFilePath The path to the config file containing the Twitter-API-Keys (oAuthConsumerKey, oAuthConsumerSecret, oAuthAccessToken, oAuthAccessTokenSecret)
     * @param streamDescriptionConfigFilePath The path to the config file containing the StreamDescriptor information as described in the wiki
     * @param debugEnabled Whether the Twitter4J-Library should print debugMessages to the console or not
     * @throws IOException 
     * */
    public ITwitterImport initializeTwitterImport(String apiKeyConfigFilePath,
            String streamDescriptionConfigFilePath, boolean debugEnabled, IData dataModule) throws IOException {
        // Initialize a TwitterImport-Instance without any active Streams
        ITwitterImport twitterImport = getInstance(apiKeyConfigFilePath, debugEnabled, dataModule);

        List<StreamDescriptor> savedStreams = dataModule.getStreams();

        // Add the streams described in the specified configFile
        Map<String, String> streamConfigValues = new HashMap<String, String>();
        BufferedReader br = new BufferedReader(new FileReader(streamDescriptionConfigFilePath));
        String line = br.readLine();

        //TODO: Add the other possible parameters for the filterStream (geoCoordinates)
        while (line != null) {
            String[] configValues = line.split(";");

            for (int i = 0; i < configValues.length; i++) {
                String[] keyValuePair = configValues[i].split("=");
                streamConfigValues.put(keyValuePair[0], keyValuePair[1]);
            }

            String streamName = streamConfigValues.get("StreamName");
            String[] trackedTags = streamConfigValues.get("TrackedTags").split(",");
            Class<?> elementType = TweetPost.class;

            if (!containsStreamWithName(savedStreams, streamName)) {
                StreamDescriptor streamDescriptor = new StreamDescriptor(streamName, trackedTags, elementType);
                dataModule.addStream(streamDescriptor);
                twitterImport.openFilterStream(streamDescriptor);
            }

            streamConfigValues.clear();
            line = br.readLine();
        }

        br.close();

        // Start the streams which are saved in the database

        for (StreamDescriptor streamDescriptor : savedStreams) {
            if (streamDescriptor.getElementType().equals(TweetPost.class)) {
                twitterImport.openFilterStream(streamDescriptor);
            }
        }

        return twitterImport;
    }

    /**
     * Initializes the TwitterImport-Module:
     *    - Reads the needed API-Keys from a config file
     * 
     * @param apiKeyConfigFilePath The path to the config file containing the Twitter-API-Keys (oAuthConsumerKey, oAuthConsumerSecret, oAuthAccessToken, oAuthAccessTokenSecret).
     * @param debugEnabled Whether the Twitter4J-Library should print debugMessages to the console or not.
     * @throws IOException 
     * */
    public ITwitterImport getInstance(String apiKeyConfigFilePath, boolean debugEnabled, IData dataModule)
            throws IOException {

        // Reading the apiKeys from the specified configFile      
        Map<String, String> keyConfigValues = new HashMap<String, String>();
        BufferedReader br = new BufferedReader(new FileReader(apiKeyConfigFilePath));
        String line = br.readLine();

        while (line != null) {
            String[] configValue = line.split("=");
            keyConfigValues.put(configValue[0], configValue[1]);
            line = br.readLine();
        }

        String oAuthConsumerKey = keyConfigValues.get("oAuthConsumerKey");
        String oAuthConsumerSecret = keyConfigValues.get("oAuthConsumerSecret");
        String oAuthAccessToken = keyConfigValues.get("oAuthAccessToken");
        String oAuthAccessTokenSecret = keyConfigValues.get("oAuthAccessTokenSecret");
        br.close();

        return getInstance(oAuthConsumerKey, oAuthConsumerSecret, oAuthAccessToken, oAuthAccessTokenSecret,
                debugEnabled, dataModule);
    }

    /**
     * Initializes the TwitterImport-Module with the given parameters
     * 
     * @param oAuthConsumerKey The oAuthConsumerKey as generated by Twitter.
     * @param oAuthConsumerSecret The oAuthConsumerSecret as generated by Twitter.
     * @param oAuthAccessToken The oAuthAccessToken as generated by Twitter.
     * @param oAuthAccessTokenSecret The oAuthAccessTokenSecret as generated by Twitter.
     * @param debugEnabled Whether the Twitter4J-Library should print debugMessages to the console or not.
     * @param dataModule A reference to the systems Data-Module.
     * 
     * */
    public ITwitterImport getInstance(String oAuthConsumerKey, String oAuthConsumerSecret, String oAuthAccessToken,
            String oAuthAccessTokenSecret, boolean debugEnabled, IData dataModule) {

        ConfigurationBuilder configBuilder = new ConfigurationBuilder();
        configBuilder.setDebugEnabled(debugEnabled).setOAuthConsumerKey(oAuthConsumerKey)
                .setOAuthConsumerSecret(oAuthConsumerSecret).setOAuthAccessToken(oAuthAccessToken)
                .setOAuthAccessTokenSecret(oAuthAccessTokenSecret).setJSONStoreEnabled(true);

        if (debugEnabled) {
            System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.StdOutLoggerFactory");
        } else {
            System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.NullLoggerFactory");
        }

        TwitterStreamFactory twitterStreamFactory = new TwitterStreamFactory(configBuilder.build());

        return new TwitterImport(twitterStreamFactory, dataModule);
    }

    /**
     * @param streamDescriptors The list of StreamDescriptors to check.
     * @param name The name of a StreamDescriptor.
     * @return Whether a descriptor with the name name is contained in a list of StreamDescriptors or not.
     * */
    private boolean containsStreamWithName(List<StreamDescriptor> streamDescriptors, String name) {
        for (StreamDescriptor streamDescriptor : streamDescriptors) {
            String streamName = streamDescriptor.getStreamName();
            Class<?> elementType = streamDescriptor.getElementType();
            if (streamName.equals(name) && elementType.equals(TweetPost.class)) {
                return true;
            }
        }
        return false;
    }

}