List of usage examples for twitter4j Status isRetweet
boolean isRetweet();
From source file:com.marpies.ane.twitter.utils.StatusUtils.java
License:Apache License
public static JSONObject getJSON(Status status) throws JSONException { JSONObject statusJSON = new JSONObject(); statusJSON.put("id", status.getId()); statusJSON.put("idStr", String.valueOf(status.getId())); statusJSON.put("text", status.getText()); statusJSON.put("replyToUserID", status.getInReplyToUserId()); statusJSON.put("replyToStatusID", status.getInReplyToStatusId()); statusJSON.put("likesCount", status.getFavoriteCount()); statusJSON.put("retweetCount", status.getRetweetCount()); statusJSON.put("isRetweet", status.isRetweet()); statusJSON.put("isSensitive", status.isPossiblySensitive()); statusJSON.put("createdAt", status.getCreatedAt()); Status retweetedStatus = status.getRetweetedStatus(); if (retweetedStatus != null) { statusJSON.put("retweetedStatus", getJSON(retweetedStatus)); }/* w w w . j av a 2 s . c o m*/ User user = status.getUser(); if (user != null) { statusJSON.put("user", UserUtils.getJSON(user)); } return statusJSON; }
From source file:com.mothsoft.alexis.engine.retrieval.TwitterRetrievalTaskImpl.java
License:Apache License
private void handleSourceImpl(final TwitterSource twitterSource) { final SocialConnection socialConnection = twitterSource.getSocialConnection(); final AccessToken accessToken = new AccessToken(socialConnection.getOauthToken(), socialConnection.getOauthTokenSecret()); final List<Status> statuses = this.twitterService.getHomeTimeline(accessToken, twitterSource.getLastTweetId(), (short) 800); if (statuses != null && statuses.size() > 0) { logger.info("Twitter retrieval found " + statuses.size() + " Tweets for user: " + socialConnection.getRemoteUsername()); // the newest tweet in the timeline will be our starting point for // the next fetch twitterSource.setLastTweetId(statuses.get(0).getId()); // import these in reverse order to ensure newest ones have the // highest document IDs Collections.reverse(statuses); final Long userId = twitterSource.getUserId(); final User user = this.userDao.get(userId); for (final Status status : statuses) { final Long tweetId = status.getId(); Tweet tweet = this.tweetDao.getTweetByRemoteTweetId(tweetId); final boolean isAdd = (tweet == null); if (isAdd) { // TODO - is this right? // Twitter allows 2 different styles of retweets. The // ones that are actually retweets show as tweeted by the // original user. Others may show // "RT @original thing original said" tweeted // by the new person final boolean retweet = status.isRetweet(); final twitter4j.User tweeter; final String text; twitter4j.User retweeter = null; final List<TweetLink> links; final List<TweetMention> mentions; final List<TweetHashtag> hashtags; if (retweet) { tweeter = status.getRetweetedStatus().getUser(); text = status.getRetweetedStatus().getText(); retweeter = status.getUser(); links = readLinks(status.getRetweetedStatus()); mentions = readMentions(status.getRetweetedStatus()); hashtags = readHashtags(status.getRetweetedStatus()); } else { tweeter = status.getUser(); text = status.getText(); links = readLinks(status); mentions = readMentions(status); hashtags = readHashtags(status); }/*from w w w . j av a 2 s. co m*/ final URL profileImageUrl = tweeter.getProfileImageUrlHttps(); final Date createdAt = status.getCreatedAt(); tweet = new Tweet(tweetId, createdAt, tweeter.getScreenName(), tweeter.getName(), profileImageUrl, text, links, mentions, hashtags, retweet, retweet ? retweeter.getScreenName() : null); this.documentDao.add(tweet); } final DocumentUser documentUser = new DocumentUser(tweet, user); if (isAdd || !tweet.getDocumentUsers().contains(documentUser)) { tweet.getDocumentUsers().add(new DocumentUser(tweet, user)); this.documentDao.update(tweet); } } } else { logger.info("Twitter retrieval found no Tweets for user: " + socialConnection.getRemoteUsername()); } twitterSource.setRetrievalDate(new Date()); this.sourceDao.update(twitterSource); }
From source file:com.narvis.frontend.twitter.input.Input.java
License:Open Source License
private boolean shouldAnswerTweet(Status status) throws TwitterException { // Checking for favorite is useless here return (status.getUser().getId() != this.stream.getId()) && !status.isRetweet() && isMentionnedIn(status.getUserMentionEntities()); }
From source file:com.twitter.graphjet.demo.TwitterStreamReader.java
License:Open Source License
public static void main(String[] argv) throws Exception { final TwitterStreamReaderArgs args = new TwitterStreamReaderArgs(); CmdLineParser parser = new CmdLineParser(args, ParserProperties.defaults().withUsageWidth(90)); try {/* ww w . j a v a2s. co m*/ parser.parseArgument(argv); } catch (CmdLineException e) { System.err.println(e.getMessage()); parser.printUsage(System.err); return; } final Date demoStart = new Date(); final MultiSegmentPowerLawBipartiteGraph userTweetBigraph = new MultiSegmentPowerLawBipartiteGraph( args.maxSegments, args.maxEdgesPerSegment, args.leftSize, args.leftDegree, args.leftPowerLawExponent, args.rightSize, args.rightDegree, args.rightPowerLawExponent, new IdentityEdgeTypeMask(), new NullStatsReceiver()); final MultiSegmentPowerLawBipartiteGraph tweetHashtagBigraph = new MultiSegmentPowerLawBipartiteGraph( args.maxSegments, args.maxEdgesPerSegment, args.leftSize, args.leftDegree, args.leftPowerLawExponent, args.rightSize, args.rightDegree, args.rightPowerLawExponent, new IdentityEdgeTypeMask(), new NullStatsReceiver()); // Note that we're keeping track of the nodes on the left and right sides externally, apart from the bigraphs, // because the bigraph currently does not provide an API for enumerating over nodes. Currently, this is liable to // running out of memory, but this is fine for the demo. Long2ObjectOpenHashMap<String> users = new Long2ObjectOpenHashMap<>(); LongOpenHashSet tweets = new LongOpenHashSet(); Long2ObjectOpenHashMap<String> hashtags = new Long2ObjectOpenHashMap<>(); // It is accurate of think of these two data structures as holding all users and tweets observed on the stream since // the demo program was started. StatusListener listener = new StatusListener() { long statusCnt = 0; public void onStatus(Status status) { String screenname = status.getUser().getScreenName(); long userId = status.getUser().getId(); long tweetId = status.getId(); long resolvedTweetId = status.isRetweet() ? status.getRetweetedStatus().getId() : status.getId(); HashtagEntity[] hashtagEntities = status.getHashtagEntities(); userTweetBigraph.addEdge(userId, resolvedTweetId, (byte) 0); if (!users.containsKey(userId)) { users.put(userId, screenname); } if (!tweets.contains(tweetId)) { tweets.add(tweetId); } if (!tweets.contains(resolvedTweetId)) { tweets.add(resolvedTweetId); } for (HashtagEntity entity : hashtagEntities) { long hashtagHash = (long) entity.getText().toLowerCase().hashCode(); tweetHashtagBigraph.addEdge(tweetId, hashtagHash, (byte) 0); if (!hashtags.containsKey(hashtagHash)) { hashtags.put(hashtagHash, entity.getText().toLowerCase()); } } statusCnt++; // Note that status updates are currently performed synchronously (i.e., blocking). Best practices dictate that // they should happen on another thread so as to not interfere with ingest, but this is okay for the pruposes // of the demo and the volume of the sample stream. // Minor status update: just print counters. if (statusCnt % args.minorUpdateInterval == 0) { long duration = (new Date().getTime() - demoStart.getTime()) / 1000; System.out.println(String.format( "%tc: %,d statuses, %,d unique tweets, %,d unique hashtags (observed); " + "%.2f edges/s; totalMemory(): %,d bytes, freeMemory(): %,d bytes", new Date(), statusCnt, tweets.size(), hashtags.size(), (float) statusCnt / duration, Runtime.getRuntime().totalMemory(), Runtime.getRuntime().freeMemory())); } // Major status update: iterate over right and left nodes. if (statusCnt % args.majorUpdateInterval == 0) { int leftCnt = 0; LongIterator leftIter = tweets.iterator(); while (leftIter.hasNext()) { if (userTweetBigraph.getLeftNodeDegree(leftIter.nextLong()) != 0) leftCnt++; } int rightCnt = 0; LongIterator rightIter = hashtags.keySet().iterator(); while (rightIter.hasNext()) { if (userTweetBigraph.getRightNodeDegree(rightIter.nextLong()) != 0) rightCnt++; } System.out.println(String.format("%tc: Current user-tweet graph state: %,d left nodes (users), " + "%,d right nodes (tweets)", new Date(), leftCnt, rightCnt)); } } public void onScrubGeo(long userId, long upToStatusId) { } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onStallWarning(StallWarning warning) { } public void onException(Exception e) { e.printStackTrace(); } }; TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); twitterStream.addListener(listener); twitterStream.sample(); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); Server jettyServer = new Server(args.port); jettyServer.setHandler(context); context.addServlet(new ServletHolder(new TopUsersServlet(userTweetBigraph, users)), "/userTweetGraph/topUsers"); context.addServlet( new ServletHolder( new TopTweetsServlet(userTweetBigraph, tweets, TopTweetsServlet.GraphType.USER_TWEET)), "/userTweetGraph/topTweets"); context.addServlet(new ServletHolder( new TopTweetsServlet(tweetHashtagBigraph, tweets, TopTweetsServlet.GraphType.TWEET_HASHTAG)), "/tweetHashtagGraph/topTweets"); context.addServlet(new ServletHolder(new TopHashtagsServlet(tweetHashtagBigraph, hashtags)), "/tweetHashtagGraph/topHashtags"); context.addServlet(new ServletHolder(new GetEdgesServlet(userTweetBigraph, GetEdgesServlet.Side.LEFT)), "/userTweetGraphEdges/users"); context.addServlet(new ServletHolder(new GetEdgesServlet(userTweetBigraph, GetEdgesServlet.Side.RIGHT)), "/userTweetGraphEdges/tweets"); context.addServlet(new ServletHolder(new GetEdgesServlet(tweetHashtagBigraph, GetEdgesServlet.Side.LEFT)), "/tweetHashtagGraphEdges/tweets"); context.addServlet(new ServletHolder(new GetEdgesServlet(tweetHashtagBigraph, GetEdgesServlet.Side.RIGHT)), "/tweetHashtagGraphEdges/hashtags"); context.addServlet(new ServletHolder(new GetSimilarHashtagsServlet(tweetHashtagBigraph, hashtags)), "/similarHashtags"); System.out.println(String.format("%tc: Starting service on port %d", new Date(), args.port)); try { jettyServer.start(); jettyServer.join(); } finally { jettyServer.destroy(); } }
From source file:crawling.FoundUsersBySearchGeo.java
License:Apache License
private static void doASearch(Twitter twitter, Query query) throws TwitterException { //try {//w ww.j a v a 2s. c om QueryResult result = twitter.search(query); List<Status> tweets = result.getTweets(); int thisCount = 0; for (Status tweet : tweets) { //System.out.println("@" + tweet.getFromUser() + tweet.getId() + " - " + tweet.getText()); // Check the tweet Long id = tweet.getUser().getId(); String screenName = tweet.getUser().getScreenName(); /*String location = ""; if (tweet.getUser().getLocation() != null){ location += tweet.getUser().getLocation(); if (tweet.getGeoLocation() != null){ location += "::" + tweet.getGeoLocation().toString(); }*/ String location = tweet.getUser().getLocation() + "::" + tweet.getGeoLocation(); // If the tweet is a retweet, the source of the tweet is from the target area if (tweet.isRetweet()) { id = tweet.getRetweetedStatus().getUser().getId(); screenName = tweet.getRetweetedStatus().getUser().getScreenName(); //location = tweet.getRetweetedStatus().getUser().getLocation(); location = tweet.getRetweetedStatus().getUser().getLocation() + "::" + tweet.getRetweetedStatus().getGeoLocation(); } if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(id, screenName, location); thisCount++; } } if (currentIndex < histCount) { avgUsers[currentIndex] = thisCount; currentIndex++; } else { currentIndex = 0; } /* Calculate the average #users in last 10 times */ double sum = 0; for (int num : avgUsers) sum += num; System.out.print( "\r" + count + ", the average number of users in last " + histCount + " is: " + sum / histCount); //} catch (TwitterException te) { // te.printStackTrace(); // System.out.println("Failed to search tweets: " + te.getMessage()); // System.exit(-1); //} }
From source file:DataCollection.ActiveTrendsStatusListener.java
@Override public void onStatus(Status arg0) { if (arg0.isRetweet()) return;/*from w w w . j a v a 2 s .c om*/ ArrayList<Trend> trends = associatedStreamer.getActiveTrends(); Trend associatedTrend = null; for (int i = 0; i < trends.size(); i++) { if (arg0.toString().contains(trends.get(i).toString())) { associatedTrend = trends.get(i); break; } } if (associatedTrend == null) return; Tweet t = new Tweet(arg0.getText(), null, associatedTrend.toString(), arg0.getUser().getName(), new Date()); cache.addDatum(t); }
From source file:de.binfalse.jatter.processors.TwitterStatusProcessor.java
License:Open Source License
/** * Translate twitter status.//from w w w. ja v a 2 s . c om * * @param status * the status * @return the string */ public static String translateTwitterStatus(Status status) { String msg = ""; if (status.getUser() != null && status.getUser().getScreenName() != null) msg += "*" + status.getUser().getScreenName() + "*: "; if (!status.isRetweet()) msg += JatterTools.processTwitterMessag(status.getText()); msg += " [" + App.printDateFormat.format(status.getCreatedAt()); if (status.getGeoLocation() != null) msg += " - " + status.getGeoLocation().getLatitude() + "," + status.getGeoLocation().getLongitude(); msg += " - " + status.getId(); msg += "]"; if (status.isRetweet()) msg += "\n *RT* > " + translateTwitterStatus(status.getRetweetedStatus()); if (status.getQuotedStatus() != null) msg += "\n *QT* > " + translateTwitterStatus(status.getQuotedStatus()); return msg; }
From source file:de.vanita5.twittnuker.model.ParcelableStatus.java
License:Open Source License
public ParcelableStatus(final Status orig, final long account_id, final boolean is_gap) { this.is_gap = is_gap; this.account_id = account_id; id = orig.getId();// w ww . j a v a 2 s. c om timestamp = getTime(orig.getCreatedAt()); is_retweet = orig.isRetweet(); final Status retweeted = orig.getRetweetedStatus(); final User retweet_user = retweeted != null ? orig.getUser() : null; retweet_id = retweeted != null ? retweeted.getId() : -1; //NOTE getTime(orig.getCreatedAt()) retweet_timestamp = retweeted != null ? getTime(retweeted.getCreatedAt()) : -1; retweeted_by_id = retweet_user != null ? retweet_user.getId() : -1; retweeted_by_name = retweet_user != null ? retweet_user.getName() : null; retweeted_by_screen_name = retweet_user != null ? retweet_user.getScreenName() : null; retweeted_by_profile_image = retweet_user != null ? ParseUtils.parseString(retweet_user.getProfileImageUrlHttps()) : null; final Status status = retweeted != null ? retweeted : orig; final User user = status.getUser(); user_id = user.getId(); user_name = user.getName(); user_screen_name = user.getScreenName(); user_profile_image_url = ParseUtils.parseString(user.getProfileImageUrlHttps()); user_is_protected = user.isProtected(); user_is_verified = user.isVerified(); user_is_following = user.isFollowing(); text_html = formatStatusText(status); media = ParcelableMedia.fromEntities(status); text_plain = status.getText(); retweet_count = status.getRetweetCount(); favorite_count = status.getFavoriteCount(); reply_count = status.getReplyCount(); descendent_reply_count = status.getDescendentReplyCount(); in_reply_to_name = getInReplyToName(status); in_reply_to_screen_name = status.getInReplyToScreenName(); in_reply_to_status_id = status.getInReplyToStatusId(); in_reply_to_user_id = status.getInReplyToUserId(); source = status.getSource(); location = new ParcelableLocation(status.getGeoLocation()); is_favorite = status.isFavorited(); text_unescaped = toPlainText(text_html); my_retweet_id = retweeted_by_id == account_id ? id : -1; is_possibly_sensitive = status.isPossiblySensitive(); mentions = ParcelableUserMention.fromUserMentionEntities(status.getUserMentionEntities()); first_media = media != null && media.length > 0 ? media[0].url : null; }
From source file:de.vanita5.twittnuker.util.ContentValuesCreator.java
License:Open Source License
public static ContentValues makeStatusContentValues(final Status orig, final long accountId, final boolean largeProfileImage) { if (orig == null || orig.getId() <= 0) return null; final ContentValues values = new ContentValues(); values.put(Statuses.ACCOUNT_ID, accountId); values.put(Statuses.STATUS_ID, orig.getId()); values.put(Statuses.STATUS_TIMESTAMP, orig.getCreatedAt().getTime()); values.put(Statuses.MY_RETWEET_ID, orig.getCurrentUserRetweet()); final boolean isRetweet = orig.isRetweet(); final Status status; final Status retweetedStatus = isRetweet ? orig.getRetweetedStatus() : null; if (retweetedStatus != null) { final User retweetUser = orig.getUser(); values.put(Statuses.RETWEET_ID, retweetedStatus.getId()); values.put(Statuses.RETWEET_TIMESTAMP, retweetedStatus.getCreatedAt().getTime()); values.put(Statuses.RETWEETED_BY_USER_ID, retweetUser.getId()); values.put(Statuses.RETWEETED_BY_USER_NAME, retweetUser.getName()); values.put(Statuses.RETWEETED_BY_USER_SCREEN_NAME, retweetUser.getScreenName()); values.put(Statuses.RETWEETED_BY_USER_PROFILE_IMAGE, ParseUtils.parseString(retweetUser.getProfileImageUrlHttps())); status = retweetedStatus;//from ww w . j ava2s .com } else { status = orig; } final User user = status.getUser(); if (user != null) { final long userId = user.getId(); final String profileImageUrl = ParseUtils.parseString(user.getProfileImageUrlHttps()); final String name = user.getName(), screenName = user.getScreenName(); values.put(Statuses.USER_ID, userId); values.put(Statuses.USER_NAME, name); values.put(Statuses.USER_SCREEN_NAME, screenName); values.put(Statuses.IS_PROTECTED, user.isProtected()); values.put(Statuses.IS_VERIFIED, user.isVerified()); values.put(Statuses.USER_PROFILE_IMAGE_URL, largeProfileImage ? getBiggerTwitterProfileImage(profileImageUrl) : profileImageUrl); values.put(CachedUsers.IS_FOLLOWING, user.isFollowing()); } final String text_html = Utils.formatStatusText(status); values.put(Statuses.TEXT_HTML, text_html); values.put(Statuses.TEXT_PLAIN, status.getText()); values.put(Statuses.TEXT_UNESCAPED, toPlainText(text_html)); values.put(Statuses.RETWEET_COUNT, status.getRetweetCount()); values.put(Statuses.REPLY_COUNT, status.getReplyCount()); values.put(Statuses.DESCENDENT_REPLY_COUNT, status.getDescendentReplyCount()); values.put(Statuses.IN_REPLY_TO_STATUS_ID, status.getInReplyToStatusId()); values.put(Statuses.IN_REPLY_TO_USER_ID, status.getInReplyToUserId()); values.put(Statuses.IN_REPLY_TO_USER_NAME, Utils.getInReplyToName(status)); values.put(Statuses.IN_REPLY_TO_USER_SCREEN_NAME, status.getInReplyToScreenName()); values.put(Statuses.SOURCE, status.getSource()); values.put(Statuses.IS_POSSIBLY_SENSITIVE, status.isPossiblySensitive()); final GeoLocation location = status.getGeoLocation(); if (location != null) { values.put(Statuses.LOCATION, location.getLatitude() + "," + location.getLongitude()); } values.put(Statuses.IS_RETWEET, isRetweet); values.put(Statuses.IS_FAVORITE, status.isFavorited()); final ParcelableMedia[] media = ParcelableMedia.fromEntities(status); if (media != null) { values.put(Statuses.MEDIA, JSONSerializer.toJSONArrayString(media)); values.put(Statuses.FIRST_MEDIA, media[0].url); } final ParcelableUserMention[] mentions = ParcelableUserMention.fromStatus(status); if (mentions != null) { values.put(Statuses.MENTIONS, JSONSerializer.toJSONArrayString(mentions)); } return values; }
From source file:de.vanita5.twittnuker.util.TwitterContentUtils.java
License:Open Source License
@NonNull public static String getInReplyToName(@NonNull final Status status) { final Status orig = status.isRetweet() ? status.getRetweetedStatus() : status; final long inReplyToUserId = status.getInReplyToUserId(); final UserMentionEntity[] entities = status.getUserMentionEntities(); if (entities == null) return orig.getInReplyToScreenName(); for (final UserMentionEntity entity : entities) { if (inReplyToUserId == entity.getId()) return entity.getName(); }/*from w w w.j av a2 s. c o m*/ return orig.getInReplyToScreenName(); }