Comparing version 1.0.2 to 1.0.3
99
app.js
#! /usr/bin/env node | ||
var request = require('request') | ||
var fs = require('fs') | ||
var cheerio = require('cheerio') | ||
var colors = require('colors') | ||
const ora = require('ora') | ||
const spinner = ora({text: 'Loading results', color: 'cyan'}).start(); | ||
const commandLineArgs = require('command-line-args') | ||
const validateCLIArguments = require('./validateCLIArguments') | ||
const googleIt = require('./googleIt') | ||
const optionDefinitions = require('./optionDefinitions') | ||
const cli_options = commandLineArgs(optionDefinitions) | ||
var cli_options = commandLineArgs(optionDefinitions) | ||
var query = "" | ||
// first arg is 'node', second is /path/to/file/app.js, third is whatever follows afterward | ||
if (process.argv.length > 2) { | ||
query = process.argv[2] | ||
// console.log("query: " + query) | ||
cli_options['query'] = process.argv[2] | ||
} | ||
var validation = validateCLIArguments(cli_options) | ||
// fs.readFile('output.html', 'utf8', (err, data) => { | ||
// if (err != null) { | ||
// console.log("err was not null, it is: " + JSON.stringify(err, null, 4)) | ||
// } | ||
// | ||
// var results = getResults(data) | ||
// }) | ||
function googleIt(query, numResults=10) { | ||
var options = { | ||
url: `https://www.google.com/search?q=${query}&gws_rd=ssl&num=${numResults}`, | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0' | ||
if (!validation.valid) { | ||
console.log("Invalid options. Error: " + validation.error) | ||
spinner.stop() | ||
} else { | ||
googleIt(cli_options, (err, results) => { | ||
spinner.stop() | ||
if (err) { | ||
console.err(err) | ||
} | ||
} | ||
request(options, (error, response, body) => { | ||
if (error) { | ||
console.log("error: " + error) | ||
} else { | ||
spinner.stop() | ||
var results = getResults(body) | ||
if (cli_options.output != null) { | ||
console.log("writing to output file! :) :) :)") | ||
fs.writeFile(cli_options.output, JSON.stringify(results, null, 2), 'utf8') | ||
} | ||
} | ||
// fs.writeFile('output.html', body, 'utf8', (err) => { | ||
// if (err) { | ||
// console.log("there was an error: " + err) | ||
// } else { | ||
// console.log("writeFile successful") | ||
// } | ||
// }); | ||
}); | ||
} | ||
googleIt(query != "" ? query : "new york penn station", cli_options.limit) | ||
function getResults(data) { | ||
const $ = cheerio.load(data) | ||
var results = [] | ||
// result titles | ||
var titles = $('div.rc > h3.r > a').contents() | ||
titles.each((index, elem) => { | ||
results.push({"title": elem.data}) | ||
}) | ||
// result links | ||
$('div.rc > h3.r > a').map((index, elem) => { | ||
if (index < results.length) { | ||
results[index] = Object.assign(results[index], {"link": elem['attribs']['href']}) | ||
} | ||
}) | ||
// result snippets | ||
$('div.rc > div.s > div > span.st').map((index, elem) => { | ||
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('') | ||
results[index] = Object.assign(results[index], {snippet: snippet}) | ||
} | ||
}) | ||
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})`) | ||
}) | ||
return results | ||
} |
@@ -9,5 +9,5 @@ const optionDefinitions = [ | ||
{ name: 'bold-matching-text', alias: 'b', type: Boolean}, // only takes effect when interactive (-i) flag is set as well, will bold test in results that matched the query | ||
{ name: 'stackoverflow-github-only', alias: 'X', type: Boolean }, // option to limit results to only these two sites | ||
{ name: 'stackoverflow-github-only', alias: 'X', type: Boolean } // option to limit results to only these two sites | ||
] | ||
module.exports = optionDefinitions |
{ | ||
"name": "google-it", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A command line utility to help retrieve, display, and store Google search results", | ||
@@ -12,3 +12,3 @@ "main": "./app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test/**/*.js" | ||
}, | ||
@@ -27,3 +27,6 @@ "keywords": [ | ||
"request": "^2.81.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^3.5.3" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# google-it | ||
# google-it ![Travis-CI](https://travis-ci.org/PatNeedham/google-it.svg?branch=master) | ||
@@ -20,2 +20,3 @@ A simple library to convert Google search results to JSON output, with an interactive display option coming in the near future. | ||
- [x] *output* - name of the JSON file to save results to | ||
- [ ] *no-display* - prevent results from appearing in the terminal output. Should only be used with --output (-o) command when saving results to a file | ||
- [ ] *save* - name of the html file if you want to save the actual response from the html request (useful for debugging purposes) | ||
@@ -22,0 +23,0 @@ - [x] *limit* - number of search results to be returned |
Sorry, the diff of this file is not supported yet
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
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4465573
8
123
1
40
1