public Collection<Tweet> requestTweetsByUser(String otherUser) { List<Tweet> tweets = new ArrayList<Tweet>(); try { // The factory instance is re-useable and thread safe. Twitter twitter = new TwitterFactory().getInstance(); AccessToken accessToken = loadAccessToken(1); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(accessToken); System.out.println("Fetching latest 100 tweets for [" + otherUser + "]"); // First param of Paging() is the page number, second is the number per page (this is capped // around 200 I think. Paging paging = new Paging(1, 100); List<Status> statuses = twitter.getUserTimeline(otherUser, paging); for (Status status : statuses) { tweets.add( new Tweet( otherUser, status.getText(), (status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() + "," + status.getGeoLocation().getLongitude() : ""))); System.out.println( status.getUser().getName() + "(" + status.getGeoLocation() + "):" + status.getText()); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return tweets; }
public List<Status> getMyTweets(Twitter twitter) { try { Paging paging = new Paging(1, 20); return twitter.getUserTimeline(paging); } catch (TwitterException e) { return null; } }
private void getTweetsFromTwitter() { try { ConfigurationBuilder confbuilder = new ConfigurationBuilder(); confbuilder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); confbuilder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); // Access Token String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, ""); // Access Token Secret String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, ""); AccessToken accessToken = new AccessToken(access_token, access_token_secret); btnLogoutTwitter.setVisibility(View.GONE); btnGetTweets.setVisibility(View.GONE); lblUserName.setVisibility(View.GONE); btnShowOnMap.setVisibility(View.GONE); btnUsers.setVisibility(View.GONE); Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance(accessToken); List<Status> statuses = twitter.getUserTimeline(USER_ID, new Paging(1, 10)); IDs following = twitter.getFriendsIDs(USER_ID); long[] followingList = following.getIDs(); for (int i = 0; i < followingList.length; i++) { Toast.makeText(getApplicationContext(), twitter.showUser(followingList[i]).toString(), 100) .show(); } int count = 0; // Collections.reverse(statuses); // reverse order ArrayList<String> arrayList = new ArrayList<String>(); for (Status status : statuses) { String tweettext = status.getUser().getName() + ":" + status.getText() + "\n (" + status.getCreatedAt().toLocaleString() + ")" + status.getGeoLocation(); arrayList.add(tweettext); // now single tweet is in tweettext } ListView lv = (ListView) findViewById(R.id.storeTweet); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); lv.setAdapter(arrayAdapter); } catch (Exception ex) { // failed to fetch tweet } }
public List<Status> getTweets(Twitter twitter) throws TwitterException { Integer i = 1; Integer cont = 1; System.out.println("Showing home timeline."); List<Status> statuses; List<Status> listaDeTweets = new ArrayList<Status>(); statuses = twitter.getUserTimeline(729614881, new Paging(i, 200)); while (!statuses.isEmpty()) { // The factory instance is re-useable and thread safe. // 729614881 for (Status status : statuses) { listaDeTweets.add(status); System.out.println(cont + ": " + status.getUser().getName() + ":" + status.getText()); cont++; } i++; statuses = twitter.getUserTimeline(729614881, new Paging(i, 200)); } return listaDeTweets; }
public static void main(String[] args) throws TwitterException { // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); Query query = new Query("ionic2"); QueryResult result = twitter.search(query); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); } List<Status> statuses = twitter.getUserTimeline("Ionicframework"); System.out.println("Showing Ionicframework timeline."); for (Status status : statuses) { System.out.println(status.getUser().getName() + ":" + status.getText()); } }
/** * Delete normal tweets sent by the users that match the delta requirements. (ie. delete tweets * older then 2 weeks. Default ) */ public void deleteTweets() { try { boolean found = true; int start = 1; int increment = twitterConfig.getIncrementCount(); while (found) { found = false; List<Status> status = twitter.getUserTimeline(new Paging(start, start + increment)); for (Status s : status) { if (earlier.getTime() > s.getCreatedAt().getTime()) { found = true; twitter.destroyStatus(s.getId()); logger.debug("removing tweeit with id: {}", s.getId()); } } logger.debug("size of status batch is: {} ", status.size()); } } catch (TwitterException e) { logger.error("Twitter exception occurred", e); } }
private static ArrayList<String> ProcessTimeLine(String user) throws InterruptedException, TwitterException { ArrayList<String> Tweets = new ArrayList<String>(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) .setOAuthConsumerKey(KEY) .setOAuthConsumerSecret(SECRET) .setOAuthAccessToken(ACCESSTOKEN) .setOAuthAccessTokenSecret(ACCESSSECRET); cb.setJSONStoreEnabled(true); // gets Twitter instance with default credentials boolean bWait = true; Twitter twitter = new TwitterFactory(cb.build()).getInstance(); do { try { Map<String, RateLimitStatus> oRT = twitter.getRateLimitStatus(); RateLimitStatus rateLimit = oRT.get("/statuses/user_timeline"); int remaining = rateLimit.getRemaining(); System.out.print("(Remaining API calls: " + remaining + ")"); int remainingTime = rateLimit.getSecondsUntilReset(); if (remaining <= NUM_TWEETS / 200 + 1) { System.out.println("Waiting " + remainingTime + " seconds"); Thread.sleep(remainingTime * 1000); } else bWait = false; } catch (Exception te) { if (te.toString().toLowerCase().contains("rate limit") && !te.toString().toLowerCase().contains("bad authentication data")) { System.out.println("Waiting 60s"); Thread.sleep(60 * 1000); } else { bWait = false; } } } while (bWait); try { Detector detector = DetectorFactory.create(); List<Status> statuses; int iPage = 1; int iTweets = 0; do { int iPageSize = 0; if (iTweets + 200 < NUM_TWEETS) { iPageSize = 200; } else { iPageSize = NUM_TWEETS - iTweets; } statuses = twitter.getUserTimeline(user, new Paging(iPage, iPageSize)); for (Status status : statuses) { String sStatusId = "-1"; try { if ((status.getRetweetedStatus() != null) && (status.getRetweetedStatus().getUser() != null)) { continue; } try { detector.append(Simplify(status.getText())); if (detector.detect().equalsIgnoreCase("es")) { String sStatusJSON = DataObjectFactory.getRawJSON(status); Tweets.add(sStatusJSON); } } catch (Exception exl) { } } catch (Exception ex) { System.out.println("ERROR in status id " + sStatusId); } iTweets++; } iPage++; } while (statuses.size() > 0 && iTweets < NUM_TWEETS); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); } catch (Exception ex) { } System.out.println("..." + Tweets.size() + " tweets."); return Tweets; }
public void getTweets() { // tweetText.setText(""); String queryText = keyword.getText().toString(); if (queryText.matches("")) { Toast.makeText(this, "Please enter something", Toast.LENGTH_SHORT).show(); return; } // System.out.println("Keyword =="+ queryText); if (isConnected()) { String oauthAccessToken = mSharedPreferences.getString(Const.PREF_KEY_TOKEN, ""); String oAuthAccessTokenSecret = mSharedPreferences.getString(Const.PREF_KEY_SECRET, ""); ConfigurationBuilder confbuilder = new ConfigurationBuilder(); Configuration conf = confbuilder .setOAuthConsumerKey(Const.CONSUMER_KEY) .setOAuthConsumerSecret(Const.CONSUMER_SECRET) .setOAuthAccessToken(oauthAccessToken) .setOAuthAccessTokenSecret(oAuthAccessTokenSecret) .build(); TwitterFactory tf = new TwitterFactory(conf); Twitter twitter = tf.getInstance(); try { // Query query = new Query(queryText); int tweetCount = 50; User user = twitter.showUser(queryText.trim()); long userId = user.getId(); List<Status> tweets = twitter.getUserTimeline(userId, new Paging(1, tweetCount)); // QueryResult result; // result = twitter.search(query); // List<Status> tweets = result.getTweets(); String[] resultStringSet; if (tweets.size() == 0) { resultStringSet = new String[1]; resultStringSet[0] = "No tweet found for this keyword. Please try some other keyword"; } else { resultStringSet = new String[tweets.size()]; } int i = 0; for (Status tweet : tweets) { final String text = "@" + tweet.getUser().getScreenName() + " - " + tweet.getText(); // System.out.println(tweet); resultStringSet[i++] = text; } final ArrayAdapter<String> resultAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultStringSet); list = (ListView) findViewById(R.id.resultList); list.post( new Runnable() { @Override public void run() { list.setAdapter(resultAdapter); // tweetText.append(text); // tweetText.append("\n"); // scrollView.fullScroll(View.FOCUS_DOWN); } }); } catch (TwitterException te) { Toast.makeText( this, "Please enter valid screen name of the twitter user", Toast.LENGTH_LONG) .show(); te.printStackTrace(); System.out.println("Failed to search tweets: " + te.getMessage()); return; // System.exit(-1); } buttonLogin.setText(R.string.label_disconnect); getTweetButton.setEnabled(true); } else { buttonLogin.setText(R.string.label_connect); } }