io.github.autsia.crowly.services.social.impl.TwitterSocialEndpoint.java Source code

Java tutorial

Introduction

Here is the source code for io.github.autsia.crowly.services.social.impl.TwitterSocialEndpoint.java

Source

/*
 * Copyright 2014 Dmytro Titov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.autsia.crowly.services.social.impl;

import io.github.autsia.crowly.model.Campaign;
import io.github.autsia.crowly.model.Mention;
import io.github.autsia.crowly.services.geolocation.GeoLocationService;
import io.github.autsia.crowly.services.sentiment.Sentiment;
import io.github.autsia.crowly.services.sentiment.SentimentAnalyzer;
import io.github.autsia.crowly.services.social.SocialEndpoint;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.twitter.api.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dmytro on 5/9/2014 at 18:20
 * Project: crowly
 */
@Service
public class TwitterSocialEndpoint implements SocialEndpoint {

    public static final String SEPARATOR = " OR ";

    protected GeoLocationService geoLocationService;
    protected Twitter twitter;
    protected SentimentAnalyzer sentimentAnalyzer;

    @Override
    public List<Mention> gatherMentions(Campaign campaign) {
        List<Mention> result = new ArrayList<>();
        String language = campaign.getLanguages().iterator().next();
        GeoCode geoCode = geoLocationService
                .convertCoordinatesToGeoCode(campaign.getLocations().values().iterator().next());
        SearchParameters searchParameters = new SearchParameters(
                StringUtils.join(campaign.getKeywords().toArray(), SEPARATOR)).lang(language).geoCode(geoCode);
        SearchResults searchResults = twitter.searchOperations().search(searchParameters);
        for (Tweet tweet : searchResults.getTweets()) {
            String text = tweet.getText();
            Pair<Sentiment, Float> sentiment = sentimentAnalyzer.analyze(text);
            if (sentiment.getLeft().equals(campaign.getSentiment())
                    && sentiment.getRight() > campaign.getThreshold()) {
                result.add(buildMention(campaign, tweet, text));
            }
        }
        return result;
    }

    protected Mention buildMention(Campaign campaign, Tweet tweet, String text) {
        Mention mention = new Mention();
        mention.setId(String.valueOf(tweet.getId()) + campaign.getId());
        mention.setCampaignId(campaign.getId());
        mention.setDatePublished(tweet.getCreatedAt());
        mention.setProfileName(tweet.getUser().getName());
        mention.setProfileName(tweet.getUser().getName());
        mention.setLinkToProfile(tweet.getUser().getProfileUrl());
        // TODO : mention.setLinkToEntry(tweet.getSource());
        mention.setText(text);
        return mention;
    }

    @Override
    public String getName() {
        return twitter.getClass().getSimpleName().replace("Template", "");
    }

    @Autowired
    public void setGeoLocationService(GeoLocationService geoLocationService) {
        this.geoLocationService = geoLocationService;
    }

    @Autowired
    public void setTwitter(Twitter twitter) {
        this.twitter = twitter;
    }

    @Autowired
    public void setSentimentAnalyzer(SentimentAnalyzer sentimentAnalyzer) {
        this.sentimentAnalyzer = sentimentAnalyzer;
    }

}