dhbw.clippinggorilla.external.twitter.TwitterUtils.java Source code

Java tutorial

Introduction

Here is the source code for dhbw.clippinggorilla.external.twitter.TwitterUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dhbw.clippinggorilla.external.twitter;

import dhbw.clippinggorilla.objects.article.Article;
import dhbw.clippinggorilla.objects.source.SourceUtils;

import dhbw.clippinggorilla.utilities.log.Log;
import dhbw.clippinggorilla.utilities.ressources.Props;
import java.time.Instant;
import twitter4j.conf.ConfigurationBuilder;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import twitter4j.MediaEntity;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

/**
 *
 * @author josua.frank
 */
public class TwitterUtils {

    private static TwitterFactory tf;

    /**
     * Configures Twitter API Keys
     */
    public static void config() {
        if (tf == null) {
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(true)
                    .setOAuthConsumerKey(Props.getOrDefault("OAuthConsumerKey", "8MEY2XDvYPblVVQgFdSzSrydQ"))
                    .setOAuthConsumerSecret(Props.getOrDefault("OAuthConsumerSecret",
                            "IpKkXw2gvfJCEwjGv2Yz5LwRv0Zn1B9nMLkQVleldyhdVPgPVj"))
                    .setOAuthAccessToken(Props.getOrDefault("OAuthAccessToken",
                            "2646251996-E0Glh4yG12AyJ6DM7PCy9sTKhs0w8gP4yCFFvU2"))
                    .setOAuthAccessTokenSecret(Props.getOrDefault("OAuthAccessTokenSecret",
                            "8D4sZcF8trluR1dFhqxhVoaf0xsrwTvwX93qkIbKFFxKP"));
            tf = new TwitterFactory(cb.build());
        }
    }

    /**
     * @param query
     * @return Searches for String Query
     */
    public static QueryResult searchTweets(Query query) {
        config();
        // The factory instance is re-useable and thread safe.
        Twitter twitter = tf.getInstance();
        QueryResult result = null;
        try {
            result = twitter.search(query);
        } catch (TwitterException e) {
            Log.error("Could not get Tweets", e);
        }
        return result;
    }

    /**
     * @param includedTagsSet
     * @param date
     * @return
     */
    private static Query queryBuilder(Set<String> includedTagsSet, LocalDateTime date) {
        List<String> includedTags = new ArrayList<>();

        includedTags.addAll(includedTagsSet);

        String query = "";

        if (includedTags.size() > 0) {
            query = replaceWhitespaces(includedTags.get(0));
            for (int i = 1; i < includedTags.size(); i++) {
                query += " OR " + replaceWhitespaces(includedTags.get(i));
            }
        }

        //System.out.println(date.toString());
        //System.out.println(query);
        Query result = new Query(query);
        result.setResultType(Query.ResultType.popular);
        result.setSince(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return result;

    }

    /**
     * @param string
     * @return
     *
     */
    private static String replaceWhitespaces(String string) {
        String word = " OR ";
        String result = "";
        List<String> allMatches = new ArrayList<>();

        if (StringUtils.countMatches(string, '"') > 0 && ((StringUtils.countMatches(string, '"') & 1) == 1)) {
            string = string.concat("\"");
        }

        Matcher m = Pattern.compile("[^,]+").matcher(string);
        while (m.find()) {
            allMatches.add(m.group());
        }

        for (String sub : allMatches) {
            sub = sub.trim();
            result = result.concat("\"" + sub + "\"" + word);
        }

        result = result.substring(0, result.length() - 4); // remove last "OR"

        return result;
    }

    /**
     * @param includedTagsSet
     * @param excludedTagsSet
     * @param sinceDate
     * @return Gets Tweets for Profile
     */
    public static LinkedHashSet<Article> getArticlesFromTwitter(Set<String> includedTagsSet,
            Set<String> excludedTagsSet, LocalDateTime sinceDate) {
        Query query = queryBuilder(includedTagsSet, sinceDate);
        LinkedHashSet<Article> result = new LinkedHashSet<>();
        QueryResult tweets = searchTweets(query);

        for (Status status : tweets.getTweets()) {
            result.add(fillArticle(status));
        }
        return result;
    }

    public static Article getArticleFromTwitter(long id) {
        try {
            config();
            // The factory instance is re-useable and thread safe.
            Twitter twitter = tf.getInstance();
            Status status = twitter.showStatus(id);
            return fillArticle(status);
        } catch (TwitterException ex) {
            Log.error("Tweet not found", ex);
        }
        return null;
    }

    private static Article fillArticle(Status status) {
        Article article = new Article();
        article.setTitle(status.getUser().getName());
        article.setDescription(status.getText());
        article.setBody(status.getText());
        article.setSource(SourceUtils.TWITTER);
        for (MediaEntity mediaEntity : status.getMediaEntities()) {
            if (!mediaEntity.getType().equals("video")) {
                article.setUrlToImage(mediaEntity.getMediaURL());
                break;
            }
        }
        if (article.getUrlToImage().isEmpty()) {
            article.setUrlToImage(status.getUser().getBiggerProfileImageURL());
        }
        article.setUrl("https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId());
        article.setId(status.getId() + "");
        article.setAuthor("@" + status.getUser().getScreenName());
        Date date = status.getCreatedAt();
        Instant instant = Instant.ofEpochMilli(date.getTime());
        LocalDateTime createdAt = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
        article.setPublishedAt(createdAt.toString());
        return article;
    }

    public static void main(String[] args) {
        Set<String> includedTagsSet = new HashSet<>();
        Set<String> excludedTagsSet = new HashSet<>();
        LocalDateTime date;

        includedTagsSet.add("bmw, mercedes");
        includedTagsSet.add("Audi, toyota");
        includedTagsSet.add("merkel");
        includedTagsSet.add("dat boi, pepe");
        includedTagsSet.add("dhbw");
        includedTagsSet.add("VW Golf");

        Query query = queryBuilder(includedTagsSet, LocalDateTime.of(2017, 5, 1, 0, 0));

        QueryResult result = searchTweets(query);

        for (Status status : result.getTweets()) {
            System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
            for (MediaEntity mediaEntity : status.getMediaEntities()) {
                System.out.println(mediaEntity.getType());
            }
            System.out.println("_________________________________________________");
        }
    }

}