[twitter4j] storing data retrieved from TwitterStream

edited December 2015 in Library Questions

Hi all

So I'm messing around with the stream API and hit a wall. I used the algorithm that has been spread through the forum about retrieving data from a stream search. It worked fine when prompting the data, but i wanna store and visualize the tweets live. For that i adapted an object that i created for the REST API that have a template for how i wanna the info displayed. I created an ArrayList that stores the Objects, but looks like i'm having trouble in adding that object to ArrayList, and also having trouble displaying the object. I created a function called parseStatus just to parse the data and create the Object.

here's the code import twitter4j.util.*; import twitter4j.*; import twitter4j.management.*; import twitter4j.api.*; import twitter4j.conf.*; import twitter4j.json.*; import twitter4j.auth.*; import java.util.*; import java.io.*; import java.util.List; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date;

TwitterStream tS;

ArrayList<Tweet> tuit=new ArrayList();

String keywords[]= {
  "hangover"
};

PFont font, fontBold;
PFont caption, captionBold;

//gobal variables
int currTweet=0;

void setup() {
  size(1280, 720);
  background(64, 153, 255);

  font=createFont("Dosis", 16);
  fontBold=createFont("Dosis-Bold", 16);
  caption=createFont("InputSansCondensed-Italic", 20);
  captionBold=createFont("InputSansCondensed-BoldItalic", 20);
  textFont(font);

  openTwitterStream();
}

void draw() {
  background(64, 153, 255);

  if (tuit.size() > 0) {
    Tweet t=tuit.get(currTweet-1);
    t.display();
  }
}

void openTwitterStream() {
  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey("XXX");
  cb.setOAuthConsumerSecret("XXX");
  cb.setOAuthAccessToken("XXX");
  cb.setOAuthAccessTokenSecret("XXX");
  cb.setDebugEnabled(true);
  cb.setJSONStoreEnabled(true);

  tS=new TwitterStreamFactory(cb.build()).getInstance();

  FilterQuery filtered=new FilterQuery();

  filtered.track(keywords);

  tS.addListener(listener);

  if (keywords.length==0) {
    tS.sample();
  } else {
    tS.filter(filtered);
  }
  println("connected");
}

//implement StatusListener interface
StatusListener listener=new StatusListener() {

  public void onStatus(Status status) {
    parseStatus(status);
    System.out.println("@"+status.getUser().getScreenName()+" - "+status.getText());
  }
  //@Override
  public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
    System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
  }

  //@Override
  public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
    System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
  }

  //@Override
  public void onScrubGeo(long userId, long upToStatusId) {
    System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
  }

  //@Override
  public void onStallWarning(StallWarning warning) {
    System.out.println("Got stall warning:" + warning);
  }

  //@Override
  public void onException(Exception ex) {
    ex.printStackTrace();
  }
};

void parseStatus(Status s) {
  String u=s.getUser().getScreenName();
  String m=s.getText();
  String loc=s.getUser().getLocation();
  GeoLocation geo=s.getGeoLocation();
  double lat, lon;
  if (geo != null) {
    lat=geo.getLatitude();
    lon=geo.getLongitude();
  } else {
    lat=1;
    lon=1;
  }
  Date d=s.getCreatedAt();
  SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
  String dt=sdf.format(d);
  String headShot=s.getUser().getOriginalProfileImageURL();
  PImage hShot=loadImage(headShot);
  float x=100;
  float y=100;
  float tW=textWidth(u)+10;

  tuit.add(new Tweet(x, y, tW, u, dt, m, loc, lat, lon, hShot));

  currTweet++;
  println("tweet added: "+currTweet);
  println("Tweet size: "+tuit.size());
}

and the object code

class Tweet {

  float x, y, tW;
  float mW, mH, bW;      //message width, messageHeight
  double lat, lon;
  String user, date, msg, id, loc;
  PImage img;

  Tweet(float _x, float _y, float _tW, String _user, String _date, String _msg, String _loc, double _lat, double _lon, PImage _img) {
    x=_x;
    y=_y;
    tW=_tW;
    user=_user;
    date=_date;
    msg=_msg;
    //id=_id;
    img=_img;
    loc=_loc;
    lat=_lat;
    lon=_lon;
    bW=width/6;
    x=x*bW+5;
  }

  void recInit() {
    mW=textWidth(msg);
    mH=floor(mW/bW)+3;
  }

  boolean isDead() {
    if (frameCount%150==0) {
      return true;
    } else {
      return false;
    }
  }

  void display() {
   // recInit();
    pushMatrix();
    translate(x, y);
    noStroke();
    //quad
    fill(#00CCFF);
    rect(-10, -60, 8, mH*20);
    //id
    //fill(0);
    //float idW=textWidth(id);
    //rect(-idW,-12,idW,12);
    //fill(255);
    //textFont(fontBold);
    //textSize(12);
    //textAlign(RIGHT);
    //text(id,-3,0);
    //textAlign(LEFT);
    //headShot
    image(img, 0, -60, 40, 40);
    //username
    fill(200);
    text(user, 0, 0);
    //data
    pushMatrix();
    fill(200, 0, 0);    
    textAlign(RIGHT);
    text(date, bW, 0);
    popMatrix();
    //location
    fill(200);
    textFont(font);
    textSize(12);
    textAlign(LEFT);
    text("geo:"+loc, 0, 14);
    text("lat:"+lat, 0, 28);
    text("lon:"+lon, 150, 28);
    //message
    textSize(16);
    textFont(font);
    textLeading(18);
    text(msg, 0, 38, bW, 300);
    popMatrix();
  }
}

Answers

  • What's not working?

    note that you have published your OAuth data.

  • it's not showing anything in the draw. i had some debugging functions in the parseStatus and they all worked, but the object is not displaying anything.

Sign In or Register to comment.