Skip to content

Commit

Permalink
Created a better URL reader to fetch data from the ColourLovers API. …
Browse files Browse the repository at this point in the history
…This should fix issue #2 (#2)
  • Loading branch information
vormplus committed Jun 11, 2015
1 parent d5ea452 commit 1e416eb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
64 changes: 50 additions & 14 deletions src/colorlib/webservices/ColourLovers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

package colorlib.webservices;

import java.io.*;
import java.net.*;

import java.util.ArrayList;

import processing.core.*;
Expand Down Expand Up @@ -104,30 +107,63 @@ private ArrayList<Palette> makePalettes( String query )
{
String feedURL = new StringBuffer( COLOURLOVERS_API_URL ).append( query ).toString();

ArrayList<Palette> out = new ArrayList<Palette>();

if ( DEBUG ) {
System.out.println( feedURL );
System.out.println( "--------------------------------" );
}

ArrayList<Palette> out = new ArrayList<Palette>();

jsonFeed = new JSONArray( p.createReader( feedURL ) );

for ( int i = 0; i < jsonFeed.size(); i++ ) {

try {

// The Processing createReader() method doesn't add an User-Agent string,
// so the ColourLovers API returns a HTTP 403 status code since they block the default Java User-Agent.
// This block of code creates a request by setting a fake User-Agent string so or Processing app will act like a
// real browser, and the ColourLovers API returns the JSON we need.

JSONObject paletteObject = jsonFeed.getJSONObject( i );
JSONArray colorsArray = paletteObject.getJSONArray( "colors" );
URL url = new URL( feedURL );
URLConnection connection = url.openConnection();
connection.setRequestProperty( "User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5" );

Palette palette = new Palette( p );
InputStream response = connection.getInputStream();

for ( int j = 0; j < colorsArray.size(); j++ ) {
int c = PApplet.unhex( "FF" + colorsArray.getString( j ) );
palette.addColor( c );
Reader in = new InputStreamReader( response );

jsonFeed = new JSONArray( in );

for ( int i = 0; i < jsonFeed.size(); i++ ) {

JSONObject paletteObject = jsonFeed.getJSONObject( i );
JSONArray colorsArray = paletteObject.getJSONArray( "colors" );

Palette palette = new Palette( p );

for ( int j = 0; j < colorsArray.size(); j++ ) {
int c = PApplet.unhex( "FF" + colorsArray.getString( j ) );
palette.addColor( c );
}

out.add( palette );

}

in.close();

if ( DEBUG ) {
System.out.println( jsonFeed );
System.out.println( "--------------------------------" );
}

} catch ( MalformedURLException e ) {

out.add( palette );
System.out.println( e );

} catch ( IOException e ) {

System.out.println( e );

}

return out;
}

Expand Down
9 changes: 2 additions & 7 deletions src/colorlib/webservices/WebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,8 @@

package colorlib.webservices;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import java.net.*;

import processing.core.*;

Expand Down

0 comments on commit 1e416eb

Please sign in to comment.