Comparing version 1.0.4 to 1.0.5
@@ -21,8 +21,5 @@ #! /usr/bin/env node | ||
} else { | ||
googleIt(cli_options, (err, results) => { | ||
spinner.stop() | ||
if (err) { | ||
console.err(err) | ||
} | ||
}) | ||
googleIt(cli_options) | ||
.then(() => spinner.stop()) | ||
.catch(err => console.err(err)); | ||
} |
@@ -10,5 +10,5 @@ var request = require('request') | ||
function googleIt(config, cb) { | ||
var {query, numResults, userAgent, output} = config | ||
var options = { | ||
function googleIt(config) { | ||
var {query, numResults, userAgent, output, options = {}} = config | ||
var defaultOptions = { | ||
url: `https://www.google.com/search?q=${query}&gws_rd=ssl&num=${numResults || 10}`, | ||
@@ -18,28 +18,42 @@ headers: { | ||
} | ||
} | ||
request(options, (error, response, body) => { | ||
if (error) { | ||
return cb("Error making web request: " + error, null) | ||
} else { | ||
var results = getResults(body, config['no-display']) | ||
if (output !== undefined) { | ||
fs.writeFile(output, JSON.stringify(results, null, 2), 'utf8', (err) => { | ||
if (err) { | ||
console.err('Error writing to file ' + output + ': ' + err) | ||
} | ||
}) | ||
}; | ||
return new Promise((resolve, reject) => { | ||
request(Object.assign({}, defaultOptions, options), (error, response, body) => { | ||
if (error) { | ||
return reject("Error making web request: " + error, null) | ||
} else { | ||
var results = getResults(body, config['no-display']) | ||
if (output !== undefined) { | ||
fs.writeFile(output, JSON.stringify(results, null, 2), 'utf8', (err) => { | ||
if (err) { | ||
console.err('Error writing to file ' + output + ': ' + err) | ||
} | ||
}) | ||
} | ||
return resolve(results); | ||
} | ||
return cb(null, results) | ||
} | ||
// // for when 'save' argument is set | ||
// fs.writeFile('output.html', body, 'utf8', (err) => { | ||
// if (err) { | ||
// console.log("there was an error: " + err) | ||
// } else { | ||
// console.log("writeFile successful") | ||
// } | ||
// }); | ||
}); | ||
}); | ||
} | ||
function getSnippet(elem) { | ||
return elem['children'].map(child => { | ||
if (child.data === null) { | ||
return child.children.map(c => c.data) | ||
} else { | ||
return child.data | ||
} | ||
}).join('') | ||
} | ||
function display(results) { | ||
results.forEach((result, i) => { | ||
console.log(result.title.blue) | ||
console.log(result.link.green) | ||
console.log(result.snippet) | ||
console.log("\n") | ||
}) | ||
} | ||
function getResults(data, noDisplay) { | ||
@@ -65,9 +79,3 @@ const $ = cheerio.load(data) | ||
if (index < results.length) { | ||
var snippet = elem['children'].map((child) => { | ||
if (child.data === null) { | ||
return child.children.map((c) => c.data) | ||
} else { | ||
return child.data | ||
} | ||
}).join('') | ||
var snippet = getSnippet(elem) | ||
results[index] = Object.assign(results[index], {snippet: snippet}) | ||
@@ -78,9 +86,3 @@ } | ||
if (!noDisplay) { | ||
results.forEach((result, i) => { | ||
console.log(result.title.blue) | ||
console.log(result.link.green) | ||
console.log(result.snippet) | ||
console.log("\n") | ||
// console.log(`#${i}: ${result.title} (${result.link})`) | ||
}) | ||
display(results) | ||
} | ||
@@ -87,0 +89,0 @@ return results |
{ | ||
"name": "google-it", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A command line utility to help retrieve, display, and store Google search results", | ||
@@ -28,4 +28,6 @@ "main": "./app.js", | ||
"devDependencies": { | ||
"mocha": "^3.5.3" | ||
"eslint": "^4.8.0", | ||
"mocha": "^3.5.3", | ||
"prettier": "1.10.2" | ||
} | ||
} |
@@ -38,5 +38,6 @@ # google-it ![Travis-CI](https://travis-ci.org/PatNeedham/google-it.svg?branch=master) | ||
``` | ||
var googleIt = require('google-it') | ||
googleIt('covfefe irony').then(results => { | ||
```js | ||
const googleIt = require('google-it') | ||
googleIt({'query': 'covfefe irony'}).then(results => { | ||
// access to results object here | ||
@@ -46,2 +47,12 @@ }).catch(e => { | ||
}) | ||
// with request options | ||
const options = { | ||
'proxy': 'http://localhost:8118' | ||
}; | ||
googleIt({options, 'query': 'covfefe irony'}).then(results => { | ||
// access to results object here | ||
}).catch(e => { | ||
// any possible errors that might have occurred (like no Internet connection) | ||
}) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7495854
12
192
57
3