List of usage examples for twitter4j QueryResult getTweets
List<Status> getTweets();
From source file:com.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java
License:Apache License
public void getMore() { if (hasMore) { canRefresh = false;/*from w w w. jav a 2 s . c o m*/ new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); QueryResult result = twitter.search(query); for (twitter4j.Status status : result.getTweets()) { statuses.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); canRefresh = true; } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { canRefresh = false; } }); } } }).start(); } }
From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfileMentionsFragment.java
License:Apache License
public void doSearch() { spinner.setVisibility(View.VISIBLE); new Thread(new Runnable() { @Override// w ww . java 2 s. c om public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); query = new Query("@" + screenName + " -RT"); query.sinceId(1); QueryResult result = twitter.search(query); tweets.clear(); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter = new TimelineArrayAdapter(context, tweets); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); canRefresh = true; if (!hasMore) { View footer = inflater.inflate(R.layout.mentions_footer, null); listView.addFooterView(footer); listView.setFooterDividersEnabled(false); } } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); canRefresh = false; if (!hasMore) { View footer = inflater.inflate(R.layout.mentions_footer, null); listView.addFooterView(footer); listView.setFooterDividersEnabled(false); } } }); } catch (OutOfMemoryError e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); canRefresh = false; if (!hasMore) { View footer = inflater.inflate(R.layout.mentions_footer, null); listView.addFooterView(footer); listView.setFooterDividersEnabled(false); } } }); } } }).start(); }
From source file:com.daiv.android.twitter.ui.profile_viewer.fragments.sub_fragments.ProfileMentionsFragment.java
License:Apache License
public void getMore() { canRefresh = false;//from w w w .ja v a 2 s .c o m new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); QueryResult result = twitter.search(query); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); canRefresh = true; if (!hasMore) { View footer = inflater.inflate(R.layout.mentions_footer, null); listView.addFooterView(footer); listView.setFooterDividersEnabled(false); } } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { canRefresh = false; hasMore = false; } }); } } }).start(); }
From source file:com.daiv.android.twitter.ui.search.TwitterSearchFragment.java
License:Apache License
public void onRefreshStarted() { new Thread(new Runnable() { @Override/*from ww w. j a v a 2 s. c o m*/ public void run() { final long topId; if (tweets.size() > 0) { topId = tweets.get(0).getId(); } else { topId = 0; } try { Twitter twitter = Utils.getTwitter(context, settings); query = new Query(searchQuery); if (topTweets) { query.setResultType(Query.POPULAR); } else { query.setResultType(null); } QueryResult result = twitter.search(query); tweets.clear(); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { int top = 0; for (int i = 0; i < tweets.size(); i++) { if (tweets.get(i).getId() == topId) { top = i; break; } } adapter = new TimelineArrayAdapter(context, tweets, onlyStatus); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); listView.setSelection(top); spinner.setVisibility(View.GONE); mPullToRefreshLayout.setRefreshing(false); } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); mPullToRefreshLayout.setRefreshing(false); } }); } } }).start(); }
From source file:com.daiv.android.twitter.ui.search.TwitterSearchFragment.java
License:Apache License
public void doSearch(final String mQuery) { spinner.setVisibility(View.VISIBLE); if (listView.getVisibility() != View.GONE) { listView.setVisibility(View.GONE); }//from w w w.jav a 2 s .c o m new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); Log.v("Test_searching", "query in frag: " + mQuery); query = new Query(mQuery); if (topTweets) { query.setResultType(Query.ResultType.popular); } else { query.setResultType(null); } QueryResult result = twitter.search(query); tweets.clear(); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter = new TimelineArrayAdapter(context, tweets, onlyStatus); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); spinner.setVisibility(View.GONE); } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); } }); } catch (OutOfMemoryError e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { spinner.setVisibility(View.GONE); } }); } } }).start(); }
From source file:com.daiv.android.twitter.ui.search.TwitterSearchFragment.java
License:Apache License
public void getMore() { if (hasMore) { canRefresh = false;// w ww .j a v a 2 s. c om mPullToRefreshLayout.setRefreshing(true); new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, settings); QueryResult result = twitter.search(query); for (twitter4j.Status status : result.getTweets()) { tweets.add(status); } if (result.hasNext()) { query = result.nextQuery(); hasMore = true; } else { hasMore = false; } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); mPullToRefreshLayout.setRefreshing(false); canRefresh = true; } }); } catch (Exception e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { mPullToRefreshLayout.setRefreshing(false); canRefresh = true; } }); } } }).start(); } }
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 {//w w w. j a v a 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.twitterapi.TweetOperator.java
License:Apache License
protected LinkedList<LocalTweet> getTweetsWithAPI(String queryString) throws TwitterException, IOException { Query query = new Query(queryString); long sinceId = db.getIdOfLatestTweetForThisEntity(queryString); System.out.println("Get ids since " + 0); QueryResult result = twitter.search(query); LinkedList<LocalTweet> results = new LinkedList<LocalTweet>(); for (Status tweet : result.getTweets()) { LocalTweet lt = new LocalTweet(); lt.copyFrom(tweet);/*from w ww . ja v a2 s. c o m*/ results.add(lt); } return results; }
From source file:com.dhamacher.tweetsentimentanalysis.Main.java
License:Open Source License
public static void main(String args[]) { try {/*from www . j av a 2s. c om*/ license(); /* Prompt for Search token */ Scanner in = new Scanner(System.in); System.out.print("Type in the search query: "); Query searchToken = new Query(in.nextLine()); /* Set maximum count */ searchToken.setCount(1500); /* Send search request to Twitter and receive List<> of results */ QueryResult result = twitter.search(searchToken); /* Notify user of the result */ System.out.println( "Amount of tweets pulled for \"" + searchToken.getQuery() + "\": " + result.getTweets().size()); persistTweet(result, searchToken.getQuery()); end(); } catch (TwitterException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.dhamacher.tweetsentimentanalysis.Main.java
License:Open Source License
private static void persistTweet(QueryResult result, String token) { EntityTransaction tx = em.getTransaction(); tx.begin();//ww w .ja v a 2 s . c o m for (Status status : result.getTweets()) { Tweet entity = new Tweet(); entity.setTweetId(status.getId()); entity.setSearchToken(token); entity.setCreatedOn(status.getCreatedAt()); entity.setIsRetweet(status.isRetweet()); entity.setSource(status.getSource()); entity.setUser(status.getUser()); entity.setText(status.getText()); em.persist(entity); } tx.commit(); }