Comparing version 1.2.2 to 1.3.0
{ | ||
"name": "google-it", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "A CLI and Node.js library to help retrieve, display, and store Google search results", | ||
@@ -5,0 +5,0 @@ "main": "./src/googleIt.js", |
@@ -11,31 +11,10 @@ /* eslint-disable no-console */ | ||
getDefaultRequestOptions, | ||
titleSelector, | ||
linkSelector, | ||
snippetSelector, | ||
getTitleSelector, | ||
getLinkSelector, | ||
getSnippetSelector, | ||
logIt, | ||
saveToFile, | ||
saveResponse, | ||
} = require('./utils'); | ||
function logIt(message, disableConsole) { | ||
if (!disableConsole) { | ||
console.log(message); | ||
} | ||
} | ||
function saveToFile(output, results) { | ||
if (output !== undefined) { | ||
fs.writeFile(output, JSON.stringify(results, null, 2), 'utf8', (err) => { | ||
if (err) { | ||
console.err(`Error writing to file ${output}: ${err}`); | ||
} | ||
}); | ||
} | ||
} | ||
function saveResponse(response, htmlFileOutputPath) { | ||
if (htmlFileOutputPath) { | ||
fs.writeFile(htmlFileOutputPath, response.body, () => { | ||
console.log(`Html file saved to ${htmlFileOutputPath}`); | ||
}); | ||
} | ||
} | ||
function errorTryingToOpen(error, stdout, stderr) { | ||
@@ -86,3 +65,9 @@ if (error) { | ||
function getResults({ | ||
data, noDisplay, disableConsole, onlyUrls, | ||
data, | ||
noDisplay, | ||
disableConsole, | ||
onlyUrls, | ||
titleSelector, | ||
linkSelector, | ||
snippetSelector, | ||
}) { | ||
@@ -92,3 +77,3 @@ const $ = cheerio.load(data); | ||
const titles = $(titleSelector).contents(); | ||
const titles = $(getTitleSelector(titleSelector)).contents(); | ||
titles.each((index, elem) => { | ||
@@ -102,3 +87,3 @@ if (elem.data) { | ||
$(linkSelector).map((index, elem) => { | ||
$(getLinkSelector(linkSelector)).map((index, elem) => { | ||
if (index < results.length) { | ||
@@ -111,3 +96,3 @@ results[index] = Object.assign(results[index], { | ||
$(snippetSelector).map((index, elem) => { | ||
$(getSnippetSelector(snippetSelector)).map((index, elem) => { | ||
if (index < results.length) { | ||
@@ -144,11 +129,6 @@ results[index] = Object.assign(results[index], { | ||
if (error) { | ||
// eslint-disable-next-line prefer-promise-reject-errors | ||
return reject(`Error making web request: ${error}`, null); | ||
reject(new Error(`Error making web request: ${error}`)); | ||
} | ||
if (response.statusCode !== 200) { | ||
return reject(cheerio.load(response.body).text()); | ||
} | ||
saveResponse(response, htmlFileOutputPath); | ||
return resolve(body); | ||
resolve(body); | ||
}); | ||
@@ -161,8 +141,11 @@ } | ||
const { | ||
query, limit, userAgent, output, open, options = {}, htmlFileOutputPath, fromFile, | ||
output, | ||
open, | ||
returnHtmlBody, | ||
titleSelector, | ||
linkSelector, | ||
snippetSelector, | ||
} = config; | ||
return new Promise((resolve, reject) => { | ||
getResponseBody({ | ||
fromFile, options, htmlFileOutputPath, query, limit, userAgent, | ||
}).then((body) => { | ||
getResponseBody(config).then((body) => { | ||
const results = getResults({ | ||
@@ -173,5 +156,11 @@ data: body, | ||
onlyUrls: config['only-urls'], | ||
titleSelector, | ||
linkSelector, | ||
snippetSelector, | ||
}); | ||
saveToFile(output, results); | ||
openInBrowser(open, results); | ||
if (returnHtmlBody) { | ||
return resolve({ results, body }); | ||
} | ||
return resolve(results); | ||
@@ -178,0 +167,0 @@ }).catch(reject); |
@@ -46,4 +46,9 @@ const optionDefinitions = [ | ||
{ name: 'fromFile', alias: 'f', type: String }, | ||
// override the hard-coded selectors defined inside /src/utils.js | ||
{ name: 'titleSelector', type: String }, | ||
{ name: 'linkSelector', type: String }, | ||
{ name: 'snippetSelector', type: String }, | ||
]; | ||
module.exports = optionDefinitions; |
@@ -10,3 +10,3 @@ const commandLineArgs = require('command-line-args'); | ||
// eslint-disable-next-line prefer-destructuring | ||
cliOptions.query = argv[2]; | ||
cliOptions.query = argv[2].replace('--query=', ''); | ||
} | ||
@@ -13,0 +13,0 @@ return cliOptions; |
@@ -0,2 +1,11 @@ | ||
/* eslint-disable no-console */ | ||
const fs = require('fs'); | ||
const { | ||
GOOGLE_IT_TITLE_SELECTOR, | ||
GOOGLE_IT_LINK_SELECTOR, | ||
GOOGLE_IT_SNIPPET_SELECTOR, | ||
} = process.env; | ||
// NOTE: | ||
@@ -24,2 +33,38 @@ // I chose the User-Agent value from http://www.browser-info.net/useragents | ||
const getTitleSelector = passedValue => ( | ||
passedValue || GOOGLE_IT_TITLE_SELECTOR || titleSelector | ||
); | ||
const getLinkSelector = passedValue => ( | ||
passedValue || GOOGLE_IT_LINK_SELECTOR || linkSelector | ||
); | ||
const getSnippetSelector = passedValue => ( | ||
passedValue || GOOGLE_IT_SNIPPET_SELECTOR || snippetSelector | ||
); | ||
const logIt = (message, disableConsole) => { | ||
if (!disableConsole) { | ||
console.log(message); | ||
} | ||
}; | ||
const saveToFile = (output, results) => { | ||
if (output !== undefined) { | ||
fs.writeFile(output, JSON.stringify(results, null, 2), 'utf8', (err) => { | ||
if (err) { | ||
console.err(`Error writing to file ${output}: ${err}`); | ||
} | ||
}); | ||
} | ||
}; | ||
const saveResponse = (response, htmlFileOutputPath) => { | ||
if (htmlFileOutputPath) { | ||
fs.writeFile(htmlFileOutputPath, response.body, () => { | ||
console.log(`Html file saved to ${htmlFileOutputPath}`); | ||
}); | ||
} | ||
}; | ||
module.exports = { | ||
@@ -29,5 +74,8 @@ defaultUserAgent, | ||
getDefaultRequestOptions, | ||
titleSelector, | ||
linkSelector, | ||
snippetSelector, | ||
getTitleSelector, | ||
getLinkSelector, | ||
getSnippetSelector, | ||
logIt, | ||
saveToFile, | ||
saveResponse, | ||
}; |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
7859542
392
3