Permalink
Browse files

added pagination and made code more pleasant to work with

  • Loading branch information...
1 parent 04d4912 commit 199b97a0657d8912fd98702f3e98735a7392b56d @vadimdemedes committed Mar 16, 2012
Showing with 44 additions and 22 deletions.
  1. +9 −9 Readme.md
  2. +14 −4 lib/google-images.coffee
  3. +20 −8 lib/google-images.js
  4. +1 −1 package.json
View
@@ -10,17 +10,17 @@ Install from NPM:
# Usage
```
-googleImages = require('google-images');
+client = require 'google-images'
-googleImages.search('Katy Perry', function(results){
- // let's say we want to save first image to a file
-
- results[0].writeTo('katy.png', function(){
- // this function fires when file write op completed
+client.search 'Michael Jackson', (err, images) ->
+ image.writeTo 'path_to_image.extension', -> # image saved to the disk
+
+client.search for: 'Michael Jackson', callback: (err, images) ->
+
+client.search for: 'Michael Jackson', page: 2, callback: (err, images) ->
+
+client.search 'Michael Jackson', page: 2, callback: (err, images) ->
- console.log('finished');
- });
-});
```
# License
View
@@ -2,10 +2,20 @@ request = require 'request'
fs = require 'fs'
module.exports=
- search: (query, callback) ->
- return callback [] if not query
+ search: (query, options) ->
+ if typeof query is 'object'
+ options = query
+ query = options.for
+ callback = options.callback if options.callback?
+ if typeof query is 'string' and typeof options is 'function'
+ callback = options
+ options = {}
+ if typeof query is 'string' and typeof options is 'object'
+ callback = options.callback if options.callback?
- request 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' + query.replace(/\s/g, '+'), (err, res, body) ->
+ options.page = 0 if not options.page?
+
+ request "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=#{ query.replace(/\s/g, '+') }&start=#{ options.page }", (err, res, body) ->
items = JSON.parse(body).responseData.results
images = []
for item in items
@@ -20,4 +30,4 @@ module.exports=
callback()
request(item.url).pipe stream
- callback images if callback
+ callback no, images if callback
View
@@ -1,12 +1,26 @@
var fs, request;
+
request = require('request');
+
fs = require('fs');
+
module.exports = {
- search: function(query, callback) {
- if (!query) {
- return callback([]);
+ search: function(query, options) {
+ var callback;
+ if (typeof query === 'object') {
+ options = query;
+ query = options["for"];
+ if (options.callback != null) callback = options.callback;
}
- return request('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' + query.replace(/\s/g, '+'), function(err, res, body) {
+ if (typeof query === 'string' && typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+ if (typeof query === 'string' && typeof options === 'object') {
+ if (options.callback != null) callback = options.callback;
+ }
+ if (!(options.page != null)) options.page = 0;
+ return request("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + (query.replace(/\s/g, '+')) + "&start=" + options.page, function(err, res, body) {
var images, item, items, _i, _len;
items = JSON.parse(body).responseData.results;
images = [];
@@ -27,9 +41,7 @@ module.exports = {
}
});
}
- if (callback) {
- return callback(images);
- }
+ if (callback) return callback(false, images);
});
}
-};
+};
View
@@ -1,6 +1,6 @@
{
"name": "google-images"
- , "version": "0.0.5"
+ , "version": "0.1.0"
, "description": "Search images using Google Images"
, "keywords": ["google", "images", "google images", "image search"]
, "author": "Vadim Demedes <sbioko@gmail.com>"

0 comments on commit 199b97a

Please sign in to comment.