Sunday, October 25, 2015

Twitter Baseball



I have successfully mapped tweets to be associated with a smiley or frowny face depending on what the emotion of the tweet it is with is, as well as changed the color to associate with the emotion (blue is happy, red is sad). The first image is the tweets appearing then disappearing, the bottom image displays the tweets piling up over time.

 The text of the tweet is important to me because I feel that reading peoples emotions is interesting and relevant to the project. The visuals, such as the color of the text and the addition of the happy and sad face, add to the artistic value of the project because they help the viewer understand what tweets are happy or sad. It has been interesting for me to be able to see the blue and red text to readily see what emotions the tweets are conveying.


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

Twitter twitter;
//search twitter for hastag or words
String searchString = "royals lose";
List<Status> tweets;

String searchString2 = "royals win";
List<Status> tweets2;

//tweets
int currentTweet2;

int currentTweet;

//images
PImage img;
PImage img2;


void setup()

{
    //size and background of sketch
    size(2000,1000);
    background(#838383);
    smooth(0);
     //keys from apps.twitter
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("key");
    cb.setOAuthConsumerSecret("key");
    cb.setOAuthAccessToken("key");
    cb.setOAuthAccessTokenSecret("key");
  
     //get tweets
    TwitterFactory tf = new TwitterFactory(cb.build());

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

    thread("refreshTweets");
  
 
 
}


void draw()

{
    //load images
    img = loadImage("frownyface.png");
    img2 = loadImage("smileyface1.png");
    //background color and size of tweet
    fill(#838383);
    //make tweets not pile up
    //rect(0, 0, width, height);


    currentTweet = currentTweet + 1;
  
    //dictate size of tweet
    if (currentTweet >= tweets.size())
    {
        currentTweet = 0;
    }
  
    //tweet status 1
    Status status = tweets.get(currentTweet);
   
    //fill(255,165,0); //orange font color
    fill(255,0,0); //red font color
    textSize(random(14,30));
    //make image appear with tweet
    float k = random(width-100);
    float l = random(height-100);
    //image to be placed with tweet
    image(img, k, l);
    //placement of tweet
    text(status.getText(), k, l);

  

    //delay tweets appear
    delay(3000);
  
    //tweet 2
    Status status2 = tweets2.get(currentTweet);
 
    currentTweet2 = currentTweet2 + 1;
    if (currentTweet2 >= tweets2.size())
    {
      currentTweet2 = 1;
    }

    //blue font color
    fill(#1B2FF7); //color and size of second tweet
    textSize(random(14,30));
    //placement of second tweet
    float k2 = random(width-100);
    float l2 = random(height-100);
    //image to be placed with second tweet
    image(img2, k2, l2);
    text(status2.getText(), k2, l2);
  
    //delay of tweets
   delay(1000);


}

//retrieve tweets

void getNewTweets()

{

  try
  {
    //try to get tweets
    Query query = new Query(searchString);
  
    Query query2 = new Query(searchString2);
  
    //search
    QueryResult result = twitter.search(query);
  
    QueryResult result2 = twitter.search(query2);
  
    //get
    tweets = result.getTweets();
  
    tweets2 = result2.getTweets();
}

catch (TwitterException te)

  {
    //can't get tweets case
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
   }

}

void refreshTweets()
{
    while (true)
    {
      //getting tweets
        getNewTweets();

        //println("Updated Tweets");

        delay(300);
    }
}




I have also been working on making tweets containing the phrase "world series" in them appear around a baseball.


I modified the "world series" search by changing the visual to more clearly state what is being searched. I made the text in the shape of a baseball to enhance the text with the visual. I changed the image to a World Series image because it helps the viewer understand that the text is about the World Series opposed to solely baseball in general.

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

Twitter twitter;

//search twitter for hastag or words
String searchString = "world series";
List<Status> tweets;



//tweets
int currentTweet;

//images
PImage img3;

float r = 300;
float r2 = 100;

void setup()

{
    size(2000,1000);
    //load images
    imageMode(CENTER);
    img3 = loadImage("worldseries.png");
    background(255, 255, 255);
    textAlign(CENTER);
    smooth(0);
     //keys from apps.twitter
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey("key");
    cb.setOAuthConsumerSecret("key");
    cb.setOAuthAccessToken("key");
    cb.setOAuthAccessTokenSecret("key");
  
     //get tweets
    TwitterFactory tf = new TwitterFactory(cb.build());

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

    thread("refreshTweets");
  
  
 
}

//retreive tweets

void getNewTweets()

{

  try
  {
    //try to get tweets here
    Query query = new Query(searchString);
    //search
    QueryResult result = twitter.search(query);
    //get
    tweets = result.getTweets();
}

catch (TwitterException te)

  {
    //can't get tweets case
    System.out.println("Failed to search tweets: " + te.getMessage());
    System.exit(-1);
   }
}

void refreshTweets()
{
    while (true)
    {
      //getting tweets
        getNewTweets();
        //println("Updated Tweets");
        delay(300);
    }
}

void draw()

{
    //background color and size of tweet
    fill(255, 255, 255);
    //make tweets not pile up
    rect(0, 0, width, height);
  
    currentTweet = currentTweet + 1;
  
    //dictate size of tweet
    if (currentTweet >= tweets.size())
    {
        currentTweet = 0;
    }
  
    //tweet status 1
    Status status = tweets.get(currentTweet);
   
    //fill(255,165,0); //orange font color
    fill(255,0,0); //red font color
    textSize(random(18,30));
    image(img3,1000, 500);

  
    //delay tweets appear
    delay(5000);
  
    translate(width/2, height/2);
    noFill();
    noStroke();
    ellipse(0,0,r*2,r*2);
  
      // We must keep track of our position along the curve
  float arclength = 0;

  // For every box
  for (int i = 0; i < status.getText().length(); i++)
  {
    // Instead of a constant width, we check the width of each character.
    char currentChar = status.getText().charAt(i);
    float w = textWidth(currentChar);

    // Each box is centered so we move half the width
    arclength += w/2;
    // Angle in radians is the arclength divided by the radius
    // Starting on the left side of the circle by adding PI
    float theta = PI + arclength / r;  

    pushMatrix();
    // Polar to cartesian coordinate conversion
    translate(r*cos(theta), r*sin(theta));
    // Rotate the box
    rotate(theta+PI/2); // rotation is offset by 90 degrees
    // Display the character
    fill(0);
    text(currentChar,0,0);
    popMatrix();
    // Move halfway again
    arclength += w/2;
  }
  
}

No comments:

Post a Comment