Skip to content
Browse files

init

  • Loading branch information...
0 parents commit 98be4af98a90da3569b59ebc40e9a770e02193db @vdemedes committed
Showing with 138 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 .npmignore
  3. +48 −0 Readme.md
  4. +10 −0 index.js
  5. +27 −0 lib/google-images.coffee
  6. +36 −0 lib/google-images.js
  7. +10 −0 package.json
3 .gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+*.sock
4 .npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
48 Readme.md
@@ -0,0 +1,48 @@
+# google-images
+
+This module for Node.js helps searching images using Google Images. It provides just one method, *search*, don't you need just that?
+
+# Installation
+Install from NPM:
+`npm install google-images`
+
+# Usage
+
+`
+googleImages = 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
+
+ console.log('finished');
+ });
+});
+`
+
+# License
+
+(The MIT License)
+
+Copyright (c) 2011 Vadim Demedes <sbioko@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 index.js
@@ -0,0 +1,10 @@
+
+module.exports = require('./lib/google-images');
+
+var search = require('./lib/google-images');
+
+search.search('Katy Perry', function(results){
+ results[0].writeTo('test.png', function(){
+ console.log('finished');
+ });
+});
27 lib/google-images.coffee
@@ -0,0 +1,27 @@
+request = require 'request'
+fs = require 'fs'
+
+class GoogleImages
+
+ search: (query, callback) ->
+ return callback [] if not query
+
+ request 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' + query.replace(/\s+/, '+'), (err, res, body) ->
+ items = JSON.parse(body).responseData.results
+ images = []
+ for item in items
+ images.push
+ width: item.width
+ height: item.height
+ unescapedUrl: item.unescapedUrl
+ url: item.url
+ writeTo: (path, callback) ->
+ stream = fs.createWriteStream path
+ stream.on 'close', ->
+ callback()
+ request(item.url).pipe stream
+
+ callback images
+
+
+module.exports = new GoogleImages
36 lib/google-images.js
@@ -0,0 +1,36 @@
+var GoogleImages, fs, request;
+request = require('request');
+fs = require('fs');
+GoogleImages = (function() {
+ function GoogleImages() {}
+ GoogleImages.prototype.search = function(query, callback) {
+ if (!query) {
+ return callback([]);
+ }
+ return request('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=' + query.replace(/\s+/, '+'), function(err, res, body) {
+ var images, item, items, _i, _len;
+ items = JSON.parse(body).responseData.results;
+ images = [];
+ for (_i = 0, _len = items.length; _i < _len; _i++) {
+ item = items[_i];
+ images.push({
+ width: item.width,
+ height: item.height,
+ unescapedUrl: item.unescapedUrl,
+ url: item.url,
+ writeTo: function(path, callback) {
+ var stream;
+ stream = fs.createWriteStream(path);
+ stream.on('close', function() {
+ return callback();
+ });
+ return request(item.url).pipe(stream);
+ }
+ });
+ }
+ return callback(images);
+ });
+ };
+ return GoogleImages;
+})();
+module.exports = new GoogleImages;
10 package.json
@@ -0,0 +1,10 @@
+{
+ "name": "google-images"
+ , "version": "0.0.1"
+ , "description": "Search images using Google Images"
+ , "keywords": ["google", "images", "google images", "image search"]
+ , "author": "Vadim Demedes <sbioko@gmail.com>"
+ , "dependencies": { "request": "*" }
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}

0 comments on commit 98be4af

Please sign in to comment.
Something went wrong with that request. Please try again.