@SuppressWarnings("unused")
  private void fetchTweets(String company) {
    Twitter twitter = new TwitterFactory().getInstance();

    Query query = new Query(company);
    query.setLang("en");
    query.setRpp(100);

    try {
      List<TweetDTO> tweets = new ArrayList<TweetDTO>();

      for (int i = 1; i < 10; i++) {
        query.setPage(i);
        QueryResult result = twitter.search(query);
        for (Tweet tweet : result.getTweets()) {
          TweetDTO dto = new TweetDTO();
          dto.setTwitterId(Long.toString(tweet.getId()));
          dto.setText(tweet.getText());
          dto.setDate(tweet.getCreatedAt());
          dto.setCompanies(company);
          tweets.add(dto);
        }
      }

      new GoogleTweetDAO().storeTweet(tweets);
    } catch (TwitterException e) {
    }
  }
Esempio n. 2
0
 public Set<Tweet> twFind(String term) {
   Set<Tweet> foundTweets = new HashSet<Tweet>();
   try {
     Query query = new Query(term);
     query.setRpp(RPP);
     for (int i = 1; i <= MAX_PAGES; i++) {
       query.setPage(i); // loads result page by page
       QueryResult result = twitter.search(query);
       List<Tweet> tweets = result.getTweets();
       for (Tweet tweet : tweets) {
         foundTweets.add(tweet);
       }
     }
   } catch (TwitterException te) {
     te.printStackTrace();
     System.out.println("Failed to search tweets: " + te.getMessage());
     System.exit(-1);
   }
   return foundTweets;
 }
  public void grab(Date timeLimit) {
    log.info("Twitter grabber started...");
    Twitter twitter = new TwitterFactory().getInstance();

    List<Brand> brandList = handler.getBrandProvider().getAllBrands();
    ArticleProvider articleProvider = handler.getArticleProvider();

    for (Brand b : brandList) {
      Query query = new Query(b.getName());
      query.setRpp(PAGE_SIZE);
      query.setLang("ru");
      query.setResultType(Query.MIXED);

      List<Tweet> resultTweets = new LinkedList<Tweet>();
      QueryResult queryResult;
      int pageNumber = 1;

      try {
        do {
          query.setPage(pageNumber);
          queryResult = twitter.search(query);
          resultTweets.addAll(queryResult.getTweets());
          pageNumber++;
          log.info(pageNumber);
        } while (ISSUANCE_SIZE > resultTweets.size());
      } catch (TwitterException e) {
        throw new RuntimeException(e);
      }
      log.info("tweets in da111y: " + resultTweets.size());
      twitter = null;
      twitter = new TwitterFactory().getInstance();

      for (Tweet t : resultTweets) {
        articleProvider.writeArticleToDataStore(
            new Article(-1, b.getId(), 2, "", t.getText(), "", getSimpleTime(t.getCreatedAt()), 1));
      }
    }
    log.info("twitter grabber finished succesful.");
  }