Loading...
Logo
Processing Forum
Hi everyone, I am just too new to both processing and twitter API. Can anyone please help me on how to create a Processing sketch that would display the public tweets from Twitter's streaming API? 

Thanks in advance 

Replies(5)

Use the twitter4j lib.... that will do what you want.
http://twitter4j.org/en/index.html

Good luck!

Rolf
here a basic sample of twitter stream:
note that the library jars are in the code folder in the sketch folder
twitter4j 3.0.3


Copy code
  1. TwitterStream twitterStream;

  2. void setup() {     
  3.   size(100, 100);    
  4.   background(0); 
  5.   openTwitterStream();
  6. }  


  7. void draw() {     
  8.   background(0);
  9. }  



  10. // Stream it
  11. void openTwitterStream() {  
  12.   
  13.   ConfigurationBuilder cb = new ConfigurationBuilder();  
  14.   cb.setOAuthConsumerKey("---INPUT HERE----");
  15.   cb.setOAuthConsumerSecret("---INPUT HERE----");
  16.   cb.setOAuthAccessToken("---INPUT HERE----");
  17.   cb.setOAuthAccessTokenSecret("---INPUT HERE----"); 

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

  19.   FilterQuery filtered = new FilterQuery();
  20.   
  21.    // if you enter keywords here it will filter, otherwise it will sample
  22.   String keywords[] = {"love"};

  23.   filtered.track(keywords);
  24.    
  25.   twitterStream.addListener(listener);
  26.   
  27.   if (keywords.length==0) // sample() method internally creates a thread which manipulates TwitterStream 
  28.   twitterStream.sample(); // and calls these adequate listener methods continuously.
  29.   else 
  30.   twitterStream.filter(filtered);

  31.   println("connected");


  32. // Implementing StatusListener interface
  33. StatusListener listener = new StatusListener() {
  34.            
  35.             //@Override
  36.             public void onStatus(Status status) {
  37.                 System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
  38.             }

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

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

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

  51.             //@Override
  52.             public void onStallWarning(StallWarning warning) {
  53.                 System.out.println("Got stall warning:" + warning);
  54.             }

  55.             //@Override
  56.             public void onException(Exception ex) {
  57.                 ex.printStackTrace();
  58.             }
  59. };