socialImport.twitter.TweetToDatabaseImportListener.java Source code

Java tutorial

Introduction

Here is the source code for socialImport.twitter.TweetToDatabaseImportListener.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.Date;

import data.IData;

import twitter4j.GeoLocation;
import twitter4j.Place;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import types.Author;
import types.Gender;
import types.GeoTag;
import types.StreamDescriptor;
import types.TweetPost;

class TweetToDatabaseImportListener implements StatusListener {

    private IData dataModule;
    private StreamDescriptor streamDescriptor;

    public TweetToDatabaseImportListener(StreamDescriptor streamDescriptor, IData dataModule) {
        this.streamDescriptor = streamDescriptor;
        this.dataModule = dataModule;
    }

    @Override
    public void onException(Exception exc) {
        //TODO React in case of too many API-Accesses. (Wait until the Twitter restrictions are lifted.)  
    }

    @Override
    public void onDeletionNotice(StatusDeletionNotice notice) {
    }

    @Override
    public void onScrubGeo(long longitude, long latitude) {
    }

    @Override
    public void onStallWarning(StallWarning warning) {
    }

    @Override
    public void onStatus(Status status) {
        Date dateOfCreation = status.getCreatedAt();
        String authorName = status.getUser().getName();
        Gender gender = Gender.unknown;
        int yearOfBirth = -1;
        Author author = new Author(authorName, gender, yearOfBirth);
        GeoTag geoTag = null;
        Place place = status.getPlace();
        GeoLocation[][] geoLocation = null;
        if (place != null) {
            geoLocation = place.getBoundingBoxCoordinates();
        }
        if (geoLocation != null) {
            float latitude = 0;
            float longitude = 0;
            int noOfCoordinates = geoLocation[0].length;
            for (int i = 0; i < noOfCoordinates; i++) {
                latitude += geoLocation[0][i].getLatitude();
                longitude += geoLocation[0][i].getLongitude();
            }
            latitude = latitude / noOfCoordinates;
            longitude = longitude / noOfCoordinates;
            geoTag = new GeoTag(latitude, longitude);
        }
        String postText = status.getText();
        long retweetCount = status.getRetweetCount();
        TweetPost tweet = new TweetPost(streamDescriptor, dateOfCreation, author, geoTag, postText, retweetCount);
        System.out.println(author);
        System.out.println(geoTag);
        System.out.println(status.getText());
        dataModule.savePost(tweet);

    }

    @Override
    public void onTrackLimitationNotice(int limitedMessages) {
    }

}