ICM Net

ITP HELP | RecentChanges | Preferences

Sharing

Whether it is motivated by a drive to commune with other lonely souls, or a drive to externalize their feelings or by memes which have taken over their minds, people want to share their experience.

The loadstrings Command, HTTP connections in Processing,

The revolutionary query string

  String url = "http://xml.weather.yahoo.com/forecastrss?p=" + zip;
  String[] lines = loadStrings(url);

Text In and Text Out

A lot of the programming that people do these days involves communication between software usually over the internet. At the end of the day this is usually done by sending text strings back and forth. Learning how to put together (contatinate) and pull apart (parse) strings is very important.

Serverside Programming

Using Other People's Server Side Programming

Security Sandbox

String proxyURL  = "http://itp.nyu.edu/proxy/proxy.php?url=";
String myURL = "http://www.cnn.com";
String[] arrayOfLines = loadStrings(proxyURL  + myURL);    //combine them, basically send your url as querystring to the proxy 

Getting Image of Stocks from cnn.com (HTML)

PImage stockIndicator;
void setup(){

  String myURL = "http://marketwatch.nytimes.com/custom/nyt-com/html-usmarkets-quote.asp?sid=1643";
  String[] arrayOfLines = loadStrings(myURL);
  String oneLongString = join(arrayOfLines, "");
  //narrow in on the whole page to something close by
  int near = oneLongString.indexOf("Dow Jones Industrial Average");
  //the html comign back gets broken up in to lines but we want it in one long string, the join command does this
   String startingLandMark = "img src=";
  //String startingLandMark = "DOW</a><div class=\"cnn4pxTpad\"><img src=";  
  //this always comes before the thing I want, notice I used the escape quote \" to include a quote in the quoted
  int start = oneLongString.indexOf(startingLandMark,near) + startingLandMark.length();  
  //find the first occurance of this landmark and then march forward past the end of the landmark
  int end = oneLongString.indexOf(">",start);  
  //look for the end landmark in this case a quote,  notice I used the escape quote \" to include a quote in the quoted
  //also notice that I used a variation of indexOf that starts looking not at the beginning of the string 
  //but a  point I specify, in this case the the beginning of what I already found
  String imageURL = "http://marketwatch.nytimes.com/custom/nyt-com/" + oneLongString.substring(start+1,end-1); //+1 -1 to loose the quotation marks
 
  println(imageURL);
  stockIndicator = loadImage(imageURL);
}

void draw(){
  image(stockIndicator,width/2,height/2);
}

Getting Dog Pictures from google (HTML)

//Dano's example, with slight adjustments.
int MAX_IN_ARRAY = 1000;
PImage[] images = new PImage[MAX_IN_ARRAY];
int numberFound = 0;

void setup(){
  size(800,800);
  String proxyURL  = "http://itp.nyu.edu/icm/proxy/proxy.php?url=";
  //this is a relay station on itp that passes your request on, this allows your applet to work in a browser
  //I told you not to worry about this and just work in proccessing            
  String myURL = "http://images.google.com/images?hl=en&q=dog";
  //String[] arrayOfLines = loadStrings(myURL);
  String[] arrayOfLines = loadStrings(proxyURL  + myURL);
  String oneLongString = join(arrayOfLines, "");

  //the html comign back gets broken up in to lines but we want it in one long string, the join command does this
  String startingLandMark = ":\",\"http:" ; //"images?q=tbn:";  
  int startLookingAt = 0;  //this is like our cursor to start looking after the last place we found somthing

  boolean stopIt = false;
  while( !stopIt ){  //different kind of repeat loop that goes for an undetermined number of interations
    //this always comes before the thing I want, notice I used the escape quote \" to include a quote in the quoted
    int start = oneLongString.indexOf(startingLandMark,startLookingAt); //you want to include the landmark  so don't do this + startingLandMark.length();  
    //find the first occurance of this landmark and then march forward past the end of the landmark
    //also notice that I used a variation of indexOf that starts looking not at the beginning of the string 
    //instead at a  point I specify, in this case after what I already found

    if (start == -1) {
      //this means it hasn't found anything, so set our variable and our WHILE loop will stop
      stopIt = true;
    } else {
      start = start + startingLandMark.length() ; //jump to the end of the startlandmark
      int end = oneLongString.indexOf("\"",start  );  
      //look for the first end landmark, in this case a space, after the starting landmark
      String imageURL = "http:" +  oneLongString.substring(start,end); //add google's default path
      println("image... " + imageURL);
      images[numberFound] = loadImage(proxyURL + imageURL);
      numberFound++;
      startLookingAt = end + 1;
      if (numberFound >= MAX_IN_ARRAY ) {
        //don't overpack the array
        stopIt = true; //stops the repeat loop;
      }
    }
  }

}

void draw(){
  float angleIncrements = 2*PI/numberFound;  //angle units between each picture, in radians 2*PI = 360 degrees
  int radius = 150;
  for(int i = 0; i < numberFound; i++){
    float thisAngle = i* angleIncrements;
    float x = radius*cos(thisAngle)  + width/2;
    float y = radius*sin(thisAngle)  + height/2;
    image(images[i],x,y,50,50);
  }

}

Getting Weather from Yahoo (XML)


// Daniel Shiffman
// ICM, 2005
// Parsing Yahoo's XML Weather Feed

PFont f;

String weather = "";
String[] zips = {"10003","21209","90210"};
int temp;
int counter = 0;

void setup() {
  size(200,200);
  // Call our new getWeather function!
  getWeather(zips[counter]);
  f = loadFont("Georgia-Bold-16.vlw");
  println(temp);
}

void draw() {
  background(100);
  textFont(f);
  textMode(SCREEN);
  fill(255);
  // Display all the stuff we want to display
  text(zips[counter],10,160);
  text(weather,10,90);
  text(temp,10,40);
  text("Click to change zip.",10,180);


  // Draw a little thermometer based on the temperature
  noStroke();
  fill(200);
  rect(10,50,temp*2+random(0,5),20);

}

void mousePressed() {
  // When the mouse is clicked, increment the counter and get the weather at the next zip code
  counter = (counter + 1) % zips.length;
  getWeather(zips[counter]);
}

void getWeather(String zip) {
  //Get all the HTML/XML source code into an array of strings (each line is one element in the array)
  String url = "http://itp.nyu.edu/icm/proxy/proxy.php?url=http://xml.weather.yahoo.com/forecastrss?p=" + zip;
  String[] lines = loadStrings(url);

  String xml = join(lines, " "); // Get rid of the array and make it one very long String

  // The manual way to concatenate
  // String xml = "";
  // for (int i = 0; i < lines.length; i++) {
  //   xml += lines[i];
  // }

  // Searching for weather condition
  String lookfor = "<yweather:condition text=\"";
  String end = "\"";
  weather = giveMe(xml,lookfor,end);

  // Searching for temperature
  lookfor = "temp=\"";
  temp = int(giveMe(xml,lookfor,end));

}

// A function that returns a substring between two substrings
String giveMe(String s, String before, String after) {
  String found = "";
  int start = s.indexOf(before);  // Find the index of the beginning tag
  if (start == -1) return ""; // If we don't find anything, send back a blank String
  start += before.length(); // Move to the end of the beginning tag
  int end = s.indexOf(after,start); // Find the index of the end tag
  if (end == -1) return ""; // If we don't find the end tag, send back a blank String
  return s.substring(start,end); // Return the text in between
}



ITP HELP | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited October 3, 2007 12:45 pm (diff)
Search:
To EDIT, You have to enter an ADMINISTRATOR password (guess) in Preferences. And refresh