acott
acott

Reputation: 35

Instagram & Processing - Real time view

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);
  }

}

Upvotes: 0

Views: 1344

Answers (1)

jesses.co.tt
jesses.co.tt

Reputation: 2729

AFAIK it should be the

client.GET("/v1/tags/coffee/media/recent.json", params);

line that actually polls Instagram. Try wrapping that in a function like this:

void getGrams() {
  client.GET("/v1/tags/coffee/media/recent.json", params);
}

then call that once in setup() and then again when you want to ...

I'd start with trying to do it on mousePressed() or keyPressed(), so that it only fires once when you really want it to

Don't try to do it in draw() without a timer (something like if(frameCount % 5000 == 0) which will fire every five seconds ((which may be too fast still but you get the idea))

Upvotes: 1

Related Questions