Working with high level Twitter data & twitter4j

edited May 2014 in How To...

Hello gracious Processing community,

I'm searching for good web references / walk-throughs of working with twitter stats. My intentions are to pull total quantities of reoccurring hashtags within the most recent 60 seconds. I want to provide realtime conversation stats. I am assuming that they're streaming API is going to be the way to go (versus the search api?), but I am not sure about this. I have setup an app, and I have all the necessary app credentials. I am just not sure about the technique in pulling all data needed and interpreting it.

I've checked out Jer Thorp's gracious tutorial here: http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter

But I'm not [yet] a strong enough coder to connect the dots from his process to where I want to go.

Any ideas / references would be greatly appreciated.

Thanks for the read!

-Jon

Answers

  • edited May 2014

    Hi Jon, I have just been doing some work myself with twitter4j. The code below is the basis of my sketches which stream tweets.

    Things to note in the code:
    keywords[] is where you can put in your hashtag search. This link has a good list of operators for the query https://dev.twitter.com/docs/using-search
    You then create a filterQuery() with this keyword.

    connectTwitter() and loadAccessToken() are just the setup methods to sort of your credentials.

    The statusListener() is the magic object which continually monitors the stream. And the onStatus() method is called whenever a tweet matching your query comes in.
    In the example I just print the tweet to my monitor. But you could load all of them into a String[] or even create a custom Tweet Class. Also look at this page https://dev.twitter.com/docs/platform-objects/tweets to see what is contained in the tweet object.

    static String OAuthConsumerKey = "";
    static String OAuthConsumerSecret = "";
    static String AccessToken = "";
    static String AccessTokenSecret = "";
    
    String keywords[] = {"#processing"};
    
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    
    void setup(){
      size(1280,720);
      noStroke();
      connectTwitter();
      twitterStream.addListener(listener);
    
      if (keywords.length==0){
        twitterStream.sample();
      }
      else{
        twitterStream.filter(new FilterQuery().track(keywords));
      }
    }  
    
    void draw() {
      background(64,153,255);
      smooth();
      noStroke();
      fill(255, 150);
    }
    
    void connectTwitter() {
      twitterStream.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
      AccessToken accessToken = loadAccessToken();
      twitterStream.setOAuthAccessToken(accessToken);
    }
    
    // Loading up the access token
    private static AccessToken loadAccessToken() {
      return new AccessToken(AccessToken, AccessTokenSecret);
    }
    
    StatusListener listener = new StatusListener(){
      public void onStatus(Status status){
        String tweet = status.getText();
        String user = "@" + status.getUser().getScreenName();
        tweet = user + " - " + tweet;
        println(tweet);
      }
      public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        //System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
      }
      public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        //  System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
      }
      public void onScrubGeo(long userId, long upToStatusId) {
        System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
      }
    
      public void onException(Exception ex) {
        ex.printStackTrace();
      }
      public void onStallWarning(StallWarning warning){
    
      }
    };
    

    Hope this helps.

Sign In or Register to comment.