Instagram & Processing - Real time view

edited June 2014 in Questions about Code

I'm working on a small app similar to instaprint and need some help. I'm using the source code from Globalgram by Andrew Haskin, it searches instagram for a particular hashtag and displays the most recent image posted with that hashtag. The problem is it only does it once, I need it to continuously search for the hashtag and display an image when a new one is added, so a refresh, I've been tinkering with it but to no avail. Any help would be greatly appreciated

Code Below :

import com.francisli.processing.http.*;

PFont InstagramFont;
PImage backgroundimg;
PImage brand;
PImage userphoto;
PImage profilepicture;

String username;
String tag;
String[] tagStrings;


com.francisli.processing.http.HttpClient client;

void setup() {
  size(580, 900);
  smooth();
  backgroundimg = loadImage("iso_background.jpg");
  brand = loadImage("iso.jpg");

  InstagramFont = loadFont("Helvetica-Bold-36.vlw");

  client = new com.francisli.processing.http.HttpClient(this, "api.instagram.com");
  client.useSSL = true;

  //// instantiate a new HashMap
  HashMap params = new HashMap();
  //// put key/value pairs that you want to send in the request
  params.put("access_token", "------ACCESS TOKEN HERE------");
  params.put("count", "1");
  client.GET("/v1/tags/coffee/media/recent.json", params);
}

void responseReceived(com.francisli.processing.http.HttpRequest request, com.francisli.processing.http.HttpResponse response) {
  println(response.getContentAsString());

  //// we get the server response as a JSON object
  com.francisli.processing.http.JSONObject content = response.getContentAsJSONObject();

  //// get the "data" value, which is an array
  com.francisli.processing.http.JSONObject data = content.get("data");

  //// get the first element in the array
  com.francisli.processing.http.JSONObject first = data.get(0);

  //// the "user" value is another dictionary, from which we can get the "full_name" string value
  println(first.get("user").get("full_name").stringValue());
  //// the "caption" value is another dictionary, from which we can get the "text" string value
  //println(first.get("caption").get("text").stringValue());
  //// get profile picture
  println(first.get("user").get("profile_picture").stringValue());
  //// the "images" value is another dictionary, from which we can get different image URL data
  println(first.get("images").get("standard_resolution").get("url").stringValue());

  com.francisli.processing.http.JSONObject tags = first.get("tags");
  tagStrings = new String[tags.size()];
  for (int i = 0; i < tags.size(); i++) {
    tagStrings[i] = tags.get(i).stringValue();
  }


  username = first.get("user").get("full_name").stringValue();


  String profilepicture_url = first.get("user").get("profile_picture").stringValue();
  profilepicture = loadImage(profilepicture_url, "png");

  String userphoto_url = first.get("images").get("standard_resolution").get("url").stringValue();
  userphoto = loadImage(userphoto_url, "png");

 //noLoop();
}


void draw() {
  background(255);
  imageMode(CENTER);
  image(brand, 100, height/1.05);


  if (profilepicture != null) {
    image(profilepicture, 60, 70, 90, 90);
  }
  imageMode(CENTER);
  if (userphoto != null) {
    image(userphoto, width/2, height/2.25, 550, 550);
  }

  textFont(InstagramFont, 20);
  if (username != null) {
    text(username, 110, 115);
    fill(0);
  }

   textFont(InstagramFont, 15);
  if ((tagStrings != null) && (tagStrings.length > 0)) {
    String line = tagStrings[0];
    for (int i = 1; i < tagStrings.length; i++) {
      line += ", " + tagStrings[i];
    }
    text(line, 25, 720, 550, 50);
    fill(0);
  }

}
Tagged:

Answers

  • edited July 2014

    Thanks to the help of jesses.co.tt over on stackoverflow this is now working - If anyone is interested here is the working code :

    import com.francisli.processing.http.*;
    
    PFont InstagramFont;
    PImage backgroundimg;
    PImage brand;
    PImage userphoto;
    PImage profilepicture;
    
    String username;
    String tag;
    String[] tagStrings;
    
    String comment;
    
    com.francisli.processing.http.HttpClient client;
    
    void setup() {
      size(580, 900);
      smooth();
      backgroundimg = loadImage("globalgram_background.jpg");
      brand = loadImage("iso.jpg");
    
      InstagramFont = loadFont("Helvetica-Bold-36.vlw");
    
      client = new com.francisli.processing.http.HttpClient(this, "api.instagram.com");
      client.useSSL = true;
    
    
    }
    
    void getGrams() {
        //// instantiate a new HashMap
      HashMap params = new HashMap();
      //// put key/value pairs that you want to send in the request
      params.put("access_token", "---Access Token---");
      params.put("count", "0");
      client.GET("/v1/tags/newyork/media/recent.json", params);
    }
    
    void responseReceived(com.francisli.processing.http.HttpRequest request, com.francisli.processing.http.HttpResponse response) {
      println(response.getContentAsString());
    
      //// we get the server response as a JSON object
      com.francisli.processing.http.JSONObject content = response.getContentAsJSONObject();
    
      //// get the "data" value, which is an array
      com.francisli.processing.http.JSONObject data = content.get("data");
    
      //// get the first element in the array
      com.francisli.processing.http.JSONObject first = data.get(0);
    
      //// the "user" value is another dictionary, from which we can get the "full_name" string value
      println(first.get("user").get("full_name").stringValue());
      //// the "caption" value is another dictionary, from which we can get the "text" string value
      //println(first.get("caption").get("text").stringValue());
      //// get profile picture
      println(first.get("user").get("profile_picture").stringValue());
      //// the "images" value is another dictionary, from which we can get different image URL data
      println(first.get("images").get("standard_resolution").get("url").stringValue());
    
      com.francisli.processing.http.JSONObject tags = first.get("tags");
      tagStrings = new String[tags.size()];
      for (int i = 0; i < tags.size(); i++) {
        tagStrings[i] = tags.get(i).stringValue();
      } 
    
      comment = first.get("caption").get("text").stringValue();
      username = first.get("user").get("full_name").stringValue();
    
      String profilepicture_url = first.get("user").get("profile_picture").stringValue();
      profilepicture = loadImage(profilepicture_url, "png");
    
      String userphoto_url = first.get("images").get("standard_resolution").get("url").stringValue();
      userphoto = loadImage(userphoto_url, "png");
    
     //noLoop();
    }
    
    
    void draw() {
      background(255);
      imageMode(CENTER);
      image(brand, 100, height/1.05);
    
      println(frameCount);
    
      if(frameCount % 1000 == 0){
        getGrams();
      }
    
      if (profilepicture != null) {
        image(profilepicture, 60, 70, 90, 90);
      }
      imageMode(CENTER);
      if (userphoto != null) {
        image(userphoto, width/2, height/2.25, 550, 550);
      }
    
      textFont(InstagramFont, 20);
      if (username != null) {
        text(username, 110, 115);
        fill(0);
      }
    
      textFont(InstagramFont, 15);
      if (comment != null) {
        text(comment, 15, 700, 550, 100);
        fill(0);
      }
    
    
    }
    
  • edited November 2014

    Hello.

    I'm currently studying new medias & digital arts and I'm trying, for an assignment about the specificities of new medias archives, to build an app allowing a user to mix the twitter and instagram flows of information corresponding to the same hashtag. I managed the twitter part and I found the globalgram code but I can't seem to make it work (I'm kind of new to APIs and all that). The framecount is indeed displayed in the console which doesn't indicate any code error, but nothing seems to came out from the responseReceived() function. Any idea why ?

    The code I'm using, which is approximately the same than above

    import com.francisli.processing.http.*;
    
    PFont InstagramFont;
    PImage backgroundimg;
    PImage brand;
    PImage userphoto;
    PImage profilepicture;
    
    String username;
    String tag;
    String[] tagStrings;
    
    String comment;
    
    com.francisli.processing.http.HttpClient client;
    
    void setup() {
      size(580, 900);
      smooth();
      //backgroundimg = loadImage("iso_background.jpg");
      //brand = loadImage("iso.jpg");
    
      InstagramFont = loadFont("ACaslonPro-Bold-48.vlw");
    
      client = new com.francisli.processing.http.HttpClient(this, "api.instagram.com");
      client.useSSL = true;
    
    
    }
    
    void getGrams() {
        //// instantiate a new HashMap
      HashMap params = new HashMap();
      //// put key/value pairs that you want to send in the request
      params.put("access_token", "1575021377.etc");
      params.put("count", "0");
      client.GET("/v1/tags/newyork/media/recent.json", params);
    }
    
    void responseReceived(com.francisli.processing.http.HttpRequest request, com.francisli.processing.http.HttpResponse response) {
      println(response.getContentAsString());
    
      //// we get the server response as a JSON object
      com.francisli.processing.http.JSONObject content = response.getContentAsJSONObject();
    
      //// get the "data" value, which is an array
      com.francisli.processing.http.JSONObject data = content.get("data");
    
      //// get the first element in the array
      com.francisli.processing.http.JSONObject first = data.get(0);
    
      //// the "user" value is another dictionary, from which we can get the "full_name" string value
      println(first.get("user").get("full_name").stringValue());
      //// the "caption" value is another dictionary, from which we can get the "text" string value
      //println(first.get("caption").get("text").stringValue());
      //// get profile picture
      println(first.get("user").get("profile_picture").stringValue());
      //// the "images" value is another dictionary, from which we can get different image URL data
      println(first.get("images").get("standard_resolution").get("url").stringValue());
    
      com.francisli.processing.http.JSONObject tags = first.get("tags");
      tagStrings = new String[tags.size()];
      for (int i = 0; i < tags.size(); i++) {
        tagStrings[i] = tags.get(i).stringValue();
      } 
    
      comment = first.get("caption").get("text").stringValue();
      username = first.get("user").get("full_name").stringValue();
    
      String profilepicture_url = first.get("user").get("profile_picture").stringValue();
      profilepicture = loadImage(profilepicture_url, "png");
    
      String userphoto_url = first.get("images").get("standard_resolution").get("url").stringValue();
      userphoto = loadImage(userphoto_url, "png");
    
     //noLoop();
    }
    
    
    void draw() {
      background(255);
      imageMode(CENTER);
      //image(brand, 100, height/1.05);
    
      println(frameCount);
    
      if(frameCount % 1000 == 0){
        getGrams();
      }
    
      if (profilepicture != null) {
        image(profilepicture, 60, 70, 90, 90);
      }
      imageMode(CENTER);
      if (userphoto != null) {
        image(userphoto, width/2, height/2.25, 550, 550);
      }
    
      textFont(InstagramFont, 20);
      if (username != null) {
        text(username, 110, 115);
        fill(0);
      }
    
      textFont(InstagramFont, 15);
      if (comment != null) {
        text(comment, 15, 700, 550, 100);
        fill(0);
      }
    
    
    }
    
Sign In or Register to comment.