Add tweet to an Array for printing on stage

edited December 2013 in Library Questions

I have been working on this code, basically it uses twitters stream, whenever it gets a certain hashtag it plays an audio track and its then supposed to display the tweet on the stage. I have pretty much everything working but I'm unsure how I should go about adding the tweet to the stage. I know I need to add the incoming text to an array and from that draw it, but I'm stuck. Any help would be greatly appreciated. heres my code so far

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
/*@pjs preload="christmas.jpg";*/

ArrayList<String> words = new ArrayList();

import ddf.minim.*;

PFont f;
PImage bg;

Christmas christmas;
TwitterStream twitterStream;
Minim minim;


void setup() {     
  size(640, 360);     
  bg = loadImage("christmas.jpg");
  // start up Minim
  minim = new Minim(this);

  christmas = new Christmas (150, 100, 32, "noddy.mp3");
  smooth();
  openTwitterStream();
}   

// Stream it
void openTwitterStream() {  

  ConfigurationBuilder cb = new ConfigurationBuilder();  
  cb.setOAuthConsumerKey("-");
  cb.setOAuthConsumerSecret("-");
  cb.setOAuthAccessToken("-");
  cb.setOAuthAccessTokenSecret("-"); 

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

  FilterQuery filtered = new FilterQuery();

   // if you enter keywords here it will filter, otherwise it will sample
  String keywords[] = {"#christmasTweat"};

  filtered.track(keywords);

  twitterStream.addListener(listener);

  if (keywords.length==0)// sample() method internally creates a thread which manipulates TwitterStream 
  twitterStream.sample(); // and calls these adequate listener methods continuously.
  else 
  twitterStream.filter(filtered);

  println("connected");
} 

void draw() {     
 background(bg);
} 

// Implementing StatusListener interface

StatusListener listener = new StatusListener() {

            //@Override
            public void onStatus(Status status) {
                System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                christmas.ring();
            }

            //@Override
            public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
                System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            //@Override
            public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
            }

            //@Override
            public void onScrubGeo(long userId, long upToStatusId) {
                System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
            }

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


};

Answers

  • edited December 2013 Answer ✓

    hey acott,

    I'd start by making a Tweet class:

    class Tweet {
       String tweet, user;
      Tweet(String t, String u) {
        tweet = t;
        user = u;
      }
    }
    

    Then you'll need an ArrayList to hold the tweets:

    ArrayList<Tweet> tweets;
    

    Then whenever you get a new status in your onStatus() method, you can create the tweet, then add it to that array list:

       public void onStatus(Status status) {
                    System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
                    Tweet t = new Tweet(status.getText(), status.getUser().getScreeName());
                    tweets.add(t);
                    christmas.ring();
                }
    

    Then you'll just need to loop through the tweets array in your draw loop and draw the username and tweet to the screen.

    Hope this helps! ak

  • excellent..thanks so much!

Sign In or Register to comment.