List of usage examples for twitter4j Status getId
long getId();
From source file:Collector.TweetCollector.java
public static List<Status> getTweets(final String q) { Timer timer = new Timer(); TimerTask hourlyTask = new TimerTask() { @Override/*from www. jav a 2 s. c o m*/ public void run() { long amountOfTweets = 0; try { long maxID = -1; Query query = new Query(q); //printTimeLine(query); Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchLimit = rateLimitStatus.get("/search/tweets"); for (int batchNumber = 0; MAX_QUERIES < 10; batchNumber++) { System.out.printf("\n\n!!! batch %d\n\n", batchNumber); if (searchLimit.getRemaining() == 0) { // so as to not get blocked by twitter Thread.sleep(searchLimit.getSecondsUntilReset() + 3 * 1001); } query.setCount(TWEETS_PER_QUERY);// constant value of 100 query.setResultType(Query.ResultType.recent); query.setLang("en");// only english tweets if (maxID != -1) { query.setMaxId(maxID - 1);// so the first querys not set to previous max } QueryResult result = twitter.search(query); if (result.getTweets().size() == 0) { break; } for (Status s : result.getTweets()) { amountOfTweets++; if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } storeTweet(s);// where stored in db System.out.printf("At%s : %s\n", // debugging purposes s.getCreatedAt().toString(), s.getText()); searchLimit = result.getRateLimitStatus(); //resets System.out.printf("\n\nA total of %d tweet retrieved\n", amountOfTweets); } } } catch (TwitterException te) { System.out.println("Error Code :" + te.getErrorCode()); System.out.println("Exception Code " + te.getExceptionCode()); System.out.println("Status Code " + te.getStatusCode()); if (te.getStatusCode() == 401) { System.out.println("Twitter Error :\nAuthentication " + "credentials (https://dev.twitter.com/auth) " + " are either missing of incorrect, " + "\nplease check consumer key /secret"); } } catch (InterruptedException ex) { } } }; // schedule the task to run starting now and then every hour... timer.schedule(hourlyTask, 0l, 1000 * 60 * 60); return statuses; }
From source file:Collector.TweetCollector.java
public static void storeTweet(Status tweet) { BasicDBObject basicOBJ = new BasicDBObject(); basicOBJ.put("user_name", tweet.getUser().getScreenName()); basicOBJ.put("retweet_count", tweet.getRetweetCount()); basicOBJ.put("tweet_followers_count", tweet.getUser().getFollowersCount()); UserMentionEntity[] mentioned = tweet.getUserMentionEntities(); basicOBJ.put("tweet_mentioned_count", mentioned.length); basicOBJ.put("tweet_ID", tweet.getId()); basicOBJ.put("tweet_text", tweet.getText()); basicOBJ.put("created_at", tweet.getCreatedAt()); try {/*from w w w .j a v a 2 s.co m*/ items.insert(basicOBJ); } catch (Exception e) { System.out.println("Could not store Tweet please try again" + e.getMessage()); } }
From source file:collector.TwitterCollector.java
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 {/*from w w w . java2 s.c o m*/ 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; }
From source file:collector.TwitterCollector.java
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 {//from w ww . j av a2 s. c o m 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; }
From source file:com.ahuralab.mozaic.db.Message.java
License:Open Source License
public static Message create(Status status) { User user = status.getUser();//from w w w . j av a 2 s.c o m return new Message(status.getText(), status.getCreatedAt(), status.getRetweetCount(), user.getId(), user.getName(), user.getScreenName(), user.getLocation(), user.getDescription(), user.getProfileImageURL(), status.getId(), 0, 0, user.getFollowersCount(), user.getStatusesCount()); }
From source file:com.alainesp.fan.sanderson.SummaryFragment.java
License:Open Source License
protected int doWork() { int state = DownloadParseSaveTask.STATE_SUCCESS; try {//from w ww. java 2s. c o m List<twitter4j.Status> tweets; // Twitter client configuration ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(TwitterAPISecrets.CONSUMER_KEY) .setOAuthConsumerSecret(TwitterAPISecrets.CONSUMER_SECRET); // TODO: Use guest authentication instead of application builder.setApplicationOnlyAuthEnabled(true).setDebugEnabled(false).setGZIPEnabled(true); Twitter twitter = new TwitterFactory(builder.build()).getInstance(); twitter.getOAuth2Token(); // Get the tweets long lastTweetID = DB.Tweet.getLastTweetID(); if (lastTweetID <= 0) tweets = twitter.getUserTimeline("BrandSanderson"); else tweets = twitter.getUserTimeline("BrandSanderson", new Paging(lastTweetID)); if (tweets != null) { long brandonID = 28187205; Hashtable<Long, DB.Tweet> findTweet = new Hashtable<>(tweets.size() * 16, 0.25f); List<DB.Tweet> dbTweets = new ArrayList<>(tweets.size()); for (twitter4j.Status tweet : tweets) { // Create the tweet if (tweet.isRetweet()) tweet = tweet.getRetweetedStatus(); long tweetID = tweet.getId(); // TODO: Include the tweets in the DB if (findTweet.get(tweetID) == null)// This eliminate tweets already in the replies tree { DB.Tweet dbTweet = new DB.Tweet(tweetID, getTweetText(tweet), tweet.getCreatedAt(), false, tweet.getUser().getName(), tweet.getUser().getBiggerProfileImageURLHttps()); InternetHelper.getRemoteFile(tweet.getUser().getBiggerProfileImageURLHttps()); dbTweets.add(dbTweet); findTweet.put(tweetID, dbTweet); // Traverse the tree of the replies tweets twitter4j.Status treeTweet = tweet; while (treeTweet != null && treeTweet.getInReplyToStatusId() >= 0) { try { long id = treeTweet.getInReplyToStatusId(); long userID = treeTweet.getUser().getId(); treeTweet = null; if (findTweet.get(id) == null || brandonID == userID) treeTweet = twitter.showStatus(id); else// Remove duplicates. Not sure why they appear, but the difference of the text is a dot at the end. dbTweets.remove(dbTweets.size() - 1); } catch (Exception ignore) { } if (treeTweet != null) { findTweet.put(treeTweet.getId(), dbTweet); StringBuilder replyBuilder = new StringBuilder(); replyBuilder.append("<blockquote>"); // Profile image replyBuilder.append("<img src=\""); InternetHelper.getRemoteFile(treeTweet.getUser().getBiggerProfileImageURLHttps()); replyBuilder.append(treeTweet.getUser().getBiggerProfileImageURLHttps()); replyBuilder.append("\"/> <b>"); // Username - date replyBuilder.append(treeTweet.getUser().getName()); replyBuilder.append("</b> @"); replyBuilder.append(treeTweet.getUser().getScreenName()); replyBuilder.append(" - "); replyBuilder .append(TwitterFragment.showDateFormat.format(treeTweet.getCreatedAt())); // Tweet text replyBuilder.append("<br/>"); replyBuilder.append(getTweetText(treeTweet)); // Remaining replyBuilder.append(dbTweet.htmlReply); replyBuilder.append("</blockquote>"); dbTweet.htmlReply = replyBuilder.toString(); } } } } DB.Tweet.updateTwitter(dbTweets); } } catch (Exception e) { Logger.reportError(e.toString()); state = DownloadParseSaveTask.STATE_ERROR_PARSING; } return state; }
From source file:com.amazonbird.db.data.Announcer.java
License:Open Source License
public long reply(String message, long statusId) throws Exception { long replyStatus = -1; try {//from w ww . j a v a 2 s . c o m Status status = getTwitterProxy().updateStatus(new StatusUpdate("message").inReplyToStatusId(statusId)); replyStatus = status.getId(); } catch (TwitterException e) { logger.error("Error while replying: " + statusId + " Announcer: " + screenName); } return replyStatus; }
From source file:com.appspot.bitlyminous.handler.twitter.NearByHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { GeoLocation geoLocation = tweet.getGeoLocation(); String text = getMentionText(tweet); if (geoLocation == null) { StatusUpdate reply = new StatusUpdate( ApplicationResources.getLocalizedString("com.appspot.bitlyminous.message.noLocation", new String[] { "@" + tweet.getUser().getScreenName() })); reply.setInReplyToStatusId(tweet.getId()); return reply; } else {/*from www .jav a2 s . c om*/ try { GeoQuery query = new GeoQuery(geoLocation); List<Tip> nearbyTips = getFoursquareGateway() .getNearbyTips(new com.appspot.bitlyminous.entity.GeoLocation(geoLocation.getLatitude(), geoLocation.getLongitude()), 1); String status = buildTipsStatus(nearbyTips); if (nearbyTips.isEmpty()) { List<Venue> nearbyVenues = new ArrayList<Venue>(); if (text != null) { nearbyVenues = getFoursquareGateway().getNearbyVenues( new com.appspot.bitlyminous.entity.GeoLocation(geoLocation.getLatitude(), geoLocation.getLongitude()), text, 1); } status = status + buildVenuesStatus(nearbyVenues); if (nearbyVenues.isEmpty()) { List<Place> nearbyPlaces = getTwitterClient().getNearbyPlaces(query); status = status + buildPlacesStatus(nearbyPlaces); } } status = "@" + tweet.getUser().getScreenName() + " " + status; StatusUpdate reply = new StatusUpdate(status); reply.setInReplyToStatusId(tweet.getId()); return reply; } catch (Exception e) { logger.log(Level.SEVERE, "Error while getting nearby places", e); } return null; } }
From source file:com.appspot.bitlyminous.handler.twitter.RetweetRelatedTweetsHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {/*from w w w. j av a2s . co m*/ WebSearchQuery webSearchQuery = getGoogleWebSearchQuery(); webSearchQuery.withCustomeSearchEngineId(ApplicationConstants.GOOGLE_API_CSE); List<Url> urls = context.getUrls(); for (Url entity : urls) { // List<Tweet> relatedTweets = new ArrayList<Tweet>(); List<String> tags = entity.getTags(); if (!tags.isEmpty()) { List<WebResult> relatedTweets = webSearchQuery.withQuery(createQuery(tags)).list(); // List<Tweet> tweets = getTweetsFromCSE(results); // relatedTweets.addAll(tweets); relatedTweets = getBestMatches(tweet, relatedTweets, 1); if (!relatedTweets.isEmpty()) { StatusUpdate reply = new StatusUpdate(buildStatus(tweet, relatedTweets)); reply.setInReplyToStatusId(tweet.getId()); return reply; } } } } catch (Exception e) { logger.log(Level.SEVERE, "Error while persisting urls.", e); } return null; }
From source file:com.appspot.bitlyminous.handler.twitter.ScanLinkHandler.java
License:Apache License
@Override public StatusUpdate execute(Status tweet) { try {// www . j ava 2 s. c om List<String> shortUrls = extractBitlyUrls(tweet.getText()); if (!shortUrls.isEmpty()) { Set<Url> longUrls = getBitlyClient() .call(Bitly.expand(shortUrls.toArray(new String[shortUrls.size()]))); GoogleSafeBrowsingGateway gateway = getGoogleSafeBrowsingGateway(); for (Url url : longUrls) { if (!isEmpty(url.getLongUrl())) { if (gateway.isBlacklisted(url.getLongUrl())) { StatusUpdate reply = new StatusUpdate(ApplicationResources.getLocalizedString( "com.appspot.bitlyminous.message.badurl", new String[] { "@" + tweet.getUser().getScreenName(), trimText(tweet.getText(), 20), ApplicationConstants.GOOGLE_SAFE_BROWSING_REF_URL })); reply.setInReplyToStatusId(tweet.getId()); return reply; } if (gateway.isMalwarelisted(url.getLongUrl())) { StatusUpdate reply = new StatusUpdate(ApplicationResources.getLocalizedString( "com.appspot.bitlyminous.message.badurl", new String[] { "@" + tweet.getUser().getScreenName(), trimText(tweet.getText(), 20), ApplicationConstants.GOOGLE_SAFE_BROWSING_REF_URL })); reply.setInReplyToStatusId(tweet.getId()); return reply; } } } } } catch (Exception e) { logger.log(Level.SEVERE, "Error while scanning urls.", e); } return null; }