Advertisement
Guest User

Tweet visualisation in Processing

a guest
May 12th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import twitter4j.conf.*; //http://twitter4j.org/javadoc/index.html
  2. import twitter4j.*;
  3. import twitter4j.auth.*;
  4. import twitter4j.api.*;
  5. import java.util.*;
  6.  
  7. Twitter twitter;
  8. String searchString = "geug14"; //Change this to what you want to search for.
  9. List<Status> tweets;
  10. int currentTweet;
  11. float previous_w = 300;
  12. float previous_h=300;
  13. PFont myFont;
  14.  
  15. void setup(){
  16.   smooth();
  17.   size(1800,600);
  18.   float brushSize = 0;
  19.   float targetBrushSize = 0;
  20.   myFont = createFont("GillSans-48.vlw", 24);
  21.   textFont(myFont);
  22.  
  23.   // Go to developer.twitter.com and get your application keys
  24.   ConfigurationBuilder cb = new ConfigurationBuilder();
  25.   cb.setOAuthConsumerKey("YOURS_GOES_HERE);
  26.  cb.setOAuthConsumerSecret("YOURS_GOES_HERE");
  27.  cb.setOAuthAccessToken("YOURS_GOES_HERE");
  28.  cb.setOAuthAccessTokenSecret("YOURS_GOES_HERE");
  29.  
  30.    TwitterFactory tf = new TwitterFactory(cb.build());
  31.    twitter = tf.getInstance();
  32.    getNewTweets();
  33.    currentTweet = 0;
  34.    thread("refreshTweets");
  35. }
  36.  
  37. void draw(){
  38.    //Draw the line
  39.    //background(#FA6508);
  40.    smooth();
  41.    fill(33 );
  42.   stroke( #12FA08 );
  43.    strokeWeight(20);
  44.    
  45.    float w = random(width);
  46.    float h = random(height);
  47.    
  48.    line(previous_w,previous_h, w, h);
  49.    delay(5000);
  50.    
  51.    fill(0, 40);
  52.    rect(0, 0, width, height);
  53.  
  54.    currentTweet = currentTweet + 1;
  55.  
  56.    if (currentTweet >= tweets.size()){
  57.        currentTweet = 0;
  58.    }
  59.  
  60.    Status status = tweets.get(currentTweet);
  61.    User user = status.getUser();
  62.    PImage img =  loadImage(user.getBiggerProfileImageURL());
  63.      
  64.    fill(200);
  65.    image(img, w-80,h);
  66.    text(status.getText(),w ,h , 500, 400);
  67.    
  68.    //translate(w,h);
  69.    previous_w = w;
  70.    previous_h = h;
  71. }
  72.  
  73. void getNewTweets(){
  74.    try {
  75.        Query query = new Query(searchString);
  76.        QueryResult result = twitter.search(query);
  77.        tweets = result.getTweets();
  78.    } catch (TwitterException te) {
  79.        System.out.println("Failed to search tweets: " + te.getMessage());
  80.        System.exit(-1);
  81.    }
  82. }
  83.  
  84. void refreshTweets(){
  85.    while (true){
  86.        getNewTweets();
  87.        println("Updated Tweets");
  88.        delay(30000);
  89.    }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement