Loading...
Logo
Processing Forum
Hi Guys

I'm trying to get Processing to send a serial command when someone @'s my twitter account. I'm using twitter4J and this is what i've got so far:
Copy code
  1. // This is where you enter your Oauth info
  2. static String OAuthConsumerKey = "KEY";
  3. static String OAuthConsumerSecret = "CONSUMERS";

  4. // This is where you enter your Access Token info
  5. static String AccessToken = "TOKEN";
  6. static String AccessTokenSecret = "TOKENSECRET";

  7. // Just some random variables kicking around
  8. String myTimeline;
  9. java.util.List statuses = null;

  10. Twitter twitter = new TwitterFactory().getInstance();
  11. RequestToken requestToken;
  12. String[] theSearchTweets = new String[11];

  13. int lastTime = 0;

  14. void setup() {

  15.   size(100,100);
  16.   background(0);
  17.   connectTwitter();

  18. }

  19. void draw() {
  20.   
  21.   background(204);
  22.    
  23.    if( millis() - lastTime >= 30000){
  24.      
  25.   getTimeline();
  26.   lastTime = millis();
  27.   }

  28.  }
  29. // Initial connection
  30. void connectTwitter() {

  31.   twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
  32.   AccessToken accessToken = loadAccessToken();
  33.   twitter.setOAuthAccessToken(accessToken);

  34. }

  35. // Loading up the access token
  36. private static AccessToken loadAccessToken(){
  37.   return new AccessToken(AccessToken, AccessTokenSecret);
  38. }

  39. // Get mentions
  40. void getTimeline() {

  41.   try {
  42.     statuses = twitter.getMentions(); 
  43.   } catch(TwitterException e) { 
  44.     println("Get timeline: " + e + " Status code: " + e.getStatusCode());
  45.   }

  46.   for(int i=0; i<statuses.size(); i++) {
  47.     Status status = (Status)statuses.get(i);
  48.     println(status.getUser().getName() + ": " + status.getText());
  49.   }
  50. }
This currently pulls through my @ replays when the script starts up with no serial command (thats coming later). Where I would like help is as follows..

I would like the sketch to count how many NEW tweets have come through since the last time it checked. Any ideas?

Replies(2)

" I would like the script to call the Twitter API every minute or so"
Well, just look at millis() to watch the amount of time spent from the last update.
The endless loop is provided by draw(), called 60 times per second (depending on the frameRate setting).

Beware, the Processing API limits the number of connections per unit of time.
Thanks for your reply, I've just worked it out and modified the post (started to edit the post before you posted). Thanks though. I've managed to call getTimeline( ) every 30 seconds using mills().

My second and final question is  whether I could count how many NEW @ replies have come through since the last time it checked. Any ideas?