Twitter4j catch exception error for "You have already favorited this status."

edited February 2016 in Library Questions

Hey all,

so I have a twitter bot that looks for statuses with a key term, then favorites. works fine and runs for some time, but then I am thrown an error of duplicate favorites:

Failed to favorite status: 403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following). message - You have already favorited this status. code - 139

see screenshot below. I am also attaching my code. i am unsure how to interpret this guide on the twitter dev docs since my error code isnt here. https://dev.twitter.com/overview/api/response-codes

I am pretty sure I need to have the correct error message to catch the exception... but thats just a guess.

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


Twitter twitter;
String searchString = "arduino";
List<Status> tweets;
Status status;
Status statusFav;

int currentTweet;
long tweetID;

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

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

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

  twitter = tf.getInstance();


  getNewTweets();

  thread("refreshTweets");
}

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

  currentTweet = currentTweet + 1;

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

  status = tweets.get(currentTweet);



  /* REFERENCE 
   status.getUser().getName(); // to get user name

   status.getId();// to get tweet id

   status.getUser().getScreenName(); // to get user screen name/ user handler

   status.getText();// tweet

   status.getFavoriteCount(); // favorite count

   status.getRetweetCount();// retweet count
   */

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

  tweetID = status.getId();
  println(tweetID);
  favTweet(tweetID); //favorite a tweet by the ID
  delay(87000); //1000 tweet limit per day. 87 seconds = 87000 milliseconds
}

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

    QueryResult result = twitter.search(query);

    tweets = result.getTweets();
  }
  catch (TwitterException te)
  {
    //You have already favorited this status.
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.out.println("You have already favorited this status. " + te.getMessage());

    System.exit(-1);
  }
}

void favTweet(long ID) {

  try {

    statusFav = twitter.createFavorite(ID);
  } 
  catch (TwitterException te) {
    te.printStackTrace();
    System.out.println("You have already favorited this status. " + te.getMessage());
    System.exit(-1);
  }
}




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

    println("Updated Tweets");

    delay(30000);
  }
}
Tagged:

Answers

  • I thought I could use the functions from Status to bypass this.

    My unsuccessful attempt

    if (status.isFavorited() == false){
        favTweet(tweetID); //favorite a tweet by the ID
    }else if (status.isFavorited() != false) {
            //do nothing
    }
    
Sign In or Register to comment.