List of usage examples for twitter4j Twitter search
QueryResult search(Query query) throws TwitterException;
From source file:Beans.Crawler.java
public String teste() { String result = ""; int totalTweets = 0; long maxID = -1; GeoLocation geo = new GeoLocation(Double.parseDouble("-19.9225"), Double.parseDouble("-43.9450")); result += geo.getLatitude() + " " + geo.getLongitude() + "<br><br>"; Twitter twitter = getTwitter(); try {//from w w w. j av a2 s. c om Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); result += "You have " + searchTweetsRateLimit.getRemaining() + " calls remaining out of " + searchTweetsRateLimit.getLimit() + ", Limit resets in " + searchTweetsRateLimit.getSecondsUntilReset() + " seconds<br/><br/>"; for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { // Do we need to delay because we've already hit our rate limits? if (searchTweetsRateLimit.getRemaining() == 0) { // Yes we do, unfortunately ... // 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_TERM); //.geoCode((geo), 100, "mi"); // Search for tweets that contains this term q.setCount(TWEETS_PER_QUERY); // How many tweets, max, to retrieve // q.setGeoCode(geo, 100, Query.Unit.mi); // q.setSince("2012-02-20"); // // 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. In this sample program, we just print them // out, but in a real application you might save them to a database, a CSV file, do some // analysis on them, whatever... for (Status s : r.getTweets()) // Loop through all the tweets... { // Increment our count of tweets retrieved totalTweets++; // 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.... result += "ID: " + s.getId() + " Data " + s.getCreatedAt().toString() + " user " + s.getUser().getScreenName() + " texto: " + cleanText(s.getText()) + " <br/>"; } // 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 } result += "<br><br>" + totalTweets + "<br>"; return result; }
From source file:Beans.Crawler.java
public List search(Categoria categoria, Localizacao localizacao) throws Exception { long maxID = -1; List<Tweet> list = new ArrayList<Tweet>(); Twitter twitter = getTwitter(); try {// w w w . j a v a 2s.c o m Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { if (searchTweetsRateLimit.getRemaining() == 0) { } String works = localizacao.getPalavrasChaves(); String[] arrWorks = works.split(","); String keywork = "", or = "OR"; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } System.out.println("exclude:retweets " + categoria.getDescricao() + keywork); Query q = new Query("exclude:retweets " + categoria.getDescricao() + keywork); q.setCount(TWEETS_PER_QUERY); if (maxID != -1) { q.setMaxId(maxID - 1); } QueryResult r = twitter.search(q); if (r.getTweets().size() == 0) { break; } for (Status s : r.getTweets()) { if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } Tweet t = new Tweet(); t.setUsuario(s.getUser().getId()); String dataPostagem = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(s.getCreatedAt()); t.setDataPostagem(dataPostagem); t.setTweet(s.getText()); t.setTweetId(s.getId()); t.setCategoriaId(categoria.getId()); t.setLocalizacaoId(localizacao.getId()); list.add(t); } searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { throw e; } return list; }
From source file:br.com.controller.TweetController.java
public String search() { Categoria c = new CategoriaDAO().find(Integer.parseInt(categoria)); Localizacao l = new LocalizacaoDAO().find(Integer.parseInt(localizacao)); long maxID = -1; Twitter twitter = getTwitter(); try {//from w w w.java2 s . co m Map<String, RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus("search"); RateLimitStatus searchTweetsRateLimit = rateLimitStatus.get("/search/tweets"); for (int queryNumber = 0; queryNumber < MAX_QUERIES; queryNumber++) { if (searchTweetsRateLimit.getRemaining() == 0) { } String works = c.getPalavrasChaves(); String[] arrWorks = works.split(","); String keywork = "", or = "OR", query = ""; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } query = "exclude:retweets " + keywork; works = l.getPalavrasChaves(); arrWorks = works.split(","); keywork = ""; or = "OR"; for (int i = 0; i < arrWorks.length; i++) { if ((i + 1) >= arrWorks.length) { or = ""; } keywork += " \"" + arrWorks[i] + "\" " + or; } query += keywork; Query q = new Query(query); q.setCount(TWEETS_PER_QUERY); if (maxID != -1) { q.setMaxId(maxID - 1); } QueryResult r = twitter.search(q); if (r.getTweets().size() == 0) { break; } for (Status s : r.getTweets()) { if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } Tweet t = new Tweet(); t.setUsuario(s.getUser().getId()); t.setDataPostagem(s.getCreatedAt()); t.setTweet(s.getText()); if (!new DAO.TweetDAO().hastTweet(s.getId())) { t.setTweetId(s.getId()); t.setCategoria(c); t.setLocalizacao(l); jpa.saveOrUpdate(t); } } searchTweetsRateLimit = r.getRateLimitStatus(); } } catch (Exception e) { } return "index"; }
From source file:br.com.porcelli.hornetq.integration.twitter.stream.reclaimer.AbstractBaseReclaimLostTweets.java
License:Apache License
protected void loadQuery(final String query, final Long lastTweetId, final Twitter twitter) throws Exception { try {//from ww w . j a v a 2 s.c o m if (lastTweetId == null) { return; } int page = 1; while (true) { final Query qry = new Query(query).sinceId(lastTweetId).page(page); final QueryResult qr = twitter.search(qry); if (qr.getTweets().size() == 0) { break; } for (final Tweet activeTweet : qr.getTweets()) { message.postMessage(activeTweet, true); } page++; } } catch (Exception e) { exceptionNotifier.notifyException(e); } }
From source file:br.unisal.twitter.bean.TwitterBean.java
public void consultarTweets() { TwitterFactory TwitterFactory = new TwitterFactory(); Twitter twitter = TwitterFactory.getSingleton(); List<Status> tweets = new ArrayList<>(); Query query = new Query(getTwittQuery()); try {//from w w w . j a v a 2 s. c o m QueryResult result = twitter.search(query); tweets.addAll(result.getTweets()); StringBuilder builder = new StringBuilder(); double lon = 0; double lat = 0; for (Status s : tweets) { if ((s.getGeoLocation() != null)) { lon = s.getGeoLocation().getLongitude(); lat = s.getGeoLocation().getLatitude(); } Tweet t = new Tweet(s.getUser().getName(), s.getUser().getLocation(), s.getText(), s.getCreatedAt(), lat, lon); dao.insert(t); builder.append(t.toString()); } this.getUiResultQuery().setSubmittedValue(builder.toString()); } catch (TwitterException te) { System.out.println("Failed to search tweets: " + te.getMessage()); } }
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 //w ww .ja v a 2 s. c o 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: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;//ww w. j a v a2s . 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.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java
License:Apache License
public void getTweets() { canRefresh = false;//from ww w . j a v 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(); }
From source file:com.daiv.android.twitter.ui.drawer_activities.discover.NearbyTweets.java
License:Apache License
public void getMore() { if (hasMore) { canRefresh = false;// w w w .jav a 2s . 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/*from www.j a va 2s . co m*/ 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(); }