random text array and updating the setup
in
Contributed Library Questions
•
2 years ago
I'm making a twitter script but I need it to randomly select a string and search it. The problem is that the search is in the setup. Is there a way to make the setup update? so that the search reloads a different keyword. I've attached my script. The important stuff is in the feeder creator.
I've tried placing the feeder into an void update(), but the i can't get at the information in the draw().
- import net.obxlabs.romefeeder.*; //romefeeder
- import com.sun.syndication.feed.synd.*; //ROME
- import processing.serial.*;
- Serial myPort;
- TwitterFeeder feeder; // the feeder
- boolean loaded = false; // flag that indicates if the search is loaded
- int feedRate = 7*1000; // rate for displaying posts (in miliseconds)
- int feedLast = 0; // time of the last displayed post
- SyndEntry entry; // feed entry
- TwitterFeeder feeder2; // the feeder
- SyndEntry entry2; // feed entry
- PFont font; // the font
- static final int WORD_FONTSIZE = 128; //self size
- static final int TEXT_FONTSIZE = 24; //rest of text size
- static final int TEXT_WIDTH = 400; //text width (in characters)
- int chooser;
- String textmark;
- String a ;
- //search query
- String QUERY = "protestors";
- String QUERY2 = "Gadhafi";
- String topString = ""; //string of text that appears before 'self' in the tweet
- String bottomString = ""; //string of text that appears after 'self' in the tweet
- void setup() {
- //arduino connection///////////////////////////////////////////////////////////////////////////////////
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[1], 9600);
- myPort.buffer(1);
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- size(600, 300);
- frameRate(30);
- smooth();
- //create and set the font
- font = createFont("bluehigh.ttf", 12, true);
- //add a loading prompt
- textFont(font, 24);
- text("Loading Tweets...", width/2, height/2);
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///FEEDER CREATIONS
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //create the feeder
- feeder = new TwitterFeeder();
- feeder.verbose = true;
- feeder.sort(Feeder.PUBLISHED_DATE);
- feeder.setLoadFeedCallback(
- new FeedCallback() { public void event(SyndFeed feed) { postLoadFeed(feed); } }
- );
- feeder.search(QUERY);
- feeder.setUpdateInterval(7*1000); // milliseconds
- feeder.startUpdate();
- //create the feeder2
- feeder2 = new TwitterFeeder();
- feeder2.verbose = true;
- feeder2.sort(Feeder.PUBLISHED_DATE);
- feeder2.setLoadFeedCallback(
- new FeedCallback() { public void event(SyndFeed feed) { postLoadFeed(feed); } }
- );
- feeder2.search(QUERY2);
- feeder2.setUpdateInterval(7*1000); // milliseconds
- feeder2.startUpdate();
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///END FEEDER CREATIONS
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- }
- //flag when the search is done loading
- void postLoadFeed(SyndFeed feed) {
- //flag as loaded
- loaded = true;
- }
- void draw() {
- //if the search is not done loading then wait
- if (!loaded) return;
- // String a = names[int(random(names.length))];
- // println(a);
- chooser= int(random(0,2));
- // println(chooser);
- switch(chooser){
- case 0:
- if ((feeder.hasNext()) && (millis()-feedLast >= feedRate)) {
- //get the next entry
- entry = feeder.next();
- //parse the entry title as text
- parseEntry(entry.getTitle(),QUERY);
- //update the feed timer
- feedLast = millis();
- myPort.write(0);
- background(0);
- //draw the rest of the tweet
- textFont(font, TEXT_FONTSIZE);
- fill(255);
- stroke(0);
- textAlign(LEFT, BOTTOM);
- text(topString, 205, -85, 350, 200);
- textAlign(RIGHT, TOP);
- text(bottomString, 50, height/2 + 54, 350, 190);
- //draw the searched text
- fill(255);
- stroke(0);
- textFont(font, WORD_FONTSIZE);
- textAlign(CENTER, CENTER);
- text(QUERY.toUpperCase(), width/2, height/2);
- break;
- }
- case 1:
- if ((feeder2.hasNext()) && (millis()-feedLast >= feedRate)) {
- //get the next entry
- entry2 = feeder2.next();
- //parse the entry title as text
- parseEntry(entry2.getTitle(),QUERY2);
- //update the feed timer
- feedLast = millis();
- myPort.write(1);
- background(0);
- //draw the rest of the tweet
- textFont(font, TEXT_FONTSIZE);
- fill(255);
- stroke(0);
- textAlign(LEFT, BOTTOM);
- text(topString, 205, -85, 350, 200);
- textAlign(RIGHT, TOP);
- text(bottomString, 50, height/2 + 54, 350, 190);
- //draw the searched text
- fill(255);
- stroke(0);
- textFont(font, WORD_FONTSIZE);
- textAlign(CENTER, CENTER);
- text(QUERY2.toUpperCase(), width/2, height/2);
- break;
- }
- }
- }
- //parses the tweet and stores the to strings
- //that are displayed above and below the word 'self'.
- void parseEntry(String title, String question) {
- //make sure the magic word is there.
- if (title.toLowerCase().indexOf(question) == -1) return;
- //get the strings before and after the magic word
- topString = title.substring(0, title.toLowerCase().indexOf(question));
- bottomString = title.substring(title.toLowerCase().indexOf(question) + question.length());
- //clean up
- topString = topString.trim();
- bottomString = bottomString.trim();
- }
1