google-search-results-nodejs
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,175 +0,10 @@ | ||
const client = require('https'); | ||
const querystring = require('querystring'); | ||
const LOCATION_API = "/locations.json" | ||
const client = require('./SerpApiClient') | ||
/*** | ||
* Google search results with serpapi.com | ||
*/ | ||
class GoogleSearchResults { | ||
class GoogleSearchResults extends client.SerpApiClient { | ||
constructor(api_key = null) { | ||
if (api_key != null) { | ||
this.api_key = api_key | ||
} | ||
this.defaultTimeout = 60000 | ||
constructor(api_key) { | ||
super(api_key, "google") | ||
} | ||
// build url on the fly | ||
// private | ||
buildUrl(path, parameter, output) { | ||
// Set language | ||
parameter["source"] = "nodejs" | ||
// Set format | ||
if (output != null) { | ||
parameter["output"] = output | ||
} | ||
// Add api_key | ||
if(parameter["api_key"] == null) { | ||
if (this.api_key != null) { | ||
parameter["api_key"] = this.api_key | ||
} | ||
else if (path == LOCATION_API) { | ||
// skip free api | ||
} | ||
else { | ||
throw new Error("api_key is required. copy it from: https://serpapi.com/dashboard ") | ||
} | ||
} | ||
// build url | ||
return "https://serpapi.com" + path + "?" + querystring.stringify(parameter) | ||
} | ||
/*** | ||
* setTimeout | ||
* @param timeout maximum time to wait the http response in ms (default: 60000ms) | ||
*/ | ||
setTimeout(timeout) { | ||
this.defaultTimeout = timeout | ||
} | ||
/*** | ||
* execute | ||
* | ||
* @param path URL path | ||
* @param parameter query | ||
* @param callback handle next step | ||
* @param output format json|html | ||
*/ | ||
execute(path, parameter, callback, output) { | ||
let url = this.buildUrl(path, parameter, output) | ||
client.timeout = this.defaultTimeout | ||
client.get(url, (resp) => { | ||
let data = '' | ||
// A chunk of data has been recieved. | ||
resp.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
// The whole response has been received. Print out the result. | ||
resp.on('end', () => { | ||
if (resp.statusCode == 200) { | ||
callback(data) | ||
return | ||
} | ||
let msg = JSON.parse(data) | ||
throw new Error(msg.error) | ||
}); | ||
}).on("error", (err) => { | ||
console.log("Error: " + err.message); | ||
throw err; | ||
}); | ||
} | ||
/*** | ||
* Run raw search query | ||
* | ||
* @param [Map] parameter (see: serpapi.com) | ||
* @param [Function] callback | ||
*/ | ||
search(parameter, output, callback) { | ||
this.execute("/search", parameter, callback, output) | ||
} | ||
/*** | ||
* Provide json search result in the callback | ||
* | ||
* @param [Map] parameter query | ||
* @param [Function] callback with search result as json | ||
*/ | ||
json(parameter, callback) { | ||
this.search(parameter, "json", (data) => { | ||
callback(JSON.parse(data)) | ||
}) | ||
} | ||
/*** | ||
* @deprecated | ||
* Provide json search result in the callback | ||
* | ||
* @param [Map] parameter query | ||
* @param [Function] callback with search result as json | ||
* @param [String] api_key | ||
*/ | ||
json(parameter, callback, api_key = null) { | ||
parameter["api_key"] = api_key | ||
this.search(parameter, "json", (data) => { | ||
callback(JSON.parse(data)) | ||
}) | ||
} | ||
/*** | ||
* Provide html search result in the callback | ||
* | ||
* @param [Map] parameter | ||
* @param [Function] callback with json as argument | ||
* @param [String] api_key | ||
*/ | ||
html(parameter, callback, api_key = null) { | ||
this.search(parameter, "html", (data) => { | ||
callback(data) | ||
}, api_key) | ||
} | ||
/*** | ||
* Location API returns matching location in the callback | ||
*/ | ||
location(q, limit, callback) { | ||
let query = { | ||
q: q, | ||
limit: limit | ||
} | ||
this.execute(LOCATION_API, query, (data) => { | ||
callback(JSON.parse(data)) | ||
}, null, null) | ||
} | ||
/*** | ||
* Account API returns account information in the callback | ||
*/ | ||
account(callback) { | ||
this.execute("/account", {}, (data) => { | ||
callback(JSON.parse(data)) | ||
}, null, null) | ||
} | ||
/*** | ||
* Search Archive API returns search result from the archive | ||
* @param search_id previous search result = search_metadata.id | ||
* @param callback handle next step | ||
* @param api_key user secret key | ||
*/ | ||
search_archive(search_id, callback, api_key = null) { | ||
this.execute("/searches/" + search_id + ".json", {}, (data) => { | ||
callback(JSON.parse(data)) | ||
}, null, api_key) | ||
} | ||
} | ||
module.exports.GoogleSearchResults = GoogleSearchResults; | ||
module.exports.GoogleSearchResults = GoogleSearchResults |
{ | ||
"name": "google-search-results-nodejs", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Google Search Results Node JS API via SerpApi.com", | ||
@@ -42,2 +42,2 @@ "main": "lib/GoogleSearchResults.js", | ||
"dependencies": {} | ||
} | ||
} |
@@ -5,3 +5,3 @@ # Google Search Results Node.js | ||
This NodeJS module is designed to scrape and parse Google results using [SERP API](https://serpapi.com). Feel free to fork this repository to add more backends. | ||
This NodeJS module is designed to scrape and parse Google, Bing and Baidu results using [SERP API](https://serpapi.com). Feel free to fork this repository to add more backends. | ||
@@ -53,3 +53,3 @@ This Ruby Gem is meant to scrape and parse Google results using [SerpApi](https://serpapi.com). | ||
- return a standardizes JSON response | ||
The Ruby class GoogleSearchResults | ||
The class GoogleSearchResults | ||
- Format the request to Serp API server | ||
@@ -60,3 +60,8 @@ - Execute GET http request | ||
Alternatively, you can search: | ||
- Bing using BingSearchResults class | ||
- Baidu using BaiduSearchResults class | ||
See the [playground to generate your code.](https://serpapi.com/playground) | ||
## Example | ||
@@ -63,0 +68,0 @@ * [How to set SERP API key](#how-to-set-serp-api-key) |
const expect = require('expect'); | ||
const GSR = require('../lib/GoogleSearchResults'); | ||
const serpapi = require('../lib/GoogleSearchResults'); | ||
@@ -18,3 +18,3 @@ describe('Account API', () => { | ||
const client = new GSR.GoogleSearchResults(api_key) | ||
const client = new serpapi.GoogleSearchResults(api_key) | ||
client.account((data) => { | ||
@@ -21,0 +21,0 @@ expect(data.account_id).toExist |
const expect = require('expect'); | ||
const GSR = require('./../lib/GoogleSearchResults'); | ||
const serpapi = require('./../lib/GoogleSearchResults'); | ||
@@ -7,3 +7,3 @@ describe('Google Search Results', () => { | ||
beforeEach(() => { | ||
p = { | ||
p = { | ||
q: "Coffee", | ||
@@ -18,3 +18,3 @@ location: "Austin, Texas" | ||
it('fail:buildUrl', (done) => { | ||
let client = new GSR.GoogleSearchResults() | ||
let client = new serpapi.GoogleSearchResults() | ||
expect(() => { | ||
@@ -27,4 +27,4 @@ client.buildUrl('/path', {}, "json", null) | ||
it('buildUrl', (done) => { | ||
let client = new GSR.GoogleSearchResults('demo') | ||
expect(client.buildUrl('/path', {q: 'Coffee', location: 'Austin, Texas', api_key: 'beta'}, "json")).toMatch(/https:\/\/serpapi.com\/path\?q=Coffee&location=Austin%2C%20Texas&api_key=beta&source=nodejs&output=json/) | ||
let client = new serpapi.GoogleSearchResults('demo') | ||
expect(client.buildUrl('/path', { q: 'Coffee', location: 'Austin, Texas', api_key: 'beta' }, "json")).toMatch(/https:\/\/serpapi.com\/path\?q=Coffee&location=Austin%2C%20Texas&api_key=beta&source=nodejs&output=json/) | ||
done() | ||
@@ -34,4 +34,4 @@ }) | ||
it('buildUrl without api_key', (done) => { | ||
let client = new GSR.GoogleSearchResults('demo') | ||
expect(client.buildUrl('/path', {q: 'Coffee', location: 'Austin, Texas'}, "json")).toMatch(/https:\/\/serpapi.com\/path\?q=Coffee&location=Austin%2C%20Texas&source=nodejs&output=json&api_key=demo/) | ||
let client = new serpapi.GoogleSearchResults('demo') | ||
expect(client.buildUrl('/path', { q: 'Coffee', location: 'Austin, Texas' }, "json")).toMatch(/https:\/\/serpapi.com\/path\?q=Coffee&location=Austin%2C%20Texas&source=nodejs&output=json&api_key=demo/) | ||
done() | ||
@@ -41,3 +41,3 @@ }) | ||
it("search", (done) => { | ||
let client = new GSR.GoogleSearchResults(api_key) | ||
let client = new serpapi.GoogleSearchResults(api_key) | ||
client.setTimeout(6000); | ||
@@ -52,3 +52,3 @@ client.search(p, "json", (raw) => { | ||
it("json", (done) => { | ||
let client = new GSR.GoogleSearchResults(api_key) | ||
let client = new serpapi.GoogleSearchResults(api_key) | ||
client.json(p, (data) => { | ||
@@ -61,3 +61,3 @@ expect(data.local_results[0].title.length).toBeGreaterThan(5) | ||
it("html", (done) => { | ||
let client = new GSR.GoogleSearchResults(api_key) | ||
let client = new serpapi.GoogleSearchResults(api_key) | ||
client.html(p, (body) => { | ||
@@ -71,3 +71,3 @@ expect(body).toMatch(/<\/html>/) | ||
try { | ||
let client = new GSR.GoogleSearchResults(api_key) | ||
let client = new serpapi.GoogleSearchResults(api_key) | ||
let fn = (data) => { | ||
@@ -74,0 +74,0 @@ done() |
const expect = require('expect'); | ||
const GSR = require('../lib/GoogleSearchResults'); | ||
const serpapi = require('../lib/GoogleSearchResults'); | ||
@@ -11,4 +11,4 @@ describe('Search Archive API', () => { | ||
var client = new GSR.GoogleSearchResults(process.env.API_KEY) | ||
client.json({q: "Coffee", location: "Portland" }, (search_result) => { | ||
var client = new serpapi.GoogleSearchResults(process.env.API_KEY) | ||
client.json({ q: "Coffee", location: "Portland" }, (search_result) => { | ||
// search in archive for the search just returned | ||
@@ -20,3 +20,3 @@ client.search_archive(search_result.search_metadata.id, (archived_search) => { | ||
}) | ||
}) | ||
}).timeout(10000) | ||
}) |
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
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
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
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
19982
15
394
260
8