Twitter API Printing in Serial but not on canvas (help asap)

edited November 2017 in Arduino

Hey guys I am trying to put the filtered tweets that print to my serial port to also show up on my canvas and I am having a difficult time sorting this out. I would appreciate any help!

Answers

  • edited November 2017
    PFont f;
    boolean twitterstate = false;
    String tweet;
    
    void setup() {     
      size(500, 500);    
      background(255); 
      openTwitterStream();
      //printArray(PFont.list());
      f = createFont("Georgia", 24);
      textFont(f);
    }  
    
    
    void draw() {     
      //background(255);
      //openTwitterStream()
      //text("@" + status.getUser().getScreenName() + " - " + status.getText(), 10, 50);
      //if (twitterstate == true) {
      //text(tweet, width/2, height-200, width/2, 100);
      //}
    
    
    }  
    
    
    
    // Stream it
    void openTwitterStream() {  
    
      ConfigurationBuilder cb = new ConfigurationBuilder();  
      cb.setOAuthConsumerKey("");
      cb.setOAuthConsumerSecret("");
      cb.setOAuthAccessToken("");
      cb.setOAuthAccessTokenSecret(""); 
    
      TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    
      FilterQuery filtered = new FilterQuery();
    
      // if you enter words it will use them to filter, otherwise it will sample
      String keywords[] = {
        "sandwich"
      };
    
      filtered.track(keywords);
    
      twitterStream.addListener(listener);
    
      if (keywords.length==0) {
        // sample() method internally creates a thread which manipulates TwitterStream 
        twitterStream.sample(); // and calls these adequate listener methods continuously.
      } else { 
        twitterStream.filter(filtered);
      }
      println("connected");
    } 
    
    
    // Implementing StatusListener interface
    StatusListener listener = new StatusListener() {
    
      Status status;
      void tweet(){
      fill(0);
      text("@" + status.getUser().getScreenName() + " - " + status.getText(),CENTER,CENTER);}
    
      public void tweeter(Status status){
          status.getUser();
      }
      //@Override
       public void onStatus(Status status) {
       // background(255);
        System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        //twitterstate = true;
        //tweet = "@" + status.getUser().getScreenName() + " - " + status.getText();
        //fill(0);
        //text(tweet, width/2, height-200, width/2, CENTER);
    
      }
    
      //@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();
      }
    };
    
  • Please format your code. Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    Kf

  • edited November 2017

    Please edit your post and remove your OAuth keys from this public forum (they are supposed to be secret)

  • I figured it out on my own. If anyone else had this problem refer to this: http://www.codasign.com/tutorials/processing-and-twitter/searching-twitter-for-tweets/

  • import java.util.*;
    
    Twitter twitter;
    String searchString = "sandwich";
    List<Status> tweets;
    boolean action = false;
    int currentTweet;
    PImage like;
    PImage dislike;
    PImage leftHand;
    PImage rightHand;
    PImage sandwich;
    PImage profile;
    PImage logo;
    
    void setup()
    {
        size(1200,800);
    
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setOAuthConsumerKey("");
        cb.setOAuthConsumerSecret("");
        cb.setOAuthAccessToken("");
        cb.setOAuthAccessTokenSecret("");
    
        TwitterFactory tf = new TwitterFactory(cb.build());
    
        twitter = tf.getInstance();
    
        getNewTweets();
    
        currentTweet = 0;
    
        //thread("refreshTweets");
        like = loadImage("icons_like.png");
        dislike = loadImage("icons_dislike.png");
        leftHand = loadImage("icons_leftHand.png");
        rightHand = loadImage("icons_rightHand.png");
        sandwich = loadImage("icons_Sandwich.png");
        profile = loadImage("icons_Avatar.png");
        logo = loadImage("Twitter_Logo_WhiteOnImage.png");
    }
    
    void draw()
    {
        background(20);
        noFill();
        stroke(255);
        rect(30, 200, 1150, 200);
        fill(255);
        textSize(30);
        text("Please Like or Dislike This Tweet:", 100, 100);
    
      if (action==true){
        currentTweet = currentTweet + 1;
        action = false;
      }
        if (currentTweet >= tweets.size())
        {
            currentTweet = 0;
        }
    
        Status status = tweets.get(currentTweet);
    
        textSize(14);
        fill(255);
        text(status.getText(), 145, 300);
    
        image(sandwich, 900,50);
        image(logo, 0,40);
        image(profile,45,260);
        //fill(150);
        image(like,100,500);
        image(dislike,900,500);
        //rect(100,500,200,200);
        //rect(900,500,200,200);
    
        delay(6000);
    }
    
    void getNewTweets()
    {
        try
        {
            Query query = new Query(searchString);
    
            QueryResult result = twitter.search(query);
    
            tweets = result.getTweets();
        }
        catch (TwitterException te)
        {
            System.out.println("Failed to search tweets: " + te.getMessage());
            System.exit(-1);
        }
    }
    
  • @cvaneeden -- thank you so much for sharing your solution with the forum!!

Sign In or Register to comment.