Twitter4j - trouble with tweets search...

edited October 2016 in Library Questions

Hello everyone! One year ago I composed an algorithm which basically was triggered by a twitter hashtag. the same sketch doesn't work today and I can't understand why. The error it give back is "indexOutOfBoundException: index:0 size:0" on the "Status status = tweets.get(currentTweet);" I tried to compose a new code which kind works only with some "searchString" for others would unlogically (to me) give back the same error. any suggestion? Thankyou very much. Andrea

import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import processing.video.*;
int interval = 3600000;//timer's interval
int lastRecordedTime = 0;
int i=1;
String nomeFile="immagine";
PImage foto;
Capture cam;
File file;
Twitter twitter;
String searchString = "#TestTestTest";
String acceso = "#TestTestTest acceso"
List<Status> tweets;
int currentTweet;

void setup ()
{
   size(1280, 720);
background(0);
  String[] cameras = Capture.list();

  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }

    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, 640,480,cameras[1]);
    cam.start();  

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxx");
cb.setOAuthConsumerSecret("xxx");
cb.setOAuthAccessToken("xxx");
cb.setOAuthAccessTokenSecret("xxx");
TwitterFactory tf = new TwitterFactory(cb.build());

    twitter = tf.getInstance();

    getNewTweets();

    currentTweet = 0;

    thread("refreshTweets");
  }
  }


void draw()
{

  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);



    Status status = tweets.get(currentTweet);
if (acceso.equals(status.getText()) == true)
{println("mancano "+ (interval-(millis()-lastRecordedTime))/60000 +" min");

  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  //set(0, 0, cam);
if(millis()-lastRecordedTime>interval){
    //change FILL color

   saveFrame(nomeFile+i+".jpg");
   //file = new File("H:/Coding/Processing/ProveProc3/tweetEfoto/"+nomeFile+i+".jpg");
   //tweetPic(file, searchString);
   background(0);

    }
  }


   //and record time for next tick
   lastRecordedTime = millis();

 }


void getNewTweets()
{
    try
    {
        Query query = new Query(searchString);

        QueryResult result = twitter.search(query);

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

void refreshTweets()
{
    while (true)
    {
        getNewTweets();

        println("Updated Tweets");

        delay(180000);
    }
}

void testPassingFile(File _file)
{
  println(_file.exists());
  println(_file.getName());
  println(_file.getPath());
  println(_file.canRead());
}

void tweetPic(File _file, String theTweet)
{
  try
    {
       StatusUpdate status = new StatusUpdate(theTweet);
       status.setMedia(_file);
       twitter.updateStatus(status);
    }
    catch (TwitterException te)
    {
        println("Error: "+ te.getMessage()); 
    }
}
Tagged:

Answers

  • I found the specific tweet was too old to be found from the program...

  • edited October 2016

    That makes sense given the exception indexOutOfBoundException: index:0 size:0.

    Since it is an expected error if a tweet is not found, you may want to try/catch that exception in your code!

Sign In or Register to comment.