├── .gitignore ├── .npmignore ├── index.js ├── package.json ├── lib ├── google-images.coffee └── google-images.js └── Readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *.sock 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = require('./lib/google-images'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "google-images" 3 | , "version": "0.1.2" 4 | , "description": "Search images using Google Images" 5 | , "keywords": ["google", "images", "google images", "image search"] 6 | , "author": "Vadim Demedes " 7 | , "dependencies": { "request": "*" } 8 | , "main": "index" 9 | , "engines": { "node": ">= 0.4" } 10 | } -------------------------------------------------------------------------------- /lib/google-images.coffee: -------------------------------------------------------------------------------- 1 | request = require 'request' 2 | fs = require 'fs' 3 | 4 | generateInfo = (item) -> 5 | info = 6 | width: item.width 7 | height: item.height 8 | unescapedUrl: item.unescapedUrl 9 | url: item.url 10 | writeTo: (path, callback) -> 11 | stream = fs.createWriteStream path 12 | stream.on 'close', -> 13 | callback?() 14 | request(item.url).pipe stream 15 | return info 16 | 17 | exports.search = (query, options) -> 18 | if typeof query is 'object' 19 | options = query 20 | query = options.for 21 | callback = options.callback if options.callback? 22 | if typeof query is 'string' and typeof options is 'function' 23 | callback = options 24 | options = {} 25 | if typeof query is 'string' and typeof options is 'object' 26 | callback = options.callback if options.callback? 27 | 28 | options.page = 0 if not options.page? 29 | 30 | request "http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=#{ query.replace(/\s/g, '+') }&start=#{ options.page }", (err, res, body) -> 31 | try 32 | data = JSON.parse(body) 33 | catch error 34 | callback no, [] if callback 35 | return 36 | 37 | if not data.responseData or not data.responseData.results 38 | callback no, [] if callback 39 | return 40 | 41 | items = data.responseData.results 42 | 43 | images = [] 44 | for item in items 45 | images.push generateInfo item 46 | 47 | callback no, images if callback -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # google-images 2 | 3 | This module for Node.js helps searching images using Google Images. It provides just one method, *search*, don't you need just that? 4 | 5 | # Installation 6 | Install from NPM: 7 | 8 | ```npm install google-images``` 9 | 10 | # Usage 11 | 12 | ``` 13 | client = require 'google-images' 14 | 15 | client.search 'Michael Jackson', (err, images) -> 16 | image.writeTo 'path_to_image.extension', -> # image saved to the disk 17 | 18 | client.search for: 'Michael Jackson', callback: (err, images) -> 19 | 20 | client.search for: 'Michael Jackson', page: 2, callback: (err, images) -> 21 | 22 | client.search 'Michael Jackson', page: 2, callback: (err, images) -> 23 | 24 | ``` 25 | 26 | # License 27 | 28 | (The MIT License) 29 | 30 | Copyright (c) 2011 Vadim Demedes <sbioko@gmail.com> 31 | 32 | Permission is hereby granted, free of charge, to any person obtaining 33 | a copy of this software and associated documentation files (the 34 | 'Software'), to deal in the Software without restriction, including 35 | without limitation the rights to use, copy, modify, merge, publish, 36 | distribute, sublicense, and/or sell copies of the Software, and to 37 | permit persons to whom the Software is furnished to do so, subject to 38 | the following conditions: 39 | 40 | The above copyright notice and this permission notice shall be 41 | included in all copies or substantial portions of the Software. 42 | 43 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 44 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 45 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 46 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 47 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 48 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 49 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /lib/google-images.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.10.0 2 | (function() { 3 | var fs, generateInfo, request; 4 | 5 | request = require('request'); 6 | 7 | fs = require('fs'); 8 | 9 | generateInfo = function(item) { 10 | var info; 11 | info = { 12 | width: item.width, 13 | height: item.height, 14 | unescapedUrl: item.unescapedUrl, 15 | url: item.url, 16 | writeTo: function(path, callback) { 17 | var stream; 18 | stream = fs.createWriteStream(path); 19 | stream.on('close', function() { 20 | return typeof callback === "function" ? callback() : void 0; 21 | }); 22 | return request(item.url).pipe(stream); 23 | } 24 | }; 25 | return info; 26 | }; 27 | 28 | exports.search = function(query, options) { 29 | var callback; 30 | if (typeof query === 'object') { 31 | options = query; 32 | query = options["for"]; 33 | if (options.callback != null) { 34 | callback = options.callback; 35 | } 36 | } 37 | if (typeof query === 'string' && typeof options === 'function') { 38 | callback = options; 39 | options = {}; 40 | } 41 | if (typeof query === 'string' && typeof options === 'object') { 42 | if (options.callback != null) { 43 | callback = options.callback; 44 | } 45 | } 46 | if (options.page == null) { 47 | options.page = 0; 48 | } 49 | 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) { 50 | var data, error, error1, i, images, item, items, len; 51 | try { 52 | data = JSON.parse(body); 53 | } catch (error1) { 54 | error = error1; 55 | if (callback) { 56 | callback(false, []); 57 | } 58 | return; 59 | } 60 | if (!data.responseData || !data.responseData.results) { 61 | if (callback) { 62 | callback(false, []); 63 | } 64 | return; 65 | } 66 | items = data.responseData.results; 67 | images = []; 68 | for (i = 0, len = items.length; i < len; i++) { 69 | item = items[i]; 70 | images.push(generateInfo(item)); 71 | } 72 | if (callback) { 73 | return callback(false, images); 74 | } 75 | }); 76 | }; 77 | 78 | }).call(this); 79 | --------------------------------------------------------------------------------