Java tutorial
/* * 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 collector; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import model.Tweet; import model.TwitterUser; import twitter4j.Paging; import twitter4j.Query; import twitter4j.QueryResult; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.conf.ConfigurationBuilder; /** * * @author fabio */ public class TwitterCollector { private final Twitter twitter; public TwitterCollector(List<String> credentials) { ConfigurationBuilder cb = new ConfigurationBuilder(); String OAuthConsumerKey = credentials.get(0); String OAuthConsumerSecret = credentials.get(1); String OAuthAccessToken = credentials.get(2); String OAuthAccessTokenSecret = credentials.get(3); cb.setDebugEnabled(true).setOAuthConsumerKey(OAuthConsumerKey).setOAuthConsumerSecret(OAuthConsumerSecret) .setOAuthAccessToken(OAuthAccessToken).setOAuthAccessTokenSecret(OAuthAccessTokenSecret); // .setJSONStoreEnabled(true); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); } public LinkedHashSet<Tweet> search(String queryExpression, int maxResults) { Query query = new Query(queryExpression); int numberOfTweets = maxResults;//512; long lastID = Long.MAX_VALUE; query = query.lang("pt"); List<Status> tweets = new ArrayList<>(); boolean finish = false; while ((tweets.size() < numberOfTweets) && !finish) { System.out.print("."); if (numberOfTweets - tweets.size() > 100) {//100) { query.setCount(100);//100); } else { query.setCount(numberOfTweets - tweets.size()); } try { QueryResult result = twitter.search(query); List<Status> resultList = result.getTweets(); if (resultList == null || resultList.isEmpty()) { finish = true; System.out.println("no foram encontrados mais tweets"); } else { tweets.addAll(resultList); for (Status t : tweets) { if (t.getId() < lastID) { lastID = t.getId(); } } } } catch (TwitterException ex) { System.err.println(ex.getMessage()); } query.setMaxId(lastID - 1); } LinkedHashSet<Tweet> out = new LinkedHashSet<>(); for (Status status : tweets) { if (!status.getText().startsWith("RT")) { TwitterUser user; user = new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName()) .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt()) .addCountTweets(status.getUser().getStatusesCount()) .addCountFavorites(status.getUser().getFavouritesCount()) .addCountFriends(status.getUser().getFriendsCount()) .addCountFollowers(status.getUser().getFollowersCount()); Tweet tweet = new Tweet().addUser(user).addText(status.getText()).addID(status.getId()) .addDate(status.getCreatedAt()) .addLatitude(status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() : Double.MAX_VALUE) .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude() : Double.MAX_VALUE); out.add(tweet); } } return out; } public LinkedHashSet<Tweet> userSearchData(String userName, int maxResults) { LinkedHashSet<Tweet> out = new LinkedHashSet<>(); Paging paging = new Paging(1, 180); int numberOfTweets = maxResults;//512; long lastID = Long.MAX_VALUE; ArrayList<Status> status = new ArrayList<>(); while (status.size() < numberOfTweets) { if (numberOfTweets - status.size() > 180) {//100) { paging.setCount(180);//100); } else { paging.setCount(numberOfTweets - status.size()); } try { List<Status> timeLine = twitter.getUserTimeline(userName, paging); status.addAll(timeLine); for (Status t : status) { if (t.getId() < lastID) { lastID = t.getId(); } } } catch (TwitterException ex) { System.err.println(ex.getMessage()); } paging.setMaxId(lastID - 1); } //armazenar os atributos interessantes a analise dos tweets int qtdretweet = 0; for (Status sta : status) { String text = sta.getText(); if (!sta.isRetweet() && !sta.isRetweeted() && !text.startsWith("RT")) { //&& !TweetMediaDetect.detect(text)) { TwitterUser user; user = new TwitterUser().addID(sta.getUser().getId()).addName(sta.getUser().getName()) .addLocation(sta.getUser().getLocation()).addDateSignin(sta.getUser().getCreatedAt()) .addCountTweets(sta.getUser().getStatusesCount()) .addCountFavorites(sta.getUser().getFavouritesCount()) .addCountFriends(sta.getUser().getFriendsCount()) .addCountFollowers(sta.getUser().getFollowersCount()); Tweet tweet = new Tweet().addUser(user).addText(sta.getText()).addID(sta.getId()) .addDate(sta.getCreatedAt()) .addLatitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLatitude() : Double.MAX_VALUE) .addLongitude(sta.getGeoLocation() != null ? sta.getGeoLocation().getLongitude() : Double.MAX_VALUE); out.add(tweet); } else { qtdretweet++; } } return out; } }