Skip to content

Instantly share code, notes, and snippets.

@andrewhaskin
Created December 16, 2011 16:20
Show Gist options
  • Save andrewhaskin/1486684 to your computer and use it in GitHub Desktop.
Save andrewhaskin/1486684 to your computer and use it in GitHub Desktop.
Here is the code for Globalgram using Processing and Instagram API. It searches Instagram for the latest postings on different search criteria.
import com.francisli.processing.http.*;
import processing.serial.*;
Serial myPort; // The serial port
int value = 0;
float oldValue;
PFont InstagramFont;
PFont InstagramFont2;
PImage backgroundimg;
PImage userphoto;
PImage profilepicture;
String username;
String tag;
String[] tagStrings;
String search;
HttpClient client;
void setup() {
size(600, 900);
smooth();
backgroundimg = loadImage("globalgram_background_v2.jpg");
InstagramFont = loadFont("Helvetica-Bold-36.vlw");
InstagramFont2 = loadFont("Helvetica-Light-16.vlw");
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
}
void serialEvent (Serial myPort) {
//// do whatever to read from the serial port
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// convert to an int and map to the screen height:
float newValue = float(inString);
if (newValue != oldValue) {
//// do what you want based on the new value- start a request, etc...
if (newValue == 1) {
search = "/v1/tags/mountain/media/recent.json";
}
if (newValue == 2) {
search = "/v1/tags/sky/media/recent.json";
}
if (newValue == 3) {
search = "/v1/tags/flower/media/recent.json";
}
if (newValue == 4) {
search = "/v1/tags/landscape/media/recent.json";
}
//// finally, this "new" value becomes the "old" value for the next check
client = new 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", "<<<INSERT ACCESS TOKEN HERE>>>");
params.put("count", "1");
client.GET(search, params);
oldValue = newValue;
}
if (newValue == 101) {
//keyPressed1();
save("globalgram_print_img.jpg");
delay(300);
}
}
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.getContentAsString());
//// we get the server response as a JSON object
JSONObject content = response.getContentAsJSONObject();
//// get the "data" value, which is an array
if (content.get("data") != null) {
JSONObject data = content.get("data");
//// get the first element in the array
JSONObject first = data.get(0);
//// the "user" value is another dictionary, from which we can get the "full_name" string value
if (first.get("user").get("full_name").stringValue() != null) {
println(first.get("user").get("full_name").stringValue());
}
//// the "caption" value is another dictionary, from which we can get the "text" string value
if (first.get("caption").get("text").stringValue() != null) {
println(first.get("caption").get("text").stringValue());
}
//// get profile picture
if (first.get("user").get("profile_picture").stringValue() != null) {
println(first.get("user").get("profile_picture").stringValue());
}
//// the "images" value is another dictionary, from which we can get different image URL data
if (first.get("images").get("standard_resolution").get("url").stringValue() != null) {
println(first.get("images").get("standard_resolution").get("url").stringValue());
}
if (first.get("tags") != null) {
JSONObject tags = first.get("tags");
tagStrings = new String[tags.size()];
for (int i = 0; i < tags.size(); i++) {
tagStrings[i] = tags.get(i).stringValue();
}
}
if (first.get("user").get("full_name").stringValue() != null) {
username = first.get("user").get("full_name").stringValue();
}
if (first.get("user").get("profile_picture").stringValue() != null) {
String profilepicture_url = first.get("user").get("profile_picture").stringValue();
profilepicture = loadImage(profilepicture_url, "png");
}
if (first.get("user").get("profile_picture").stringValue() != null) {
String userphoto_url = first.get("images").get("standard_resolution").get("url").stringValue();
userphoto = loadImage(userphoto_url, "png");
}
}
}
void draw() {
background(255);
imageMode(CENTER);
image(backgroundimg, width/2, height/2);
if (profilepicture != null) {
image(profilepicture, 82, 90, 90, 90);
}
imageMode(CENTER);
if (userphoto != null) {
image(userphoto, width/2, height/2-30, 525, 525);
}
textFont(InstagramFont, 36);
if (username != null) {
text(username, 140, 105);
fill(0);
}
textFont(InstagramFont2, 16);
if ((tagStrings != null) && (tagStrings.length > 0)) {
String line = "#" + tagStrings[0];
for (int i = 1; i < tagStrings.length; i++) {
line += ", " + tagStrings[i];
}
text(line, 40, 690, 525, 150);
fill(0);
}
}
void keyPressed() {
if (value == 0) {
save("globalgram_print_img.jpg");
}
else {
value = 0;
}
}
@wiliamDev
Copy link

hey @OnkelDittmeyer,it can be solved setting "com.francisli.processing.http." before the "ambiguos" thing, I dont know why, but al least don't generate any error...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment