Search code examples
javatwitterarraylisttwitter4j

populating an arraylist in twitter4j


I need to populate my arraylist with tweets from online, instead of hardcoded strings. Im using twitter4j 3.0.3.

Im pretty stumped with this project. Since its for school, i really need to get it figured out. Im a beginner programmer and i'm very confused.

   int numGood = 50;
   int numBad = 50;
  for (int i = 0; i < numGood; i++) {
    tweets.add("test");
  }
  for (int i = 0; i < numBad; i++) {
    tweets.add("#bad");
  }

}

ArrayList<String> tweets = new ArrayList<String>();



//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

FULL VERSION:

ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitterInstance;
Query queryForTwitter;

//ArrayList tweets;

void setup() {
  cb.setOAuthConsumerKey("xxxx");
  cb.setOAuthConsumerSecret("xxxx");
  cb.setOAuthAccessToken("xxxx");
  cb.setOAuthAccessTokenSecret("xxxx");
  cb.setUseSSL(true);

  size(640,440);

} //setup

//ArrayList<String> tweets = new ArrayList<String>();

public static void main (String args[]) throws TwitterException {
    Twitter twitter = new TwitterFactory().getInstance();
    List<Status> statuses = twitter.getUserTimeline("google");
    String hashtag = "#AlphaGo";
    System.out.println("The Twitter page contains " 
                        + countTweets(hashtag, statuses) 
                        + " tweets with the hashtag : " + hashtag);

}

public static int countTweets(String hashtag, List<Status> statuses){
    return (int) statuses.stream()
                         .filter(x -> x.getText().contains(hashtag))
                         .count();
}

//create a function that counts the tweets
//that contain a certain hashtag
int countTweets(String hashtag){
  int total = 0;
  for(String tweet : tweets){
    if(tweet.contains(hashtag)){
      total++;
    }
  }
  return total;
}

void draw(){

  //count the good and bad tweets
  int goodTweets = countTweets("#good");
  int badTweets = countTweets("#bad");

  //calculate color based on tweet counts
  float r = badTweets/100.0 * 255;
  float g = goodTweets/100.0 * 255;
  float b = 0;

  background(r, g, b);

}

EDIT: I want that hashtag data to modify the color of the background to a shade of color in between red and green, depending on the number of #good and #bad tweets. I like to think of it as a +100/-100 spectrum. each #good tweet is +1, each #bad is -1. If it is at -100 tweets, then the ellipse is full red. If it is at +100 tweets, then the ellipse is full green.

I know this is a little complicated, but its for an art project im doing. I followed a tutorial and currently have the twitter data responding on a simple array list of tweets (tutorial @ https://www.youtube.com/watch?v=gwS6irtGK-c) I am using processing, java, twitter4j 3.0.3, and a macbook pro with OSX el capitan 10.11.3


Solution

  • Here it is, using and a simple implementation of

    public static void main (String args[]) throws TwitterException {
        Twitter twitter = new TwitterFactory().getInstance();
        List<Status> statuses = twitter.getUserTimeline("google");
        String hashtag = "#AlphaGo";
        System.out.println("The Twitter page contains " 
                            + countTweets(hashtag, statuses) 
                            + " tweets with the hashtag : " + hashtag);
    
    }
    
    public static int countTweets(String hashtag, List<Status> statuses){
        return (int) statuses.stream()
                             .filter(x -> x.getText().contains(hashtag))
                             .count();
    }