List of usage examples for twitter4j Status getId
long getId();
From source file:com.daiv.android.twitter.data.sq_lite.ListDataSource.java
License:Apache License
public int insertTweets(List<Status> statuses, long listId) { ContentValues[] valueses = new ContentValues[statuses.size()]; for (int i = 0; i < statuses.size(); i++) { Status status = statuses.get(i); ContentValues values = new ContentValues(); String originalName = ""; long time = status.getCreatedAt().getTime(); long id = status.getId(); if (status.isRetweet()) { originalName = status.getUser().getScreenName(); status = status.getRetweetedStatus(); }/*w w w . j a va 2s . co m*/ String[] html = TweetLinkUtils.getLinksInStatus(status); String text = html[0]; String media = html[1]; String url = html[2]; String hashtags = html[3]; String users = html[4]; if (media.contains("/tweet_video/")) { media = media.replace("tweet_video", "tweet_video_thumb").replace(".mp4", ".png"); } values.put(ListSQLiteHelper.COLUMN_TEXT, text); values.put(ListSQLiteHelper.COLUMN_TWEET_ID, id); values.put(ListSQLiteHelper.COLUMN_NAME, status.getUser().getName()); values.put(ListSQLiteHelper.COLUMN_PRO_PIC, status.getUser().getOriginalProfileImageURL()); values.put(ListSQLiteHelper.COLUMN_SCREEN_NAME, status.getUser().getScreenName()); values.put(ListSQLiteHelper.COLUMN_TIME, time); values.put(ListSQLiteHelper.COLUMN_RETWEETER, originalName); values.put(ListSQLiteHelper.COLUMN_PIC_URL, media); values.put(ListSQLiteHelper.COLUMN_URL, url); values.put(ListSQLiteHelper.COLUMN_USERS, users); values.put(ListSQLiteHelper.COLUMN_HASHTAGS, hashtags); values.put(ListSQLiteHelper.COLUMN_LIST_ID, listId); values.put(ListSQLiteHelper.COLUMN_ANIMATED_GIF, TweetLinkUtils.getGIFUrl(status, url)); valueses[i] = values; } return insertMultiple(valueses); }
From source file:com.daiv.android.twitter.data.sq_lite.MentionsDataSource.java
License:Apache License
public synchronized void createTweet(Status status, int account, boolean initial) { ContentValues values = new ContentValues(); String originalName = ""; long id = status.getId(); long time = status.getCreatedAt().getTime(); String[] html = TweetLinkUtils.getLinksInStatus(status); String text = html[0];/*from ww w . j av a2 s . c om*/ String media = html[1]; String otherUrl = html[2]; String hashtags = html[3]; String users = html[4]; if (media.contains("/tweet_video/")) { media = media.replace("tweet_video", "tweet_video_thumb").replace(".mp4", ".png"); } values.put(MentionsSQLiteHelper.COLUMN_ACCOUNT, account); values.put(MentionsSQLiteHelper.COLUMN_TEXT, text); values.put(MentionsSQLiteHelper.COLUMN_TWEET_ID, id); values.put(MentionsSQLiteHelper.COLUMN_NAME, status.getUser().getName()); values.put(MentionsSQLiteHelper.COLUMN_PRO_PIC, status.getUser().getOriginalProfileImageURL()); values.put(MentionsSQLiteHelper.COLUMN_SCREEN_NAME, status.getUser().getScreenName()); values.put(MentionsSQLiteHelper.COLUMN_TIME, time); values.put(MentionsSQLiteHelper.COLUMN_RETWEETER, originalName); values.put(MentionsSQLiteHelper.COLUMN_UNREAD, 0); values.put(MentionsSQLiteHelper.COLUMN_PIC_URL, media); values.put(MentionsSQLiteHelper.COLUMN_URL, otherUrl); values.put(MentionsSQLiteHelper.COLUMN_PIC_URL, media); values.put(MentionsSQLiteHelper.COLUMN_USERS, users); values.put(MentionsSQLiteHelper.COLUMN_HASHTAGS, hashtags); values.put(MentionsSQLiteHelper.COLUMN_ANIMATED_GIF, TweetLinkUtils.getGIFUrl(status, otherUrl)); values.put(HomeSQLiteHelper.COLUMN_CONVERSATION, status.getInReplyToStatusId() == -1 ? 0 : 1); try { database.insert(MentionsSQLiteHelper.TABLE_MENTIONS, null, values); } catch (Exception e) { open(); database.insert(MentionsSQLiteHelper.TABLE_MENTIONS, null, values); } }
From source file:com.daiv.android.twitter.data.sq_lite.MentionsDataSource.java
License:Apache License
public synchronized void createTweet(Status status, int account) { ContentValues values = new ContentValues(); String originalName = ""; long id = status.getId(); long time = status.getCreatedAt().getTime(); String[] html = TweetLinkUtils.getLinksInStatus(status); String text = html[0];//from w ww . java 2 s. co m String media = html[1]; String otherUrl = html[2]; String hashtags = html[3]; String users = html[4]; if (media.contains("/tweet_video/")) { media = media.replace("tweet_video", "tweet_video_thumb").replace(".mp4", ".png"); } values.put(MentionsSQLiteHelper.COLUMN_ACCOUNT, account); values.put(MentionsSQLiteHelper.COLUMN_TEXT, text); values.put(MentionsSQLiteHelper.COLUMN_TWEET_ID, id); values.put(MentionsSQLiteHelper.COLUMN_NAME, status.getUser().getName()); values.put(MentionsSQLiteHelper.COLUMN_PRO_PIC, status.getUser().getOriginalProfileImageURL()); values.put(MentionsSQLiteHelper.COLUMN_SCREEN_NAME, status.getUser().getScreenName()); values.put(MentionsSQLiteHelper.COLUMN_TIME, time); values.put(MentionsSQLiteHelper.COLUMN_RETWEETER, originalName); values.put(MentionsSQLiteHelper.COLUMN_UNREAD, 1); values.put(MentionsSQLiteHelper.COLUMN_PIC_URL, media); values.put(MentionsSQLiteHelper.COLUMN_URL, otherUrl); values.put(MentionsSQLiteHelper.COLUMN_USERS, users); values.put(MentionsSQLiteHelper.COLUMN_HASHTAGS, hashtags); values.put(MentionsSQLiteHelper.COLUMN_ANIMATED_GIF, TweetLinkUtils.getGIFUrl(status, otherUrl)); values.put(HomeSQLiteHelper.COLUMN_CONVERSATION, status.getInReplyToStatusId() == -1 ? 0 : 1); try { database.insert(MentionsSQLiteHelper.TABLE_MENTIONS, null, values); } catch (Exception e) { open(); database.insert(MentionsSQLiteHelper.TABLE_MENTIONS, null, values); } }
From source file:com.daiv.android.twitter.data.sq_lite.MentionsDataSource.java
License:Apache License
public synchronized int insertTweets(List<Status> statuses, int account) { ContentValues[] valueses = new ContentValues[statuses.size()]; for (int i = 0; i < statuses.size(); i++) { Status status = statuses.get(i); ContentValues values = new ContentValues(); String originalName = ""; long id = status.getId(); long time = status.getCreatedAt().getTime(); String[] html = TweetLinkUtils.getLinksInStatus(status); String text = html[0];/* w w w . ja v a2s . c o m*/ String media = html[1]; String otherUrl = html[2]; String hashtags = html[3]; String users = html[4]; if (media.contains("/tweet_video/")) { media = media.replace("tweet_video", "tweet_video_thumb").replace(".mp4", ".png"); } values.put(MentionsSQLiteHelper.COLUMN_ACCOUNT, account); values.put(MentionsSQLiteHelper.COLUMN_TEXT, text); values.put(MentionsSQLiteHelper.COLUMN_TWEET_ID, id); values.put(MentionsSQLiteHelper.COLUMN_NAME, status.getUser().getName()); values.put(MentionsSQLiteHelper.COLUMN_PRO_PIC, status.getUser().getOriginalProfileImageURL()); values.put(MentionsSQLiteHelper.COLUMN_SCREEN_NAME, status.getUser().getScreenName()); values.put(MentionsSQLiteHelper.COLUMN_TIME, time); values.put(MentionsSQLiteHelper.COLUMN_RETWEETER, originalName); values.put(MentionsSQLiteHelper.COLUMN_UNREAD, 1); values.put(MentionsSQLiteHelper.COLUMN_PIC_URL, media); values.put(MentionsSQLiteHelper.COLUMN_URL, otherUrl); values.put(MentionsSQLiteHelper.COLUMN_USERS, users); values.put(MentionsSQLiteHelper.COLUMN_HASHTAGS, hashtags); values.put(MentionsSQLiteHelper.COLUMN_ANIMATED_GIF, TweetLinkUtils.getGIFUrl(status, otherUrl)); values.put(HomeSQLiteHelper.COLUMN_CONVERSATION, status.getInReplyToStatusId() == -1 ? 0 : 1); valueses[i] = values; } return insertMultiple(valueses); }
From source file:com.daiv.android.twitter.utils.api_helper.TwitLongerHelper.java
License:Apache License
/** * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger * to include the posted status's id.// w w w . ja va 2 s . c om * * @return id of the status that was posted to twitter */ public long createPost() { TwitLongerStatus status = postToTwitLonger(); long statusId; try { Status postedStatus; StatusUpdate update = new StatusUpdate(status.getText()); if (replyToStatusId != 0) { update.setInReplyToStatusId(replyToStatusId); } if (location != null) { update.setLocation(location); } postedStatus = twitter.updateStatus(update); statusId = postedStatus.getId(); updateTwitlonger(status, statusId); } catch (Exception e) { e.printStackTrace(); statusId = 0; } // if zero, then it failed return statusId; }
From source file:com.daiv.android.twitter.utils.api_helper.TwitPicHelper.java
License:Apache License
/** * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger * to include the posted status's id./*ww w .j a v a2 s . c o m*/ * * @return id of the status that was posted to twitter */ public long createPost() { TwitPicStatus status = uploadToTwitPic(); Log.v("Test_twitpic", "past upload"); long statusId; try { Status postedStatus; StatusUpdate update = new StatusUpdate(status.getText()); if (replyToStatusId != 0) { update.setInReplyToStatusId(replyToStatusId); } if (location != null) { update.setLocation(location); } postedStatus = twitter.updateStatus(update); statusId = postedStatus.getId(); } catch (Exception e) { e.printStackTrace(); statusId = 0; } // if zero, then it failed return statusId; }
From source file:com.daiv.android.twitter.utils.api_helper.TwitterMultipleImageHelper.java
License:Apache License
public ArrayList<String> getImageURLs(Status status, Twitter twitter) { ArrayList<String> images = TweetLinkUtils.getAllExternalPictures(status); try {//from w ww.j a v a 2s . co m AccessToken token = twitter.getOAuthAccessToken(); String oauth_token = token.getToken(); String oauth_token_secret = token.getTokenSecret(); // generate authorization header String get_or_post = "GET"; String oauth_signature_method = "HMAC-SHA1"; String uuid_string = UUID.randomUUID().toString(); uuid_string = uuid_string.replaceAll("-", ""); String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here // get the timestamp Calendar tempcal = Calendar.getInstance(); long ts = tempcal.getTimeInMillis();// get current time in milliseconds String oauth_timestamp = (new Long(ts / 1000)).toString(); // then divide by 1000 to get seconds // the parameter string must be in alphabetical order, "text" parameter added at end String parameter_string = "oauth_consumer_key=" + AppSettings.TWITTER_CONSUMER_KEY + "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_token=" + encode(oauth_token) + "&oauth_version=1.0"; String twitter_endpoint = "https://api.twitter.com/1.1/statuses/show/" + status.getId() + ".json"; String twitter_endpoint_host = "api.twitter.com"; String twitter_endpoint_path = "/1.1/statuses/show/" + status.getId() + ".json"; String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string); String oauth_signature = computeSignature(signature_base_string, AppSettings.TWITTER_CONSUMER_SECRET + "&" + encode(oauth_token_secret)); String authorization_header_string = "OAuth oauth_consumer_key=\"" + AppSettings.TWITTER_CONSUMER_KEY + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp + "\",oauth_nonce=\"" + oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + encode(oauth_signature) + "\",oauth_token=\"" + encode(oauth_token) + "\""; HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUserAgent(params, "HttpCore/1.1"); HttpProtocolParams.setUseExpectContinue(params, false); HttpProcessor httpproc = new ImmutableHttpProcessor(// Required protocol interceptors new RequestContent(), new RequestTargetHost(), // Recommended protocol interceptors new RequestConnControl(), new RequestUserAgent(), new RequestExpectContinue()); HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); HttpContext context = new BasicHttpContext(null); HttpHost host = new HttpHost(twitter_endpoint_host, 443); DefaultHttpClientConnection conn = new DefaultHttpClientConnection(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, null, null); SSLSocketFactory ssf = sslcontext.getSocketFactory(); Socket socket = ssf.createSocket(); socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), 0); conn.bind(socket, params); BasicHttpEntityEnclosingRequest request2 = new BasicHttpEntityEnclosingRequest("GET", twitter_endpoint_path); request2.setParams(params); request2.addHeader("Authorization", authorization_header_string); httpexecutor.preProcess(request2, httpproc, context); HttpResponse response2 = httpexecutor.execute(request2, conn, context); response2.setParams(params); httpexecutor.postProcess(response2, httpproc, context); String responseBody = EntityUtils.toString(response2.getEntity()); conn.close(); JSONObject fullJson = new JSONObject(responseBody); JSONObject extendedEntities = fullJson.getJSONObject("extended_entities"); JSONArray media = extendedEntities.getJSONArray("media"); Log.v("Test_images", media.toString()); for (int i = 0; i < media.length(); i++) { JSONObject entity = media.getJSONObject(i); try { // parse through the objects and get the media_url String url = entity.getString("media_url"); String type = entity.getString("type"); // want to check to make sure it doesn't have it already // this also checks to confirm that the entity is in fact a photo if (!images.contains(url) && type.equals("photo")) { images.add(url); } } catch (Exception e) { } } } catch (Exception e) { e.printStackTrace(); } return images; }
From source file:com.daiv.android.twitter.utils.TweetLinkUtils.java
License:Apache License
public static String getGIFUrl(Status s, String otherUrls) { // this will be used after twitter begins to support them for (ExtendedMediaEntity e : s.getExtendedMediaEntities()) { if (e.getType().equals("animated_gif")) { return e.getMediaURL().replace("tweet_video_thumb", "tweet_video").replace(".png", ".mp4"); } else if (e.getType().equals("video")) { if (e.getVideoVariants().length > 0) { String url = ""; for (ExtendedMediaEntity.Variant v : e.getVideoVariants()) { if (v.getUrl().contains(".mp4")) { url = v.getUrl(); }/* w w w. j ava2s .co m*/ } Log.v("Test_video_link", url); return url; } } } // this is how the urls are currently stored String gifUrl = "twitter.com/" + s.getUser().getScreenName() + "/status/" + s.getId() + "/photo/1"; if (otherUrls.contains(gifUrl)) { return gifUrl; } // otherwise, lets just go with a blank string return ""; }
From source file:com.data.dataanalytics.twitter.TwitterFeed.java
public List<Tweet> getTweets(String search) { // We're curious how many tweets, in total, we've retrieved. Note that TWEETS_PER_QUERY is an upper limit, // but Twitter can and often will retrieve far fewer tweets twitter4j.Twitter twitter = getTwitter(); /* This variable is the key to our retrieving multiple blocks of tweets. In each batch of tweets we retrieve, we use this variable to remember the LOWEST tweet ID. Tweet IDs are (java) longs, and they are roughly sequential over time. Without setting the MaxId in the query, Twitter will always retrieve the most recent tweets. Thus, to retrieve a second (or third or ...) batch of Tweets, we need to set the Max Id in the query to be one less than the lowest Tweet ID we've seen already. This allows us to page backwards through time to retrieve additional blocks of tweets*/ long maxID = -1; try {//from w w w . j a va 2 s .co m // There are limits on how fast you can make API calls to Twitter, and if you have hit your limit // and continue to make calls Twitter will get annoyed with you. I've found that going past your // limits now and then doesn't seem to be problematic, but if you have a program that keeps banging // the API when you're not allowed you will eventually get shut down. // // Thus, the proper thing to do is always check your limits BEFORE making a call, and if you have // hit your limits sleeping until you are allowed to make calls again. // // Every time you call the Twitter API, it tells you how many calls you have left, so you don't have // to ask about the next call. But before the first call, we need to find out whether we're already // at our limit. // This returns all the various rate limits in effect for us with the Twitter API Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); // This finds the rate limit specifically for doing the search API call we use in this program RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); // Always nice to see these things when debugging code... System.out.printf("You have %d calls remaining out of %d, Limit resets in %d seconds\n", searchTweetsRateLimit.getRemaining(), searchTweetsRateLimit.getLimit(), searchTweetsRateLimit.getSecondsUntilReset()); // This is the loop that retrieve multiple blocks of tweets from Twitter for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { System.out.printf("\n\n!!! Starting loop %d\n\n", queryNumber); // Delay if (searchTweetsRateLimit.getRemaining() == 0) { System.out.printf("!!! Sleeping for %d seconds due to rate limits\n", searchTweetsRateLimit.getSecondsUntilReset()); // If you sleep exactly the number of seconds, you can make your query a bit too early // and still get an error for exceeding rate limitations // // Adding two seconds seems to do the trick. Sadly, even just adding one second still triggers a // rate limit exception more often than not. I have no idea why, and I know from a Comp Sci // standpoint this is really bad, but just add in 2 seconds and go about your business. Or else. Thread.sleep((searchTweetsRateLimit.getSecondsUntilReset() + 2) * 1000l); } Query q = new Query(search); // Search for tweets that contains this term q.setCount(TWEETS_PER_QUERY); // How many tweets, max, to retrieve //q.resultType("recent"); // Get all tweets q.setLang("en"); // English language tweets, please // If maxID is -1, then this is our first call and we do not want to tell Twitter what the maximum // tweet id is we want to retrieve. But if it is not -1, then it represents the lowest tweet ID // we've seen, so we want to start at it-1 (if we start at maxID, we would see the lowest tweet // a second time... if (maxID != -1) { q.setMaxId(maxID - 1); } // This actually does the search on Twitter and makes the call across the network QueryResult r = twitter.search(q); // If there are NO tweets in the result set, it is Twitter's way of telling us that there are no // more tweets to be retrieved. Remember that Twitter's search index only contains about a week's // worth of tweets, and uncommon search terms can run out of week before they run out of tweets if (r.getTweets().size() == 0) { break; // Nothing? We must be done } // loop through all the tweets and process them. Need to save as CSV file for database for (Status s : r.getTweets()) { // Loop through all the tweets... // Increment our count of tweets retrieved // Keep track of the lowest tweet ID. If you do not do this, you cannot retrieve multiple // blocks of tweets... if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } // Do something with the tweet.... ta.processTweets(s, new Date()); } // As part of what gets returned from Twitter when we make the search API call, we get an updated // status on rate limits. We save this now so at the top of the loop we can decide whether we need // to sleep or not before making the next call. searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { // Catch all -- you're going to read the stack trace and figure out what needs to be done to fix it System.out.println("That didn't work well...wonder why?"); e.printStackTrace(); } System.out.printf("\n\nA total of %d tweets retrieved\n", ta.getTotalTweets()); System.out.println("The total amount of tweets in an hour " + ta.getTweetsInAnHour()); ta.checkIfTrending(ta.getTweetsInAnHour(), ta.getTotalTweets()); return ta.getTweetList(); }
From source file:com.dhamacher.sentimentanalysis4tweets.common.LocalTweet.java
License:Apache License
public void copyFrom(Status tweet) { setId(tweet.getId()); setContent(tweet.getText());/*from ww w . j a v a 2s . c o m*/ setAuthor(tweet.getUser().getName()); setDate(tweet.getCreatedAt()); setAuthorId(tweet.getId()); fromDB = false; }