wko-google-search
Advanced tools
Comparing version 1.0.0 to 2.0.0
#!/usr/bin/env node | ||
'use strict'; | ||
const GoogleSearch = require('../lib/google-search'); | ||
var GoogleSearch = require('../lib/google-search'); | ||
// get environment variables | ||
const KEY = process.env.GOOG_API_KEY; | ||
const CX = process.env.GOOG_API_CX; | ||
// create a searcher | ||
var googleSearch = new GoogleSearch({ | ||
key: KEY, | ||
cx: CX | ||
const searcher = new GoogleSearch({ | ||
key: process.env.GOOG_API_KEY, | ||
cx: process.env.GOOG_API_CX | ||
}); | ||
// ------ Execute Query with 20 Results ------- | ||
searcher.fetch('foo bar baz', { limit: 20 }, (err, items) => { | ||
if (err) { | ||
throw err; | ||
} | ||
// perform a search with 20 results | ||
search('kung fu', function (err, resp) { | ||
if(err) console.error(err); | ||
else { | ||
console.dir(resp); | ||
console.info(resp.items.length + ' total'); | ||
} | ||
}); | ||
function search(term, cb) { | ||
// object used to build query | ||
let req = { | ||
q: term, | ||
num: 10, //max is 10 | ||
start: 1 | ||
}; | ||
// send query | ||
googleSearch.build(req, (err1, res1) => { | ||
if(err1) cb(err1); | ||
if(res1.searchInformation && res1.searchInformation.totalResults > 10) { | ||
//perform the nextPage query | ||
req.start = 11; | ||
googleSearch.build(req, (err2, res2) => { | ||
if(err2) cb(err2); | ||
//concat results and return them | ||
if(res2.items) | ||
res1.items = res1.items.concat(res2.items); | ||
//return response | ||
cb(null, res1); | ||
}); | ||
} else{ | ||
cb(null, res1); | ||
} | ||
}); | ||
}; | ||
items.forEach(({ numIndex, title, snippet, link }) => | ||
console.log(`${numIndex}. ${title}\n${snippet}\n${link}\n\n`)); | ||
console.log(`${items.length} items total`); | ||
}); |
@@ -1,69 +0,134 @@ | ||
var https = require('https'), | ||
url = require('url'); | ||
const https = require('https'); | ||
const url = require('url'); | ||
var GoogleSearch = function(options) { | ||
if (!options) options = {}; | ||
options.format = options.format || 'json'; | ||
options.headers = options.headers || {'User-Agent': 'GoogleSearch'}; | ||
options.host = options.host || 'www.googleapis.com'; | ||
options.port = options.port || 443; | ||
options.path = options.path || '/customsearch/v1'; | ||
options.alt = options.alt || 'json'; | ||
this.config = { | ||
key: options.key, | ||
format: options.format, | ||
headers: options.headers, | ||
host: options.host, | ||
port: options.port, | ||
path: options.path, | ||
cx: options.cx | ||
}; | ||
return this; | ||
const DEFAULTS = { | ||
format: 'json', | ||
headers: { 'User-Agent': 'GoogleSearch' }, | ||
host: 'www.googleapis.com', | ||
port: 443, | ||
path: '/customsearch/v1', | ||
alt: 'json' | ||
}; | ||
GoogleSearch.prototype.build = function(options, callback) { | ||
this._doRequest(this._generateUrl(options), callback); | ||
}; | ||
module.exports = class GoogleSearch { | ||
GoogleSearch.prototype._generateUrl = function(query) { | ||
query.key = this.config.key; | ||
query.cx = this.config.cx; | ||
var pathname = this.config.path; | ||
var urlFormatted = url.format({ | ||
protocol: "https", | ||
hostname: this.config.host, | ||
pathname: pathname, | ||
query: query | ||
}); | ||
//console.log(urlFormatted); | ||
return url.parse(urlFormatted); | ||
}; | ||
constructor(options={}) | ||
{ | ||
this._config = Object.assign({}, DEFAULTS, options); | ||
} | ||
GoogleSearch.prototype._doRequest = function(requestQuery, callback) { | ||
https.get(requestQuery, function(res) { | ||
var data = []; | ||
// for (var item in res.headers) { | ||
// console.log(item + ":" + res.headers[item]); | ||
// } | ||
// console.info(res.headers.date); | ||
// console.info('\tGoogleSearch: ', requestQuery.path); //.split('&num')[0]); | ||
/** | ||
* retrieves from offset up to (offset + limit) recursively | ||
* and calls callback with results when done | ||
*/ | ||
fetch(q, options, cb, results=[]) | ||
{ | ||
res.on('data', function(chunk) {data.push(chunk);}) | ||
.on('end', function() { | ||
var dataBuffer = data.join('').trim(); | ||
var result; | ||
try { | ||
result = JSON.parse(dataBuffer); | ||
} catch(e) { | ||
result = {'status_code': 500, 'status_text': 'JSON parse failed'}; | ||
} | ||
callback(null, result); | ||
}). | ||
on('error', function(e) { | ||
callback(e); | ||
// allow 2 param calls | ||
if (!cb) { | ||
cb = options; | ||
options = { limit: 10, offset: 1, _next: 0 }; | ||
} | ||
// pluck out vars from options | ||
let { limit=1, offset=1, _next=0 } = options; | ||
// min start of 1 | ||
if (offset <= 0) { | ||
offset = 1; | ||
} | ||
// determine how many are left to fetch | ||
const leftToFetch = (limit + offset) - (offset + _next); | ||
if (leftToFetch < 1) { | ||
return cb(null, results); | ||
} | ||
// setup the query object | ||
const query = { | ||
q, | ||
num: limit < 10 ? limit : leftToFetch < 10 ? leftToFetch : 10, | ||
start: offset + _next | ||
}; | ||
// determine if this is the last fetch | ||
const done = (query.start + 10) >= (offset + limit); | ||
this.build(query, (err, resp) => { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (!resp.items) { | ||
return cb(null, results); | ||
} | ||
results = results.concat(resp.items.map((h, i) => { | ||
return Object.assign({}, h, { numIndex: (query.start + i) }); | ||
})); | ||
if (done) { | ||
return cb(null, results); | ||
} | ||
return this.fetch(q, { limit, offset, _next: _next + 10 }, cb, results); | ||
}); | ||
return null; | ||
} | ||
/** | ||
* Builds a proper request and queries google's search | ||
* api. Callback is in the form: callback(err, resp) | ||
*/ | ||
build(query, callback) | ||
{ | ||
const data = []; | ||
https.get(this._generateUrl(query), resp => | ||
resp | ||
.on('data', chunk => data.push(chunk)) | ||
.on('error', err => callback(err)) | ||
.on('end', () => this._processPayload(data, callback)) | ||
); | ||
} | ||
/** | ||
* Formats, parses, and returns a proper url using given | ||
* query options q, and cx/key api credentials | ||
*/ | ||
_generateUrl(q) | ||
{ | ||
const query = Object.assign({}, q, { | ||
key: this._config.key, | ||
cx: this._config.cx | ||
}); | ||
return url.parse(url.format({ | ||
protocol: 'https', | ||
hostname: this._config.host, | ||
pathname: this._config.path, | ||
query | ||
})); | ||
} | ||
/** | ||
* Handles parsing of data chunks collected after | ||
* the request connection ends. Invokes callback | ||
* with search results or helpful failure message | ||
*/ | ||
_processPayload(chunkArray, callback) | ||
{ | ||
const payload = chunkArray.join('').trim(); | ||
try { | ||
const result = JSON.parse(payload); | ||
callback(null, result); | ||
} | ||
catch (e) { | ||
callback(null, { | ||
status_code: 500, | ||
status_text: 'JSON.parse failed' | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
module.exports = GoogleSearch; |
{ | ||
"name": "wko-google-search", | ||
"description": "node.js client for google search API", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/William-Olson/google-search", | ||
"author": "Will Olson <willko747@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/William-Olson/google-search.git" | ||
}, | ||
"dependencies": { | ||
"url": "0.7.9" | ||
} | ||
"name": "wko-google-search", | ||
"description": "node.js client for google search API", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/William-Olson/google-search", | ||
"author": "Will Olson <willko747@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/William-Olson/google-search.git" | ||
}, | ||
"dependencies": { | ||
"url": "0.7.9" | ||
} | ||
} |
#google-search | ||
Query Google search API using REST | ||
This node.js module is intended for who wants to interact with the [Google Custom Search API](https://developers.google.com/custom-search/v1/using_rest). This module requires that you have a [Google API Key](https://code.google.com/apis/console/) and [Google CX](http://www.google.com/cse/manage/create). | ||
Query Google search API using REST. | ||
Intended for interacting with the [Google Custom Search API](https://developers.google.com/custom-search/v1/using_rest). This module requires that you have a [Google API Key](https://code.google.com/apis/console/) and [Google CX](http://www.google.com/cse/manage/create). | ||
## Install | ||
@@ -13,5 +15,6 @@ | ||
## Usage | ||
```js | ||
var GoogleSearch = require('google-search'); | ||
var googleSearch = new GoogleSearch({ | ||
const GoogleSearch = require('wko-google-search'); | ||
const googleSearch = new GoogleSearch({ | ||
key: 'YOUR_API_KEY', | ||
@@ -21,11 +24,30 @@ cx: 'YOUR_CX' | ||
googleSearch.fetch('foo', (err, hits) => { | ||
console.log(hits); | ||
}); | ||
``` | ||
**You can pass in limit and offset in options as 2nd param** | ||
```js | ||
googleSearch.fetch('foo', { | ||
offset: 25, | ||
limit: 50 | ||
}, (err, hits) => { | ||
console.log(hits); | ||
}); | ||
``` | ||
**Or you can build a more elaborate query with the build method** | ||
```js | ||
googleSearch.build({ | ||
q: "", | ||
q: 'foo', | ||
start: 5, | ||
fileType: "pdf", | ||
gl: "tr", //geolocation, | ||
lr: "lang_tr", | ||
fileType: 'pdf', | ||
gl: 'tr', //geolocation, | ||
lr: 'lang_tr', | ||
num: 10, // Number of search results to return between 1 and 10, inclusive | ||
siteSearch: "http://kitaplar.ankara.edu.tr/" // Restricts results to URLs from a specified site | ||
siteSearch: 'http://kitaplar.ankara.edu.tr/' // Restricts results to URLs from a specified site | ||
}, function(error, response) { | ||
@@ -32,0 +54,0 @@ console.log(response); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7022
144
80
2