Code

//Note: After much time spent on trying to get this to work, I had to stop
//I can't figure out why I keep getting a null pointer exception and can only assume
//that somewhere I haven't looked, one of my values are null when they shouldn't 
//Note2: My backspace function works except that, 
//it does not remove the last letter, but instead replaces the last
//letter with the next inputted letter
//Note3: if for some reason you wanted to use all caps,
//the search query would not fit in the rectangle, but then again
//I'm not sure if caps work or not since a println results in ?X
//(where X is the capital letter), rather than X
//in addition spaces can be typed but I doubt they work with Twitter

// THE CONCEPT:
// TYPE A WORD, SEARCH FOR CURRENT TWEETS WITH THAT WORD.
PFont myfont;
int letterCount = 0;
char letterArray[] = new char[20];
String query = " ";
String queryUrl = " ";
String result[];
String finresult[];
boolean z = false;
//----------------
void keyPressed(){
  if (key == 10){ // the Enter key!
    doTwitterSearchFromLetterArray();
    z = true;
  } 
  else if (key == 8){
    if (letterCount > 0){
      int lc = letterCount;
      query = query.substring(0,lc);
      letterCount = query.length()-1;
      for (int i = query.length()-1; i < letterArray.length; i++){
        if (i >= 1)
          letterArray[i] = ' ';
      }
    }
  }
  else {
    if (letterCount < 20){
      letterArray[letterCount] = key;
      query = query.valueOf(letterArray);
      letterCount++;
    }
    else if (letterCount == 20){
      letterCount = 0;
      for (int i = 0; i < letterArray.length; i++){
        letterArray[i] = ' ';
      }
    }
  }
}

//----------------
void setup(){
  size (640, 480);
  myfont = loadFont("TimesNewRomanPSMT-24.vlw");
  textFont(myfont);
}

//----------------
void draw(){
  background (100);
  fill (200, 200);
  rect (45,30, 250, 30);
  fill (255);
  text (query, 50, 50);
  if (z == true){
    for (int a = 0; a < 11; a++){
      text (finresult[a], 50, 100 + a*20);
    }
  }
}

//----------------
void doTwitterSearchFromLetterArray(){
  // Using the letterArray, create a String similar to the following
  // (but instead of "purple", use the search typed in by the user).
  // String queryUrl = "http://search.twitter.com/search.rss?rpp=100&q=purple";
  // Note: the "rpp=100" portion allows you to specify that you want up to 100 results.
  // ...
  query = query.trim();
  //println (query);
  queryUrl = "http://search.twitter.com/search.rss?rpp=100&q=" + query;
  modifiedex70x();

}

//--------------
void modifiedex70x(){
  String[] strArray = loadStrings(queryUrl);
  String result[] = new String[strArray.length];
  int n = 0;

  for (int i = 0; i < strArray.length; i++) {
    if (result[i] != null){  
      result[i] = result[i].trim();
      if (result[i].startsWith("<title>")) {
        String pref = "<title>";
        String suf = "</title>";
        int preflen = pref.length();
        int suflen = suf.length();
        if (n < 11) {
          finresult[n] = result[i].substring(preflen, result[i].length()-suflen);
          n++;
        }
      }
    }
  }
}

0704 - Twitter Freestyle: Scraping custom terms

Statement:At the Twitter API page, they provide commands that allow you to search for recent postings containing any word of interest. For example, copy the following URL into a browser: http://search.twitter.com/search.rss?q=miserable. You will receive a page back with the most recent Tweets containing the word "miserable". If you view the source of this web page, you will find it is easy to identify the lines that contain the actual posted text.

For this assignment, you will make a program which fetches Twitter postings in response to custom queries from user input. The code below is the basis for such a program. It collects keypresses into an array of characters. When the user presses the Enter key (which is handled for you in the code below), your program should launch a process that uses the array of characters as the basis for a custom Twitter search.

Refer once again to the Java String API. You will need to use one of the special String constructors to create a String from the array of keypress chars.
Some stuff your program should do:

  1. Use some typography to display the word currently being typed by the user.
  2. Use some typography to display some of the Tweets that are returned from Twitter.
  3. Your program should handle the possibility that no results are returned. For example, a nonsense search like "http://search.twitter.com/search.rss?q=sdflkj" may produce no results.
  4. Be aware that your applet will not run in a browser, due to Java security restrictions. Upload your code and a screenshot of your working program instead.

// THE CONCEPT:
// TYPE A WORD, SEARCH FOR CURRENT TWEETS WITH THAT WORD.

int letterCount = 0;
char letterArray[] = new char[20];

//----------------
void keyPressed(){
  if (key == 10){ // the Enter key!
    doTwitterSearchFromLetterArray();
  } 
  else {
    if (letterCount < 20){
      letterArray[letterCount] = key;
      letterCount++;
    }
  }
}

//----------------
void setup(){
  // ...
}

//----------------
void draw(){
  // ...
}

//----------------
void doTwitterSearchFromLetterArray(){
  // Using the letterArray, create a String similar to the following
  // (but instead of "purple", use the search typed in by the user).
  // String queryUrl = "http://search.twitter.com/search.rss?rpp=100&q=purple";
  // Note: the "rpp=100" portion allows you to specify that you want up to 100 results.
  // ...
}

hide statement