Detect a XML with a list of Tweets with Processing and the library Twitter4j

Hello, our projectis about an app that detect a list of tweets related to emotions and convert into sounds. So we think that the best idea is to have a large list of tweets on an XML with their sound. Now we are only trying to connect the tweets from the XML at processing. We are based on this example: http://codasign.com/tutorials/processing-and-twitter-searching-twitter-for-tweets/

We tried to change the String tag for the XML but we have this error:

Failed to search tweets: 400:The request was invalid. An accompanying error message will explain why. This is the status code will be returned during version 1.0 rate limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request without authentication is considered invalid and you will get this response. message - Query parameters are missing. code - 25

The code is this one, can someone help us please? Thanks!

import twitter4j.conf.*; import twitter4j.*; import twitter4j.auth.*; import twitter4j.api.*; import java.util.*;

Twitter twitter; //String tag = "kjaks"; ////// NOM DEL TWITTER String tag; List tweets; int currentTweet;

processing.data.XML[] tags;

void setup() { size(800,600); cargarXMLTags();

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("Y3u3C9ikb68xjdDY2w0GpQ");
cb.setOAuthConsumerSecret("yWBAEETGtiubzT54IQXN2UE7ASI0Lxsq8rpsbNA");
cb.setOAuthAccessToken("2253256123-lIANTBWORgLvrcOlvZiuhUXqqN7ppA6JWVgPnnV");
cb.setOAuthAccessTokenSecret("qO4p1wDTa4I03qjq7VfPtajRthvfAypNawLmkYhelT9h8");

TwitterFactory tf = new TwitterFactory(cb.build());

twitter = tf.getInstance();
getNewTweets();
currentTweet = 0;
}

void draw() { fill(0, 100); rect(0, 0, width, height);

currentTweet = currentTweet + 1;

if (currentTweet >= tweets.size())
{
    currentTweet = 0;
}

 if (currentTweet >= tweets.size())
{
    currentTweet = 0;
}

Status status = tweets.get(currentTweet);

fill(200);
text(status.getText(), random (width), random(height), 300, 200);
//text(status.getText(), width/2, height/2);

delay(250);
}

void getNewTweets() { try { Query query = new Query(); //possar tag dins QueryResult result = twitter.search(query); tweets = result.getTweets();

     long[] msg = new long[tags.length];
    for (int i=0; i<tags.length; i++){
      msg[i] = twitter.showUser(tags[i].getContent()).getId();
  }

} 

catch (TwitterException te) 
{
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
} 
}

void cargarXMLTags() { processing.data.XML xml = loadXML("tags.xml"); tags = xml.getChildren("tag");

for (int i=0; i<tags.length; i++) { String nom = tags[i].getContent(); //usuarios[i] = new Usuario(nom); } }

Answers

    1. Avoid using delay() in a sketch. Better base timings on millis(), there are lot of examples in the forums.
    2. "rate limiting" Twitter (and lot of Web APIs) limits the number of requests per hour. You seem to do 4 requests per second! Twitter sees that as an abuse and returns an error.
  • edited January 2014

    I was looking at the parameters of my application in Twitter and I went to the OAuth tools (where is stabilized the number of requests) and I saw this:

    Authorization: OAuth oauth_consumer_key="Y3u3C9ikb68xjdDY2w0GpQ", oauth_nonce="1480da1cc31ec8c536bdcfcb42c6ec02", oauth_signature="ki3agmAyKlESF8aHej5Kx5B9eoA%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1390734562", oauth_token="2253256123-lIANTBWORgLvrcOlvZiuhUXqqN7ppA6JWVgPnnV", oauth_version="1.0"

    So the oauth_ version is at version 1.0 instead of the 1.1. Could be this the problem? Anyone know how to change it? Or there's some function that I can put at the Processing file and limits the number of requests?

  • Again, use millis() to find out when to do a new request. Eg. once every minute or similar (within the limits set by Twitter).

  • Thanks PhiLho!

    I tried and this kind of error disappear and the project compiles. However, when the millis() reaches the number of seconds that we declared, the program crashes and we have another error:

    Could not run the sketch (Target VM failed to initialize). Make sure that you haven't set the maximum available memory too high. For more information, read revisions.txt and Help → Troubleshooting.

    Anyone know what I should do?

    In this forum http://forum.processing.org/two/discussion/806/could-not-run-the-sketch/p1 it appear something related to remove the Opengl, but I don't know if it is the solution or how I can remove it.

    Thanks

  • A side note, twitter expects you to not publish your oauth data...

    from them...

    Use the access token string as your "oauth_token" and the access token secret as your "oauth_token_secret" to sign requests with your own Twitter account. Do not share your oauth_token_secret with anyone.

    Your application's OAuth settings. Keep the "Consumer secret" a secret. This key should never be human-readable in your application.

Sign In or Register to comment.