List of usage examples for twitter4j Query Query
public Query(String query)
From source file:cp.Demo.java
private void MoviesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_MoviesListValueChanged // Obtain the movie object Movie mov;/* ww w . java 2 s . c om*/ try { mov = (Movie) MoviesList.getSelectedValue(); System.out.println(mov.getApiQuery()); } catch (Exception ex) { PosterLabel.setIcon(null); PosterLabel.setText("Movie not found"); return; } // Display the movie poster try { // Load the movie poster URL url = new URL(mov.getPosterLink()); ImageIcon img = new ImageIcon(url); // Rescale the movie poster int w = img.getIconWidth(); int h = img.getIconHeight(); int labelH = MovieTabs.getHeight() - 36; img.setImage(img.getImage().getScaledInstance(labelH * w / h, labelH, Image.SCALE_SMOOTH)); // Put the movie poster into the label, and change the text PosterLabel.setIcon(img); PosterLabel.setText(""); } catch (Exception ex) { // If for some reason the poster isn't available, indicate this PosterLabel.setIcon(null); PosterLabel.setText("Poster could not be displayed"); } // Display tweets about the movie try { // clear out the tweets TweetsArea.setText(""); // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); Query query = new Query(mov.getTitle()); QueryResult result = twitter.search(query); String tweet; for (Status status : result.getTweets()) { tweet = String.format("[%s] @%s tweeted %s\n", status.getCreatedAt(), status.getUser().getScreenName(), status.getText()); TweetsArea.append(tweet); } } catch (TwitterException ex) { TweetsArea.setText(ex.toString()); } // Display reviews of the movie try { ReviewsArea.setText(""); for (Review rev : (new ReviewRequest(mov.getMovie())).getReviews()) { String review = String.format("[%s] (by %s, %s)\n%s\n%s\n\n", rev.getDateOfReview(), rev.getCritic(), rev.getPublication(), rev.getQuote(), rev.getReviewLinkUrl()); ReviewsArea.append(review); } } catch (IOException ex) { Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); } catch (ReviewRequest.ReviewRequestException ex) { Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:crawler.DataSearch.java
License:Apache License
public void collectNewTweets() { while (keywords.size() > 0) { String hash = keywords.get(0); Query query = new Query(hash); query.setCount(Settings.searchTweetNo); QueryResult result;//from w w w. j av a 2 s .co m try { result = twitter.search(query); List<Status> tweets = result.getTweets(); if (tweets.size() == 0) TwitterCrawler.TFFreq.remove(hash); for (Status status : tweets) { //System.out.println("{"+hash+"}["+status.getCreatedAt().toString()+"]- [USER: " + status.getUser().getScreenName() + "] - " + status.getText()); String temp; if (trash(status.getText())) { if (Settings.limitTime) { long currentTime = System.currentTimeMillis(); long tweetTime = status.getCreatedAt().getTime(); if (currentTime - tweetTime > Settings.timeLimit) continue; } if (hashtext.containsKey(hash)) temp = hashtext.get(hash) + status.getText(); else temp = status.getText(); hashtext.put(hash, temp); } } //System.out.println("{"+hash+"} with "+tweets.size()+"tweets"); keywords.remove(0); } catch (TwitterException e) { if (e.getErrorCode() == 88) { System.out.println("Using other keys???"); String ConsumerKey_temp = Settings.ConsumerKey.get(0); String ConsumerSecret_temp = Settings.ConsumerSecret.get(0); String AccessToken_temp = Settings.AccessToken.get(0); String AccessSecret_temp = Settings.AccessSecret.get(0); Settings.ConsumerKey.remove(ConsumerKey_temp); Settings.ConsumerSecret.remove(ConsumerSecret_temp); Settings.AccessToken.remove(AccessToken_temp); Settings.AccessSecret.remove(AccessSecret_temp); twitter = new TwitterFactory( OA.build(Settings.ConsumerKey.get(0), Settings.ConsumerSecret.get(0), Settings.AccessToken.get(0), Settings.AccessSecret.get(0))).getInstance(); Settings.ConsumerKey.add(Settings.ConsumerKey.size(), ConsumerKey_temp); Settings.ConsumerSecret.add(Settings.ConsumerSecret.size(), ConsumerSecret_temp); Settings.AccessToken.add(Settings.AccessToken.size(), AccessToken_temp); Settings.AccessSecret.add(Settings.AccessSecret.size(), AccessSecret_temp); System.out.println("**************************************************************"); System.out.println("[" + ConsumerKey_temp + "] to [" + Settings.ConsumerKey.get(0) + "]"); System.out.println("[" + ConsumerSecret_temp + "] to [" + Settings.ConsumerSecret.get(0) + "]"); System.out.println("[" + AccessToken_temp + "] to [" + Settings.AccessToken.get(0) + "]"); System.out.println("[" + AccessSecret_temp + "] to [" + Settings.AccessSecret.get(0) + "]"); System.out.println("**************************************************************"); continue; } e.printStackTrace(); } } }
From source file:crawler.TwitterFeed.java
/** * */// www. ja v a 2 s . c o m public void get_tweets() throws SQLException { CSVReader cr = new CSVReader(); ArrayList<String> names = new ArrayList<>(); Map<String, String> name_ticker; names = cr.get_company_names(tickers); System.out.println(names); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey("WNSycI2GS33ZAwHJ9Fb4A"); cb.setOAuthConsumerSecret("BBWeSThT3ZV2g9c5BDuUduDyNAWyzouMI0XjQy7KUc"); cb.setOAuthAccessToken("1852271029-SLfE061bImfcRxWQZpy1pAgpEkfFhg3757Q9JRf"); cb.setOAuthAccessTokenSecret("1uPSfYPbaENtXFYWsryBIAHnUsmG3pT6lGH0NzxTCGW7O"); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); for (String s : names) { String[] temp; if (s.contains(",")) { temp = s.split(","); } else { temp = s.split(" "); } System.out.println( "--------------------------------------------------------------------------------------" + temp[0]); Query query = new Query("#" + temp[0]); query.lang("en"); int numberOfTweets = 500; long lastID = Long.MAX_VALUE; ArrayList<Status> tweets = new ArrayList<>(); while (tweets.size() < numberOfTweets) { if (numberOfTweets - tweets.size() > 100) { query.setCount(100); } else { query.setCount(numberOfTweets - tweets.size()); } try { QueryResult result = twitter.search(query); tweets.addAll(result.getTweets()); System.out.println("Gathered " + tweets.size() + " tweets"); for (Status t : tweets) { if (t.getId() < lastID) { lastID = t.getId(); } } } catch (TwitterException te) { System.out.println("Couldn't connect: " + te); } query.setMaxId(lastID - 1); } Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CrawledData", "root", ""); for (int i = 0; i < tweets.size(); i++) { Status t = (Status) tweets.get(i); //GeoLocation loc = t.getGeoLocation(); String user = t.getUser().getScreenName(); String msg = t.getText(); String time = t.getCreatedAt().toString(); //if (loc!=null) { // Double lat = t.getGeoLocation().getLatitude(); // Double lon = t.getGeoLocation().getLongitude(); // System.out.println(i + " USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon); //} //else{ System.out.println(i + " USER: " + user + " wrote: " + msg + " at: " + time); //} try { Class.forName("com.mysql.jdbc.Driver"); PreparedStatement ps = con .prepareStatement("INSERT IGNORE INTO TwitterFeed VALUES (?,?,?,?,?)"); ps.setString(1, s); ps.setString(2, cr.get_ticker_from_company(s)); ps.setString(3, user); ps.setString(4, msg); ps.setString(5, time); int k = ps.executeUpdate(); if (k <= 0) { System.out.println("Entry Unsuccessful"); } else { System.out.println("Entry Successful"); } } catch (ClassNotFoundException | SQLException e) { System.out.println(e); } } con.close(); } }
From source file:crawltweets2mongo.MonoThread.java
void getNewTweets(String keyword) { try {/*from ww w . j a v a2s. com*/ Query query = new Query(keyword); query.setCount(20000); QueryResult result; result = twitter.search(query); //System.out.println("Getting Tweets..by Key."); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { BasicDBObject basicObj = new BasicDBObject(); basicObj.put("user_Rname", tweet.getUser().getName()); basicObj.put("user_name", tweet.getUser().getScreenName()); basicObj.put("retweet_count", tweet.getRetweetCount()); basicObj.put("tweet_followers_count", tweet.getUser().getFollowersCount()); UserMentionEntity[] mentioned = tweet.getUserMentionEntities(); basicObj.put("tweet_mentioned_count", mentioned.length); basicObj.put("tweet_ID", tweet.getId()); basicObj.put("tweet_text", tweet.getText()); Status temp1 = tweet.getRetweetedStatus(); if (temp1 != null) basicObj.put("Re_tweet_ID", temp1.getUser().getId()); GeoLocation loc = tweet.getGeoLocation(); if (loc != null) { basicObj.put("Latitude", loc.getLatitude()); basicObj.put("Longitude", loc.getLongitude()); } basicObj.put("CreateTime", tweet.getCreatedAt()); basicObj.put("FavoriteCount", tweet.getFavoriteCount()); basicObj.put("user_Id", tweet.getUser().getId()); if (tweet.getUser().getTimeZone() != null) basicObj.put("UsertimeZone", tweet.getUser().getTimeZone()); if (tweet.getUser().getStatus() != null) basicObj.put("UserStatus", tweet.getUser().getStatus()); //basicObj.put("tweetLocation", tweet.getPlace().getGeometryCoordinates()); String U_Loc = tweet.getUser().getLocation(); if (U_Loc != null) basicObj.put("userLocation", U_Loc); basicObj.put("number_of_rt", tweet.getRetweetCount()); if (mentioned.length > 0) { basicObj.append("mentions", pickMentions(mentioned)); } try { collection.insert(basicObj); } catch (Exception e) { //System.out.println("MongoDB Connection Error : " + e.getMessage()); // loadMenu(); } } collection.ensureIndex(new BasicDBObject("tweet_ID", 1), new BasicDBObject("unique", true)); } catch (TwitterException ex) { java.util.logging.Logger.getLogger(MonoThread.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:DataCollections.TweetCollections_HashTags.java
public void collectTweetsFromHashtag_popterm(Hashtag_dbo hashtag) throws InterruptedException { long max_id = 0; boolean midused = false, sidused = false; if (hashtag.values[Hashtag_dbo.map.get("max_id")].used) { max_id = hashtag.values[Hashtag_dbo.map.get("")].lnumber; midused = true;// ww w. j a va 2s . co m } long since_id = 0; if (hashtag.values[Hashtag_dbo.map.get("since_id")].used) { since_id = hashtag.values[Hashtag_dbo.map.get("since_id")].lnumber; sidused = true; } Query q = new Query(hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string); //LogPrinter.printLog("Collection Tweets for hashtag_searchterm"+hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string); q.setCount(100); if (midused) { q.setMaxId(max_id); } if (sidused) { q.setSinceId(since_id); } QueryResult result = null; try { result = searchres.search(q); } catch (Exception e) { LogPrinter.printLog("Tweet Search Resources Rate Limit reached "); if (e instanceof TwitterException) { } if (e instanceof InterruptedException) { //Thread.sleep(((TwitterException)e).getRetryAfter()*1000+5000); } } int count = 0; for (Status s : result.getTweets()) { Tweet_dbo tweet = thelper.convertStatusToTweet_dbo(s); String whereclause = "tweet_id = " + Long.toString(tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber); tweet.values[Tweet_dbo.map.get("processed")].setValue("true"); tweet.values[Tweet_dbo.map.get("f_search")].setValue("true"); tweet.values[Tweet_dbo.map.get("searchterm")] .setValue(hashtag.values[Hashtag_dbo.map.get("hashtag_popterm")].string); if (TweetsTable.select(whereclause, 0, 2).length == 0) { //LogPrinter.printLog(" Inserting tweet "+count+tweet.values[Tweet_dbo.map.get("tweet_id")].lnumber); TweetsTable.insert(tweet); users_edgescollections.extract_InsertUsers_EdgesFromTweet(s); count++; } } }
From source file:de.dev.eth0.retweeter.Distributor.java
License:BEER-WARE LICENSE
/** * Distributes the tweets to all target accounts. * * @return/*from w w w . jav a2 s . c om*/ * @throws TwitterException */ public int distribute() throws TwitterException { if (!getConfig().isDistributorConfigValid()) { return Integer.MIN_VALUE; } Twitter twitter = getTwitter(); String search = buildSearchString(); Query query = new Query(search); QueryResult result = twitter.search(query); int count = 0; // Iterate through results for (Tweet tweet : result.getTweets()) { // Check if already distributed if (!distributedTweets.contains(tweet.getId()) && StringUtils.contains(tweet.getText(), getConfig().getDistributorConfig().getHashtag())) { // Retweet each result to all targetaccounts for (String targetaccount : getConfig().getDistributorConfig().getTargetaccounts()) { // Dont distribute to yourself if (!StringUtils.equalsIgnoreCase(tweet.getFromUser(), targetaccount)) { StringBuilder sb = new StringBuilder(); sb.append("@"); sb.append(targetaccount); sb.append(": RT @"); sb.append(tweet.getFromUser()); sb.append(" "); sb.append(tweet.getText()); String text = sb.toString(); // try to tweet, might cause an exception (status duplicate) if already tweeted logger.debug("Distributing tweet {} to {}", tweet.toString(), targetaccount); try { twitter.updateStatus(text.length() < 140 ? text : text.substring(0, 140)); count++; } catch (TwitterException te) { logger.warn("distribute of tweet " + tweet.toString() + "failed " + text, te); } distributedTweets.add(tweet.getId()); } } } } return count; }
From source file:de.dev.eth0.retweeter.Retweeter.java
License:BEER-WARE LICENSE
/** * Performs the retweet-action and returns the number of tweets found with the configured hashtag * * @return number of retweeted tweets/*from ww w. j a va2s. c o m*/ * @throws TwitterException */ public int retweet() throws TwitterException { if (!getConfig().isRetweeterConfigValid()) { return Integer.MIN_VALUE; } Twitter twitter = getTwitter(); String search = buildSearchString(); Query query = new Query(search); QueryResult result = twitter.search(query); List<Status> lastretweets = twitter.getRetweetedByMe(); int count = 0; for (Tweet tweet : result.getTweets()) { // ignore retweets and check if the hashtag is really in the tweet's text if (!StringUtils.startsWith(tweet.getText(), "RT @") && StringUtils.contains(tweet.getText(), getConfig().getRetweeterConfig().getHashtag()) && !ALREADY_RETWEETED.contains(tweet.getId())) { boolean retweeted = false; for (Status retweet : lastretweets) { if (tweet.getId() == retweet.getRetweetedStatus().getId()) { retweeted = true; break; } } if (!retweeted) { // try to retweet, might fail logger.debug("found new tweet to retweet: {}", tweet.toString()); try { twitter.retweetStatus(tweet.getId()); count++; } catch (TwitterException te) { logger.debug("retweet failed", te); } finally { ALREADY_RETWEETED.add(tweet.getId()); } } } } return count; }
From source file:de.jetsli.twitter.TwitterSearch.java
License:Apache License
Query createQuery(String str) { Query q = new Query(str); q.setResultType(Query.RECENT); return q; }
From source file:de.jetsli.twitter.TwitterSearch.java
License:Apache License
long search(String term, Collection<Status> result, Map<String, User> userMap, int tweets, long lastMaxCreateTime) throws TwitterException { long maxId = 0L; long maxMillis = 0L; // TODO it looks like only one page is possible with 4.0.0 int maxPages = 1; int hitsPerPage = tweets; boolean breakPaging = false; for (int page = 0; page < maxPages; page++) { Query query = new Query(term); // RECENT or POPULAR query.setResultType(Query.MIXED); // avoid that more recent results disturb our paging! if (page > 0) query.setMaxId(maxId);/* w w w .ja va2 s. c o m*/ query.setCount(hitsPerPage); QueryResult res = twitter.search(query); // is res.getTweets() sorted? for (Status twe : res.getTweets()) { // determine maxId in the first page if (page == 0 && maxId < twe.getId()) maxId = twe.getId(); if (maxMillis < twe.getCreatedAt().getTime()) maxMillis = twe.getCreatedAt().getTime(); if (twe.getCreatedAt().getTime() + 1000 < lastMaxCreateTime) breakPaging = true; else { String userName = twe.getUser().getScreenName().toLowerCase(); User user = userMap.get(userName); if (user == null) userMap.put(userName, twe.getUser()); result.add(twe); } } // minMillis could force us to leave earlier than defined by maxPages // or if resulting tweets are less then request (but -10 because of twitter strangeness) if (breakPaging || res.getTweets().size() < hitsPerPage - 10) break; } return maxMillis; }
From source file:de.jetwick.ese.search.Feeding.java
License:Apache License
public Collection<MyTweet> createReal() { List<MyTweet> tweets = new ArrayList<MyTweet>(); try {/*from w ww .j a va 2 s . c om*/ // get some tweets about java Twitter twitter4j = new TwitterFactory().getInstance(); for (int i = 0; i < 3; i++) { Query q = new Query("java"); q.setRpp(100); for (Tweet tw : twitter4j.search(q).getTweets()) { MyTweet myTw = new MyTweet(tw.getId(), tw.getFromUser()); myTw.setText(tw.getText()); myTw.setCreatedAt(tw.getCreatedAt()); myTw.setFromUserId(tw.getFromUserId()); tweets.add(myTw); } Thread.sleep(1000); } } catch (Exception ex) { logger.error("Error while grabbing tweets from twitter!", ex); } return tweets; }