List of usage examples for twitter4j QueryResult getTweets
List<Status> getTweets();
From source file:cd.examentwitter.Metodos.java
/** * Metodo para buscar una determinada secuencia caracteres por todo Twitter *///from w ww . j av a 2s . c o m public void search() { try { String search = JOptionPane.showInputDialog("Qu secuencia de caracteres desea buscar?"); QueryResult result = twitter.search(new Query(search)); for (Status status : result.getTweets()) { System.out.println("@" + status.getUser().getScreenName() + ": " + status.getText()); } } catch (TwitterException ex) { Logger.getLogger(Metodos.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ch.schrimpf.core.TwitterCrawler.java
License:Open Source License
/** * Performs a single crawl step according to the previous initialized query * until the specified limit is reached. Received tweets are stored in the * *.csv specified in the easyTwitterCrawler.properties file. * <p/>/*from ww w . j a v a 2 s.co m*/ * TODO make selecting values flexible * * @param limit to stop on */ public void crwal(int limit) { LOG.info("receiving tweets..."); int i = 0; while (i < limit && running) { try { QueryResult res = twitter.search(query); if (res.getMaxId() > last) { for (Status status : res.getTweets()) { String[] line = { String.valueOf(status.getId()), String.valueOf(status.getCreatedAt()), status.getText(), String.valueOf(status.getUser()), String.valueOf(status.getPlace()), status.getLang() }; csv.writeResult(Arrays.asList(line)); i++; } last = res.getMaxId(); } else { break; } } catch (TwitterException e) { LOG.warning("could not process tweets"); } } tweets += i; LOG.info(i + " tweets received in this crawl"); LOG.info("totally " + tweets + " received"); }
From source file:Classes.TwitterPull.java
public void retrieveTweets() throws TwitterException { Query query = new Query("\"" + this.queryString + "\""); query.setLang("en"); query.setCount(100);/*from w ww .j a v a 2 s . c o m*/ QueryResult result; int i = 0; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { String t = tweet.getText().replaceAll("\n", "").replaceAll("\r", ""); // appendTweetDocument(t); retrievedTweets.add(t); } i++; } while ((query = result.nextQuery()) != null && i < 50); }
From source file:co.uk.socialticker.ticker.TickerActivity.java
License:Open Source License
/** * Test code to try and retrieve some data from twitter in a search! * @throws TwitterException //from w w w . ja v a2s. co m * */ public JSONArray doSearch(View v) throws TwitterException { if (mApiClient != null || debugOn) { // The factory instance is re-useable and thread safe. //get the hashtag - check to make sure if returned value is set to something with a length JSONArray jsA = new JSONArray(); String qHash = p.getString(KEY_CAST_HASHTAG, ""); Log.d(TAG, "Hash to search: " + qHash); if (qHash.length() == 0) { Toast.makeText(this, "The hashtag looks like it is not setup. May want to fix that", Toast.LENGTH_LONG).show(); } else { try { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); builder.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); Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken); //Query query = new Query("#MOTD2014"); Query query = new Query(qHash); query.count(TWEET_COUNT); QueryResult result = twitter.search(query); for (twitter4j.Status status : result.getTweets()) { MediaEntity[] me = status.getMediaEntities(); String meUrl = ""; if (me.length > 0) { Log.d(TAG, "me[0] : " + me[0].getMediaURL()); //meUrl = me[0].getDisplayURL(); //sjort URl = useless. meUrl = me[0].getMediaURL(); } JSONObject jso = tweetJSON(status.getUser().getScreenName(), status.getUser().getName() // , status.getUser().getOriginalProfileImageURL() //Whatever the size was it was uploaded in // , status.getUser().getProfileImageURL() // 48x48 , status.getUser().getBiggerProfileImageURL() // 73x73 , status.getText(), status.getCreatedAt().toString(), status.getFavoriteCount(), status.getRetweetCount(), meUrl); jsA.put(jso); } } catch (TwitterException e) { // Error in updating status Log.d("Twitter Search Error", e.getMessage()); Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } ; return jsA; } else { Toast.makeText(this, "You do not seem to be connected to a cast device...", Toast.LENGTH_LONG).show(); return null; } }
From source file:Collector.TweetCollector.java
public static List<Status> getTweets(final String q) { Timer timer = new Timer(); TimerTask hourlyTask = new TimerTask() { @Override//from ww w. j av a2s. 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 List<Status> printTimeLine(String account) { try {// w w w. j a v a2 s.co m Query query = new Query(account); QueryResult result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText()); } return tweets; } catch (TwitterException te) { System.out.println("Failed to search tweets: " + te.getMessage()); System.exit(-1); } return null; }
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 .j a va 2 s . co 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:com.concursive.connect.web.modules.profile.jobs.TwitterQueryJob.java
License:Open Source License
public void execute(JobExecutionContext context) throws JobExecutionException { long startTime = System.currentTimeMillis(); LOG.debug("Starting job..."); SchedulerContext schedulerContext = null; Connection db = null;//from w w w . j av a 2s .c om try { schedulerContext = context.getScheduler().getContext(); // Determine if the twitter hash is enabled ApplicationPrefs prefs = (ApplicationPrefs) schedulerContext.get("ApplicationPrefs"); String twitterHash = prefs.get(ApplicationPrefs.TWITTER_HASH); if (!StringUtils.hasText(twitterHash)) { LOG.debug("Hash is not defined exiting from Twitter query job..."); return; } db = SchedulerUtils.getConnection(schedulerContext); // Determine the previous retrieved twitter id to use for query Process process = new Process(db, "TwitterQueryJob"); long sinceId = process.getLongValue(); LOG.debug("Last saved twitter id is : " + sinceId); // Create Query Object for searching twitter Query query = new Query("#" + twitterHash); query.setRpp(99); if (sinceId > 0) { // Set since_id in the query query.setSinceId(sinceId); } // Get the Twitter search results Twitter twitter = new Twitter(TWITTER_BASE_URL); QueryResult result = twitter.search(query); LOG.debug("Found and retrieved " + result.getTweets().size() + " tweet(s)."); // Iterate through the tweets and store in project history int count = 0; for (Tweet tweet : result.getTweets()) { count++; LOG.debug("Got tweet from " + tweet.getFromUser() + " as " + tweet.getText()); // See if this matches any profiles in the system // @note it's possible that more than one project can have the same twitter id ProjectList projectList = new ProjectList(); projectList.setTwitterId(tweet.getFromUser()); projectList.setApprovedOnly(true); projectList.buildList(db); // Clean up the tweet output String message = tweet.getText(); // Turn links into wiki links message = WikiUtils.addWikiLinks(message); // Remove the hash tag - beginning or middle message = StringUtils.replace(message, "#" + twitterHash + " ", ""); // Remove the hash tag - middle or end message = StringUtils.replace(message, " #" + twitterHash, ""); // Remove the hash tag - untokenized message = StringUtils.replace(message, "#" + twitterHash, ""); // Update the activity stream for the matching profiles for (Project project : projectList) { ProjectHistory projectHistory = new ProjectHistory(); projectHistory.setProjectId(project.getId()); projectHistory.setEnabled(true); // If there is a user profile, use the user's id, else use the businesses id? or use a different event if (project.getProfile()) { projectHistory.setEnteredBy(project.getOwner()); } else { projectHistory.setEnteredBy(project.getOwner()); } projectHistory.setLinkStartDate(new Timestamp(System.currentTimeMillis())); String desc = WikiLink.generateLink(project) + " [[http://twitter.com/" + tweet.getFromUser() + "/statuses/" + tweet.getId() + " tweeted]] " + message; projectHistory.setDescription(desc); projectHistory.setLinkItemId(project.getId()); projectHistory.setLinkObject(ProjectHistoryList.TWITTER_OBJECT); projectHistory.setEventType(ProjectHistoryList.TWITTER_EVENT); // Store the tweets in project history projectHistory.insert(db); } // Set the tweet id as since_Id if (sinceId < tweet.getId()) { sinceId = tweet.getId(); } } //update the recent sinceId and process timestamp process.setLongValue(sinceId); process.setProcessed(new Timestamp(new java.util.Date().getTime())); process.update(db); long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; LOG.debug("Finished: " + count + " took " + totalTime + " ms"); } catch (Exception e) { LOG.error("TwitterQueryJob Exception", e); throw new JobExecutionException(e.getMessage()); } finally { SchedulerUtils.freeConnection(schedulerContext, db); } }
From source file:com.daemon.Minion.java
License:Open Source License
/** * Fetches tweets for the given search terms. Each term is fetched only once. * @param searchTermMetaData The search terms for which tweets should be fetched. * @return Returns a list of all fetched tweets. * @throws TwitterException Thrown if there is a problem with the Twitter service. */// w w w . j a v a2 s . c o m public Map<SearchTerm, List<Status>> fetchTweetsForSearchTerms(List<SearchTermMetaData> searchTermMetaData) throws TwitterException { Map<SearchTerm, List<Status>> allTweetsMap = new HashMap<SearchTerm, List<Status>>(); // Initialize map for (SearchTermMetaData searchTerm : searchTermMetaData) { allTweetsMap.put(searchTerm.getSearchTerm(), new LinkedList<Status>()); } // Iterate over every search term and do some cool stuff for (SearchTermMetaData metaData : searchTermMetaData) { //System.out.println(prependInfo("MY CURRENT USED RATE LIMIT: " + _twitterProfile.getUsedRateLimit())); //System.out.println(prependInfo("MY MAX USED RATE LIMIT: " + _props.maxRateLimit)); // If the rate limit has reached 0, we stop if (_twitterProfile.getUsedRateLimit() >= _props.maxRateLimit) return allTweetsMap; // If we have already fetched enough times for this search times, we do not want // to fetch any more if (metaData.getFetchedCount() == _limitPerSearchTerm) { metaData.setFiltered(true); } // Ignore search terms that have been filtered if (metaData.isFiltered()) continue; SearchTerm term = metaData.getSearchTerm(); // Update counting stuff. // We do this before the actual search because the search can throw an // exception, but we have to count nonetheless. _twitterProfile.setUsedRateLimit(_twitterProfile.getUsedRateLimit() + 1); _numRequests++; metaData.setFetchedCount(metaData.getFetchedCount() + 1); // search for tweets QueryResult result = search(term); List<Status> tweets = result.getTweets(); if (tweets.size() > 1) { if (term.getOldStart() != null) { if (tweets.get(tweets.size() - 1).getCreatedAt().getTime() < term.getOldStart().getMillis()) { // If we enter this case, we are in state 4, but the end constraint was met, because // the last fetched tweet date was older than the old start. So we alter the last fetched // tweet id to NULL and the old start becomes the current start, so we move from state 4 // to state 3. // Update the number of new tweets for the search term by counting only // the new tweets int count = countNewTweets(term, tweets); metaData.setTweetCount(metaData.getTweetCount() + count); term.setOldStart(term.getCurrentStart()); term.setLastFetchedTweetId(null); // This term is now finished (for the time being), so we remove it from the list of // search terms we still want to process. metaData.setFiltered(true); // If this is the first call for the search term, save the date for the very first tweet if (count > 0 && metaData.getNewestTweetDate() == null) metaData.setNewestTweetDate(new DateTime(tweets.get(0).getCreatedAt().getTime())); if (count > 0) { // Also save the date of the last acceptable tweet (count - 1, because count starts at 1 [= index 0]) metaData.setOldestTweetDate( new DateTime(tweets.get(count - 1).getCreatedAt().getTime())); } // We do not have save each tweet individually but can save them in one big transaction // (see below), because the transactor will handle duplicate entries. } else { // If we enter this case, we are in state 4 and the end constraint was not met. So we save // all tweets in a transaction (see below) and stay in state 4. term.setLastFetchedTweetId(tweets.get(tweets.size() - 1).getId()); // Update the number of new tweets for the search term metaData.setTweetCount(metaData.getTweetCount() + tweets.size()); // If this is the first call for the search term, save the date for the very first tweet if (metaData.getNewestTweetDate() == null) { metaData.setNewestTweetDate(new DateTime(tweets.get(0).getCreatedAt().getTime())); } // Update old tweet date int count = countNewTweets(term, tweets); if (count > 0) { // Also save the date of the last acceptable tweet (count - 1, because count starts at 1 [= index 0]) metaData.setOldestTweetDate( new DateTime(tweets.get(count - 1).getCreatedAt().getTime())); } } } else { // If we enter this case, we are in state 1 or 2 and have found at least 2 tweets. // So we have not to take care of anything special, so we enter state 2 next. term.setLastFetchedTweetId(tweets.get(tweets.size() - 1).getId()); // Update the number of new tweets for the search term metaData.setTweetCount(metaData.getTweetCount() + tweets.size()); // If this is the first call for the search term, save the date for the very first tweet if (metaData.getNewestTweetDate() == null) { metaData.setNewestTweetDate(new DateTime(tweets.get(0).getCreatedAt().getTime())); } // Update old tweet date // Also save the date of the last acceptable tweet (count - 1, because count starts at 1 [= index 0]) metaData.setOldestTweetDate( new DateTime(tweets.get(tweets.size() - 1).getCreatedAt().getTime())); } // We save all tweets in a transaction allTweetsMap.get(term).addAll(tweets); } else { // If we enter this case, we are in state 1, 2 (or very unlikely 4) and have not found any // new tweets since the last search. So we have to update the old start date and set the // last fetched tweet id to NULL, so that we enter state 3. term.setOldStart(term.getCurrentStart()); term.setLastFetchedTweetId(null); // This term is now finished (for now), so we remove it from the list of // search terms we still want to process. metaData.setFiltered(true); } term.setTimeLastFetched(new DateTime()); } return allTweetsMap; }
From source file:com.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java
License:Apache License
public void getTweets() { canRefresh = false;/*from ww w. j av a2s. c o m*/ new Thread(new Runnable() { @Override public void run() { try { Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings); boolean manualLoc = sharedPrefs.getBoolean("manually_config_location", false); int i = 0; while (!connected && i < 5 && !manualLoc) { try { Thread.sleep(1500); } catch (Exception e) { } i++; } double latitude = -1; double longitude = -1; if (manualLoc) { // need to query yahoos api for the location... double[] loc = getLocationFromYahoo(sharedPrefs.getInt("woeid", 2379574)); latitude = loc[0]; longitude = loc[1]; } else { // set it from the location client Location location = mLastLocation; latitude = location.getLatitude(); longitude = location.getLongitude(); } query = new Query(); query.setGeoCode(new GeoLocation(latitude, longitude), 10, Query.MILES); QueryResult result = twitter.search(query); if (result.hasNext()) { hasMore = true; query = result.nextQuery(); } else { hasMore = false; } for (Status s : result.getTweets()) { statuses.add(s); } ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { adapter = new TimelineArrayAdapter(context, statuses); listView.setAdapter(adapter); listView.setVisibility(View.VISIBLE); LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress); spinner.setVisibility(View.GONE); } }); } catch (Throwable e) { e.printStackTrace(); ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { try { Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show(); } catch (IllegalStateException e) { // not attached to activity } } }); } canRefresh = true; } }).start(); }