@Override public List<Status> loadInBackground() { try { // 大阪のWOEID // int osaka = 15015370; // トレンドを取得する // Trend[] trend = this.twitter.getPlaceTrends(osaka).getTrends(); // 取得したトレンドから、ランダムで1つを選択する // Random rnd = new Random(); // String q = trend[rnd.nextInt(trend.length)].getQuery(); // 検索文字列を設定する Query query = new Query(keyword); query.setLocale("ja"); // 日本語のtweetに限定する query.setCount(100); // 最大20tweetにする(デフォルトは15) query.resultType(Query.RECENT); QueryResult result = null; // 最大1500件(15ページ)なので15回ループ for (int i = 1; i <= 15; i++) { result = twitter.search(query); System.out.println("ヒット数 : " + result.getTweets().size()); System.out.println("ページ数 : " + new Integer(i).toString()); /* // 検索結果を見てみる for (Status tweet : result.getTweets()) { // 本文 String str = tweet.getText(); java.util.Date hiduke = tweet.getCreatedAt(); System.out.println(hiduke + str); // ハッシュタグとURLの削除 } if (result.hasNext()) { query = result.nextQuery(); } else { break; } */ if (result.getTweets().size() < 100) { break; } } // 検索の実行 // QueryResult result = this.twitter.search(query); return result.getTweets(); } catch (TwitterException e) { Log.d("twitter", e.getMessage()); } return null; }
/** * It returns MAX(available_tweets,200,limit) tweets from the search of the specified query. In * case of error, returns null. */ public QueryResult searchTweets(String queryTerm, int limit) { try { Query query = new Query(queryTerm); query.setCount(limit); QueryResult result = twitter.search(query); LoggerClass.log( Level.INFO, "OK. Searching tweets for term " + queryTerm + " and limit " + limit + ". " + result.getCount() + " results."); return result; } catch (Exception ex) { LoggerClass.log( Level.SEVERE, "ERROR searching tweets for term " + queryTerm + " and limit " + limit + "."); System.err.println("[ERROR] Can't search tweets"); ex.printStackTrace(); return null; } }
private static List<Status> buscarTag(String tag) throws TwitterException { TwitterFactory factory = new TwitterFactory(); AccessToken accessToken = loadAccessToken(); Twitter twitter = factory.getInstance(); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.setOAuthAccessToken(accessToken); Query query = new Query(tag); query.setCount(100); QueryResult result = twitter.search(query); // printResultadosConsulta(result); return result.getTweets(); }
// Method to perform the actual query public List<Status> searchTweet(String inQuery) { Query query = new Query(inQuery); query.setCount(100); // Query Length List<Status> tweetList = new ArrayList<>(); try { int count = 0; QueryResult queryResult; do { queryResult = twitter.search(query); // Perform Search tweetList = queryResult.getTweets(); // get the results and put in Array // keep printing as long as there are tweets in the Array } while ((query = queryResult.nextQuery()) != null && count <= LIMIT); } // if anything goes wrong print this line catch (TwitterException te) { System.out.println("Couldn't connect: " + te.getErrorMessage()); } return tweetList; }
public void doNext() { try { if (result == null) { Query query = new Query(this.keywords); query.setCount(tweetBatchCount); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } result = twitter.search(query); i = 0; } else if (i >= tweetBatchCount - 1) { Query nextResult = result.nextQuery(); if (nextResult == null) { finished = true; } else { result = twitter.search(nextResult); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } i = 0; } } activeResultSet = result.getTweets(); } catch (TwitterException e) { e.printStackTrace(); } }
private void getAndStoreTweetsOnTopic(String topic) { // Configuring twitter ConfigurationBuilder configBuilder = new ConfigurationBuilder(); configBuilder.setOAuthAccessToken(ACCESS_TOKEN); configBuilder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); configBuilder.setOAuthConsumerKey(CONSUMER_KEY); configBuilder.setOAuthConsumerSecret(CONSUMER_KEY_SECRET); Configuration configuration = configBuilder.build(); TwitterFactory factory = new TwitterFactory(configuration); Twitter twitter = factory.getInstance(); Query query = new Query(topic); // number of tweets to return per page query.setCount(100); try { // search for tweets on specifc topics QueryResult search = twitter.search(query); List<Status> tweets = search.getTweets(); // initialize Mongo DB Mongo mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT); DB db = mongo.getDB(MONGO_DB_NAME); DBCollection collection = db.getCollection(MONGO_DB_COLLECTION); for (Status tweet : tweets) { System.out.println(getTweetInfo(tweet)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dateWithoutTime = sdf.parse(sdf.format(tweet.getCreatedAt())); // create a document BasicDBObject document = new BasicDBObject(); document.put("createdAt", dateWithoutTime); document.put("user", tweet.getUser().getName()); document.put("rTCount", tweet.getRetweetCount()); document.put("tweet", tweet.getText()); document.put("source", tweet.getSource()); // store it in database collection.insert(document); } /** * // Find all records from collection DBCursor cursorDoc = collection.find(); * * <p>// ... and display while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next()); * }* */ } catch (TwitterException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } }