I spent today working out how to get data from the Twitter and Instagram APIs into Processing. It wasn’t easy, but it’s done and I have a nice thing that has been grabbing Instagrams tagged #selfie and combining them with tweets that contain #myself, example shown above.
It’s been such a slog that it’s actually hard to write up. The twitter bit was done beginning with Jer Thorp’s code example here, and making some changes to the twitter4j import. At the bottom of that blog post there are a few important tips, and you should change the following in that code:
// 1. added import java.util.*; (for Date support)
// 2. Replace Tweet with Status
// 3. Replace query.setRpp(100); with query.setCount(100);
It’s also worth noting that changing the way that Twitter4J is installed is an important step to getting it to work: details here.
The Instagram data import took longer to figure out. Firstly, a lot of examples you’ll find online lead you to Francis Li’s http library, and while this looks the part, I couldn’t get it to work on my system. In the end, I used the http library recommended on the Processing libraries page, and modified the JSON example here to search for a hashtag. Instagram’s JSON stream comes back differently to the examples that used Francis Li’s library too, so it took me a while to sift through the stream to get to the bits I needed. The code is here, so feel free to use it and best of luck to you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import http.requests.*; // the http lib PFont InstagramFont; // in case you need it PImage userphoto; // to hold the incoming image public void setup() { size(400, 400); smooth(); getGrams(); } void getGrams() { GetRequest get = new GetRequest("https://api.instagram.com/v1/tags/selfie/media/recent?count=2&client_id=YOUR_CLIENT_ID"); get.send(); // program will wait untill the request is completed // now we need to deal with the data. // First, we convert it to an "internal" JSON object JSONObject content = parseJSONObject(get.getContent()); // Next, we get from that an array of all the posts in the returned data object JSONArray data = content.getJSONArray("data"); // Let's get the first chunk of that data into another object called first JSONObject first = data.getJSONObject(0); // To test, let's get out the filter from that chunk, because that's a string not an object String filter = first.getString("filter"); // Let's find the images object in the first chunk of data JSONObject images = first.getJSONObject("images"); // and let's get the standard_resolution version of the image in the images object JSONObject standard_resolution = images.getJSONObject("standard_resolution"); // finally, let's get the string with the URL in it String URL = standard_resolution.getString("url"); // Print it to the console, open champagne etc println("URL = " + URL); // Load in the image at that URL userphoto = loadImage(URL); } void draw() { if (frameCount % 1000 == 0) { println("Getting grams..."); getGrams(); } imageMode(CENTER); if (userphoto != null) { image(userphoto, width/2, height/2.25, 550, 550); } else { println("userphoto null"); } fill(0, 10); rect(0, 0, width, height); } |