Example #1
0
  /**
   * Method that handles the Twitter streaming API. <br>
   * <b>WARNING:</b> Method does not terminate by itself, due to the fact that the streamer runs in
   * a different thread.
   *
   * @param keywords The keywords for which the streamer searches for tweets.
   * @param mongoDB A handler for the MongoDB database.
   * @param config A configuration object.
   */
  public final void retrieveTweetsWithStreamingAPI(
      String[] keywords, MongoHandler mongoDB, Config config) {

    ConfigurationBuilder cb = getAuthorization();
    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    final StatusListener listener;
    listener =
        new StatusListener() {

          @Override
          public final void onStatus(Status status) {
            // Insert tweet to MongoDB
            mongoDB.insertSingleTweetIntoMongoDB(status, "NULL");
          }

          @Override
          public final void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            PrintUtilities.printInfoMessageln(
                "Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }

          @Override
          public final void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            PrintUtilities.printInfoMessageln(
                "Got track limitation notice:" + numberOfLimitedStatuses);
          }

          @Override
          public final void onScrubGeo(long userId, long upToStatusId) {
            PrintUtilities.printInfoMessageln(
                "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }

          @Override
          public final void onStallWarning(StallWarning warning) {
            PrintUtilities.printInfoMessageln("Got stall warning:" + warning);
          }

          @Override
          public final void onException(Exception ex) {
            ex.printStackTrace(System.out);
          }
        };

    FilterQuery fq = new FilterQuery();
    fq.language("en"); // Set language of tweets to "English"
    fq.track(keywords); // Load the search terms

    twitterStream.addListener(listener); // Start listening to the stream
    twitterStream.filter(fq); // Apply the search filters
  }
  @Override
  public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    statuses = new LinkedBlockingQueue<Status>(1000);
    this.spoutOutputCollector = collector;

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder
        .setOAuthConsumerKey(consumerKey)
        .setOAuthConsumerSecret(consumerSecret)
        .setOAuthAccessToken(accessToken)
        .setOAuthAccessTokenSecret(accessTokenSecret);

    OAuthAuthorization authAuthorization = new OAuthAuthorization(configurationBuilder.build());

    twitterStream = new TwitterStreamFactory().getInstance(authAuthorization);
    twitterStream.addListener(
        new StatusListener() {
          @Override
          public void onStatus(Status status) {
            statuses.offer(status);
          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

          @Override
          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

          @Override
          public void onScrubGeo(long userId, long upToStatusId) {}

          @Override
          public void onStallWarning(StallWarning warning) {}

          @Override
          public void onException(Exception ex) {}
        });

    twitter = new TwitterFactory().getInstance(authAuthorization);
    filterQuery = new FilterQuery();

    if (filterQuery == null) {
      twitterStream.sample();
      ;
    } else {
      twitterStream.filter(filterQuery.track(filterWords));
      twitterStream.filter(filterQuery.language(filterLanguages));
    }
  }
Example #3
0
  public static void main(String args[]) {
    FilterQuery tweetFilterQuery = new FilterQuery();
    String a[] = {"en"};
    tweetFilterQuery.language(a);
    ConfigurationBuilder _configurationBuilder = new ConfigurationBuilder();
    _configurationBuilder
        .setOAuthConsumerKey(consumerKey)
        .setOAuthConsumerSecret(consumerSecret)
        .setOAuthAccessToken(accessToken)
        .setOAuthAccessTokenSecret(accessTokenSecret)
        .setJSONStoreEnabled(true);

    twitterStream = new TwitterStreamFactory(_configurationBuilder.build()).getInstance();

    StatusListener listener =
        new StatusListener() {

          @Override
          public void onException(Exception arg0) {
            System.out.println("Error occured: " + arg0.getMessage());
            arg0.printStackTrace();
          }

          @Override
          public void onTrackLimitationNotice(int arg0) {
            System.out.println("Track limitation notice for " + arg0);
          }

          @Override
          public void onStatus(Status status) {
            // if(status.getLang().equals("en"))
            //	System.out.println(status.getText().replaceAll("[\n\r]", ""));
            String filename = totalCount / 100 + ".txt";
            File f = new File(filename);
            PrintWriter out = null;

            if (f.exists() && !f.isDirectory()) {
              // System.out.println("@" + status.getUser().getScreenName() + " - " +
              // status.getText() + " -> "+ status.getCreatedAt());
              // String mapstring = status.getUser().getScreenName() + " - " + status.getText() + "
              // -> "+ status.getCreatedAt() +"\n";
              String mapstring = status.getText().replace("\n", " ") + "\n";

              try {
                out = new PrintWriter(new FileOutputStream(new File(filename), true));
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              }
              out.append(mapstring);
              out.close();
            } else {

              try {
                out = new PrintWriter(filename);
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              }
              String mapstring = status.getText().replace("\n", " ") + "\n";
              out.println(mapstring);
              out.close();
            }
            totalCount++;
          }

          @Override
          public void onStallWarning(StallWarning arg0) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onScrubGeo(long arg0, long arg1) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onDeletionNotice(StatusDeletionNotice arg0) {
            // TODO Auto-generated method stub

          }
        };
    twitterStream.addListener(listener);

    twitterStream.sample();
  }