• fullscreen
  • ArticleCirc.pde
  • DateButton.pde
  • NYT_rollColorFullDate1.pde
  • Offset.pde
  • Request1.pde
  • Request2.pde
  • Request3.pde
  • class ArticleCirc { // give Classes names that begin with a capital letter to differentiate
      float x1;
      float y1;
      int maxdiam;
      boolean mouse;
      float r1;
      float g1;
      float b1;
      int md;
      String title = "";
    
      ArticleCirc(int maxdiam_, float rr, float gg, float bb) { 
        x1 = width/2;
        y1 = height/2-60;
        maxdiam = maxdiam_;
        mouse = false;
        r1 = rr;
        g1 = gg;
        b1 = bb;
      }
    
      void display(int i) {
        pushMatrix();
        translate(x1,y1);
        md = maxdiam - i;
        int d = md + 8*md;
        if (mouse) { 
          //      noFill();
          stroke(255,25);
          strokeWeight(.02);
          //      stroke(255,0,0);
          float r2 = r1+d/10;
          float g2 = g1+d/10;
          float b2 = b1+d/10;
          fill(r2,g2,b2);
        }  
        else {
          stroke(255,10);
          strokeWeight(.02);
          float r3 = r1+d/8;
          float g3 = g1+d/8;
          float b3 = b1+d/8;
          fill(r3,g3,b3);
        }
        ellipse(0,0,d,d);
        popMatrix();
      }
    
    
      void rollText(int i) {
        //    if(textBox) {
        //    }
        md = maxdiam - i;
        int d = md + 8*md;
        if (mouse) { 
          textFont(f2, 22);
          fill(255);
          textAlign(LEFT);
          if (titleArray[i] != null) text(titleArray[i],35,height-110);
          textFont(f2, 16);
          fill(255,150);
          textAlign(LEFT);
          textLeading(22);
          if (bodyArray[i] != null) text(bodyArray[i],35,height-95, 850, 90);
          textFont(f, 10);
          fill(0,150);
          if (titleArray[i] != null) {  
            float top = y1-md*3.25; 
            float left = x1-md*3.5;
            float topScreen = constrain(top, 20,600); 
            if (topScreen == 20) left = (left-8);
            text("article: "+md,left,topScreen);
          }
          println(titleArray[i] +" "+ i);
          println(bodyArray[i] +" "+ i);
        }
      }
    
    
      void boxText(String sWord1, String sWord2) {
        if (clear && offsetCount>1) {
          adTotal = 0;
          xx = 0;
          fill(255);
          rect(0,0,width,height);
          redraw();
        }
        noStroke();
        fill(0,220);
        rect(0,height-150,width,height);
        textFont(f, 28);
        fill(0,200);
        textAlign(RIGHT);
        text(tt2,170,height/2-82);
        textFont(f, 13);
        text("occurences",170,height/2-67);
        text("of the word",170,height/2-51);
        text("\""+sWord1+"\"",170,height/2-35);
    
        textFont(f, 28);
        fill(0,200);
        textAlign(LEFT);
        text(tt3,830,height/2-82);
        textFont(f, 13);
        text("occurences",830,height/2-67);
        text("of the word",830,height/2-51);
        text("\""+sWord2+"\"",830,height/2-35);
    
        imageMode(CENTER);
        image(myImage, width/2,35); 
        textFont(f, 18);
        fill(0);
        textAlign(CENTER);
        text("KEYWORD CROSS SECTION",width/2,80);
    
        fill(0,20);
        noStroke();
        //    ellipse(140,height/2-65,140,140);
        //    ellipse(860,height/2-65,140,140);
        //    arc(170, height/2-68, 70, 70, PI+HALF_PI, TWO_PI+HALF_PI);
        //    arc(830, height/2-68, 70, 70, HALF_PI, PI+HALF_PI);
      }
    
    
      void rollover(int i) { 
        md = maxdiam - i;
        int dia = md +8*md;
        float mouseDist= dist(mouseX,mouseY,x1,y1);
        if (mouseDist>=dia/2.0-2 && mouseDist<=dia/2.0+2) {
          mouse = true;
        } 
        else {
          mouse = false;
        }
      }
    
    
      void textDisplay() { 
        noFill();
        strokeWeight(.02);
        stroke(200);
        int dee = 50 +8*50;
        int ree = 100 +8*100;
        ellipse(x1,y1,dee,dee);
        ellipse(x1,y1,ree,ree);
    
        textFont(f, 120);
        fill(0,30);
        textAlign(CENTER);
        text(total,x1,height/2-20);
        textFont(f, 25);
        text("cross section",x1,height/2+5);
        text("matches",width/2,height/2+30);
      }
    }// end class
    
    
    class DateButton { // give Classes names that begin with a capital letter to differentiate
      int x;
      int y;
      int state = 0;
      int rollFill = 60;
      String date = "";
      String yyear = "";
    
      DateButton(int xx, int yy, String dateY) { 
        x = xx;
        y = yy;
        date = dateY;
      }
    
      void display() {
        String yyear = "2009";
        noStroke();
        fill(rollFill);
        rectMode(CORNER);
        rect(465,87,70,15);
        rect(x,y,70,16);
        textFont(f, 12);
        fill(255);
        textAlign(CENTER);
        text("< "+yyear+" >",465+35,87+12);
        text("< "+date+" >",x+35,y+12);
        if (mouseX>465 && mouseX<465+70 && mouseY>87 && mouseY<87+15) {
          rollFill = 100;
        }
        else {
          rollFill = 60;
          if (mouseX>x && mouseX<x+70 && mouseY>y && mouseY<y+15) {
            rollFill = 100;
          }
          else {
            rollFill = 60;
          }
          if (mousePressed) { 
            if (mouseX>x && mouseX<x+70 && mouseY>y && mouseY<y+15) {
              dateClick = !dateClick;
              yyear = date;
            }
          }
        }
        if (mousePressed) { 
          if (mouseX>465 && mouseX<465+70 && mouseY>87 && mouseY<87+15) {
            dateClick = !dateClick;
          }
        }
      }
    
      //  void select() {
    }
    
    
    /*
    NYTimes Keyword Cross section
    ITP Fall 2010
    Che-Wei Wang's Intro to Computational Media:
    Uses the NYTimes API, JSON, and interfascia
    Special thanks to Che-Wei, Steve Klise, and the ITP residents
    Works once around for now. I will make it work - update coming soon!
    */
    
    import org.json.*;//import JSON library
    import interfascia.*;
    PImage myImage;
    boolean dateClick = false;
    boolean fuck = false;
    
    GUIController c1;
    IFTextField t1;
    IFLabel l1;
    String txt1="";
    GUIController c2;
    IFTextField t2;
    IFLabel l2;
    String txt2="";
    
    PFont f;
    PFont f2;
    boolean clear;
    
    String apiKey = "8258e5cc834626de062eafe00dd656c4:10:61866308"; //my NYT API key code
    String baseURL = "http://api.nytimes.com/svc/search/v1/article";
    
    DateButton dMain;
    DateButton dMain1;
    DateButton dMain2;
    DateButton dMain3;
    DateButton dMain4;
    DateButton dMain5;
    DateButton dMain6;
    DateButton dMain7;
    DateButton dMain8;
    DateButton dMain9;
    DateButton dMain10;
    
    
    String [] titleArray;
    String [] bodyArray;
    int total;
    int total2;
    int total3;
    String title;
    int indent = 25;
    int lead = 36;
    boolean go;
    boolean textBox;
    int tt;
    int tt2;
    int tt3;
    int xx;
    int adTotal;
    String startD = "2008";
    String endD = "2009";
    int offsetCount;
    float r = random(10,180);
    float g = random(10,50);
    float b = random(10,120);
    ArticleCirc[] AC1 = new ArticleCirc[adTotal];
    ArticleCirc[] AC2 = new ArticleCirc[1];
    Request1 Rq1;
    Offset [] Off1 = new Offset[xx];
    Request2 Rq2;
    Request3 Rq3;
    
    void setup() {
      //    size(screen.width,screen.height);
      size(1000,675);
      //  smooth();
      f = createFont("DIN-Medium",22,true);
      f2 = createFont("Baskerville", 10, true);
    
      for(int i = 0; i < adTotal; i++) {
        AC1[i] = new ArticleCirc(tt,r,g,b);
      }
      for(int i = 0; i < 1; i++) {
        AC2[i] = new ArticleCirc(tt,r,g,b);
      }
    
      for(int i = 0; i < xx; i++) {
        Off1[i] = new Offset(txt1, txt2, startD, endD);
      }
      clear = false;
    
      c1 = new GUIController(this);
      t1 = new IFTextField("Text Field", (width/2)-320, (height/2)-75, 150);
      c1.add(t1);
      t1.addActionListener(this);
    
      c2 = new GUIController(this);
      t2 = new IFTextField("Text Field", (width/2)+170, (height/2)-75, 150);
      c2.add(t2);
      t2.addActionListener(this);
    
      myImage=loadImage("nytLogo.jpg");
      dMain = new DateButton(-100,-100,"year");
      dMain1 = new DateButton(385,87,"2000");
      dMain2 = new DateButton(305,87,"2001");
      dMain3 = new DateButton(225,87,"2002");
      dMain4 = new DateButton(145,87,"2003");
      dMain5 = new DateButton(65,87,"2004");
      dMain6 = new DateButton(545,87,"2005");
      dMain7 = new DateButton(625,87,"2006");
      dMain8 = new DateButton(705,87,"2007");
      dMain9 = new DateButton(785,87,"2008");
      dMain10 = new DateButton(865,87,"2009");
    }
    
    void draw() {
      background(255);
    
      dMain.display();
      if (dateClick){
      dMain1.display();
      dMain2.display();
      dMain3.display();
      dMain4.display();
      dMain5.display();
      dMain6.display();
      dMain7.display();
      dMain8.display();
      dMain9.display();
      dMain10.display();
      }
      imageMode(CENTER);
      image(myImage, width/2,35); 
      textFont(f, 18);
      fill(0);
      textAlign(CENTER);
      text("KEYWORD CROSS SECTION",width/2,80);
      noStroke();
      fill(0,220);
      rect(0,height-150,width,height);
      textFont(f, 16);
      fill(0,200);
      textAlign(CENTER);
      text("<-- click and type a word into each field -->",width/2,height/2-60);
      text("then press return",width/2,height/2-40);
    
      if(clear)
      {
        //    fill(255);
        //    rect(0,60,width,height-60);
        //    clear = false;
      }
      if(go) {
        fill(255);
        rect(0,0,width,height);
        for(int i = 0; i < 1; i++) {
          Off1[i].getTitles();
        }
      }
      for(int i = 0; i < adTotal; i++) {
        AC1[i].display(i);
        AC1[i].rollover(i);
        AC1[1].textDisplay();
      }
      for(int i = 0; i < 1; i++) {
        if(go) {
          AC2[i].boxText(txt1,txt2);
        }
      }
      for(int i = 0; i < adTotal; i++) {
        AC1[i].rollText(i);
      }
    }
    
    void actionPerformed(GUIEvent e) {
      if (e.getMessage().equals("Completed")) {
        txt1 = t1.getValue();
        txt2 = t2.getValue();
      }
    }
    
    
    void keyPressed() {
      // If the return key is pressed, save the String and clear it
      if (key == '\n' ) {
        if (go && offsetCount>1) {
          clear = !clear;
        }
         
        Rq1 = new Request1(txt1, txt2,startD,endD); 
        Rq1.getCount();
        Rq2 = new Request2(txt1,startD,endD); 
        Rq2.getCount();
        Rq3 = new Request3(txt2,startD,endD); 
        Rq3.getCount();
    
        adTotal = constrain(tt,0,150);
        if (adTotal < 12) {
          xx = 2;
        } 
        else {
          xx = ceil(adTotal/9);
        }
    
        go = true;
        background(255);
        println(tt);
    
        for(int i = 0; i < xx; i++) {
          Offset tempOff = new Offset(txt1, txt2, startD, endD);  
          Off1 = (Offset[])append ( Off1, tempOff);
        }
    
        for(int i = 0; i < adTotal; i++) {
          ArticleCirc tempAC = new ArticleCirc(adTotal,r,g,b);  
          AC1 = (ArticleCirc[])append ( AC1, tempAC);
        }   
        if (go) {
        } 
        titleArray = new String [adTotal];
        bodyArray = new String [adTotal];
      }
    }
    
    
    
    class Offset {
      String sWord1; 
      String sWord2; 
      String bDate; 
      String eDate;
      boolean offBalance = true;
    
      Offset(String searchWord1, String searchWord2, String beginDate, String endDate) {
        sWord1 = searchWord1; 
        sWord2 = searchWord2; 
        bDate = beginDate; 
        eDate = endDate;
      }
    
      void getTitles() { //Method (function) to retrieve articles
        if (offBalance == true) {
          String request = baseURL + "?query="+sWord1+","+sWord2+"&begin_date="+bDate+"0101&end_date="+eDate+"1231&offset="+offsetCount+"&api-key="+apiKey;
          String result = join(loadStrings( request ),"");
          try {
            JSONObject nytData = new JSONObject(join(loadStrings(request),""));
            JSONArray results = nytData.getJSONArray("results");
            for(int j=0;j<results.length(); j++) {
              JSONObject resultsTitle = results.getJSONObject(j);
    //          JSONObject resultsBody = results.getJSONObject(j);
              println(resultsTitle.getString("title"));
              println(resultsTitle.getString("body"));
              titleArray [offsetCount*10+j] = resultsTitle.getString("title");
              bodyArray [offsetCount*10+j] = resultsTitle.getString("body");
            }
          }
          catch(JSONException e) {
            println( "There was an error parsing the JSONObject nytData." );
          }
          offsetCount++;
          println(offsetCount);
        }
        if (offsetCount == xx) offBalance = false;
        if (offsetCount == 15) offBalance = false;
      }// END getTitles function
    }
    
    
    class Request1 {
      String sWord1; 
      String sWord2; 
      String bDate; 
      String eDate;
      int offsetCount;
    
      Request1(String searchWord1, String searchWord2, String beginDate, String endDate) {
        sWord1 = searchWord1; 
        sWord2 = searchWord2; 
        bDate = beginDate; 
        eDate = endDate;
        offsetCount = 0;
      }
    
    
      int getCount() { //Method (function) to retrieve articles
        String request = baseURL + "?query="+sWord1+","+sWord2+"&begin_date="+bDate+"0101&end_date="+eDate+"1231&offset="+offsetCount+"&api-key="+apiKey;
        String result = join(loadStrings( request ),"");
        //    int total = 0;
        //wrap it in a try/catch statement so we catch an exception if something goes wrong
        try {
          JSONObject nytData = new JSONObject(join(loadStrings(request),""));
          JSONArray results = nytData.getJSONArray("results");
          total = nytData.getInt("total");
          tt=total;
    
          //    JSONObject temp = results.getJSONObject(0);
          println( "There were " + total + " occurences of "+sWord1+ " and " +sWord2 + " between "+bDate+" & "+eDate);
        }
        catch(JSONException e) {
          println( "There was an error parsing the JSONObject nytData." );
        }  
        return(total); //the int called for at the beginning of the function
      }// END getCount function
    }
    
    
    class Request2 {
      String sWord1; 
      String bDate; 
      String eDate;
    
      Request2(String searchWord1, String beginDate, String endDate) {
        sWord1 = searchWord1; 
        bDate = beginDate; 
        eDate = endDate;
      }
    
    
        int getCount() { //Method (function) to retrieve articles
          String request = baseURL + "?query="+sWord1+"&facets=geo_facet"+"&begin_date="+bDate+"0101&end_date="+eDate+ "0101&api-key=" + apiKey;
          String result = join(loadStrings( request ),"");
          //    int total = 0;
          //wrap it in a try/catch statement so we catch an exception if something goes wrong
          try {
            JSONObject nytData = new JSONObject(join(loadStrings(request),""));
            JSONArray results = nytData.getJSONArray("results");
            total2 = nytData.getInt("total");
            tt2=total2;
            //    JSONObject temp = results.getJSONObject(0);
            println( "There were " + tt2 + " occurences of "+sWord1+ " between "+bDate+" & "+eDate);
            }
          catch(JSONException e) {
            println( "There was an error parsing the JSONObject nytData." );
          }  
          return(tt2); //the int called for at the beginning of the function
        }// END getArticles function
      
    }
    
    
    class Request3 {
      String sWord2; 
      String bDate; 
      String eDate;
    
      Request3(String searchWord2, String beginDate, String endDate) {
        sWord2 = searchWord2; 
        bDate = beginDate; 
        eDate = endDate;
      }
    
    
        int getCount() { //Method (function) to retrieve articles
          String request = baseURL + "?query="+sWord2+"&facets=geo_facet"+"&begin_date="+bDate+"0101&end_date="+eDate+ "0101&api-key=" + apiKey;
          String result = join(loadStrings( request ),"");
          //    int total = 0;
          //wrap it in a try/catch statement so we catch an exception if something goes wrong
          try {
            JSONObject nytData = new JSONObject(join(loadStrings(request),""));
            JSONArray results = nytData.getJSONArray("results");
            total3 = nytData.getInt("total");
            tt3=total3;
            //    JSONObject temp = results.getJSONObject(0);
            println( "There were " + tt3 + " occurences of "+sWord2+ " between "+bDate+" & "+eDate);
            }
          catch(JSONException e) {
            println( "There was an error parsing the JSONObject nytData." );
          }  
          return(tt2); //the int called for at the beginning of the function
        }// END getArticles function
      
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Jeremy

    NYTimes Keyword Cross Section

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    NYTimes Keyword Cross section ::ONLY KINDA WORKING::
    ITP Fall 2010
    Che-Wei Wang's Intro to Computational Media:
    Uses the NYTimes API, JSON, and interfascia
    Special thanks to Che-Wei, Steve Klise, and the ITP residents
    Works once around for now. I will make it work - update coming soon!

    You need to login/register to comment.