Skip to content

Instantly share code, notes, and snippets.

@murilopolese
Last active November 12, 2015 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilopolese/e9ac5bfb616c642f92f1 to your computer and use it in GitHub Desktop.
Save murilopolese/e9ac5bfb616c642f92f1 to your computer and use it in GitHub Desktop.
Processing Instagram Fetcher
import http.requests.*;
class Instagram {
private String accessToken = "508806944.c6cb753.2088e0733e0d432689efc5fcd15bb3b0";
private String tag = "nofilter";
private JSONArray posts;
private ArrayList<PImage> images;
private String imageFormat = "jpg";
private int imageListSize = 100;
// Constructors
Instagram() {
this.images = new ArrayList<PImage>();
}
// Getters and setters
String getTag() {
return this.tag;
}
void setTag( String tag ) {
this.tag = tag;
}
String getImageFormat() {
return this.imageFormat;
}
void setImageFormat( String imageFormat ) {
this.imageFormat = imageFormat;
}
void setImageListSize( int size ) {
this.imageListSize = size;
}
int getImageListSize() {
return this.imageListSize;
}
void setAccessToken( String accessToken ) {
this.accessToken = accessToken;
}
ArrayList<PImage> getImages() {
ArrayList<PImage> images = this.images;
// If the requested amount of images is larger then the amount of available images
// fill the response with repeated images
if( this.images.size() > 0 && this.imageListSize > this.images.size() ) {
for( int i = this.images.size(); i < this.imageListSize; i++ ) {
images.add( this.images.get( i % this.images.size() ) );
}
}
return images;
}
JSONArray getPosts() {
return this.posts;
}
// HTTP requests
GetRequest makeRequest() {
GetRequest get = new GetRequest("https://api.instagram.com/v1/tags/" + this.tag + "/media/recent?access_token=" + this.accessToken );
return get;
}
// Make an http request to instagram api and cache the json response locally
void fetch() {
GetRequest get = this.makeRequest();
get.send();
JSONObject json = parseJSONObject( get.getContent() );
JSONArray posts = json.getJSONArray( "data" );
this.posts = posts;
}
// Parse the cached posts and save the images on the data folder
void saveImages() {
for( int i = 0; i < this.posts.size(); i++ ) {
PImage img = loadImage(
this.posts.getJSONObject( i )
.getJSONObject("images")
.getJSONObject("standard_resolution")
.getString("url")
);
String filename = this.posts.getJSONObject( i ).getString( "id" );
img.save( savePath( "data/" + filename ) + "." + this.imageFormat );
}
}
void sync() {
this.fetch();
this.saveImages();
this.readImages();
}
// Load images from data folder
void readImages() {
File dir = new File( savePath( "data" ) );
String[] list = dir.list();
if( list.length > 0 && list[ 0 ].equals( ".DS_Store" ) ) {
list = subset( list, 1 );
}
this.images = new ArrayList<PImage>();
int max = list.length;
if( max > this.imageListSize ) {
max = this.imageListSize;
}
for( int i = 0; i < max; i++ ) {
if( list[ i ].indexOf( "." + this.imageFormat ) != -1 ) {
this.images.add( loadImage( list[ i ] ) );
}
}
}
}
Instagram i = new Instagram();
ArrayList<PImage> images = new ArrayList<PImage>();
void setup() {
i.setTag( "designit" );
i.setImageListSize( 200 );
sync();
}
void draw() {
if (frameCount % 300 == 0) {
thread("sync");
println( images.size() );
}
}
void sync() {
println( "syncing" );
try {
i.sync();
images = i.getImages();
println( "synced" );
} catch( Exception e ) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment