Socket
Socket
Sign inDemoInstall

google-sr

Package Overview
Dependencies
16
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.4 to 2.0.0

index.d.ts

146

index.js

@@ -1,67 +0,85 @@

const cheerio = require('cheerio')
const fetch = require('node-fetch')
const querystring = require('querystring')
const { getDescription, parseUrl, PrepareUrl } = require('./utils.js');
let DescriptionSelector = '#main > div > div > div > div:not(.v9i61e) > div.AP7Wnd'
let linkSelector = 'div.ZINbbc > div:nth-child(1) > a'
let TitleSelector = 'div.ZINbbc > div:nth-child(1) > a > h3'
async function search(query, options = {}) {
const Poptions = {
query: query,
safeMode: options.safe ? options.safe !== 'active' ? false : 'active' : options.safe !== false ? 'active' : 'false',
page: options.page ? options.page : false
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.search = exports.defaultSelectors = exports.REQUEST_URL = void 0;
const axios_1 = __importDefault(require("axios"));
const cheerio_1 = require("cheerio");
exports.REQUEST_URL = "https://www.google.com/search";
// UTILITY: Get the url to scrape
function getURL(query, options = {}) {
const params = new URLSearchParams();
params.append("q", query);
// if safe mode is enabled, we need to add the safe mode parameter
if (options.safeMode) {
params.append("safe", "active");
}
let URL = PrepareUrl(Poptions)
if(options.selectors){
if(options.selectors.linkSelector) linkSelector = options.selectors.linkSelector
if(options.selectors.DescriptionSelector) DescriptionSelector = options.selectors.DescriptionSelector
if(options.selectors.TitleSelector) linkSelector = options.selectors.TitleSelector
}
const resp = await fetch(URL).then(r => r.text())
const $ = cheerio.load(resp);
const response = {
url: URL,
searchResults: [],
raw: resp,
// if the user defined a page number, we need to add it to the query as the start parameter
if (options.page) {
params.append("start", options.page.toString());
}
const titles = $(TitleSelector).contents();
titles.each((index, elem) => {
if (elem.data) {
response.searchResults.push({ title: elem.data });
} else {
response.searchResults.push({ title: elem.children[0].data });
}
});
$(linkSelector).map((index, elem) => {
if (index < response.searchResults.length) {
response.searchResults[index] = Object.assign(response.searchResults[index], {
link: parseUrl(elem.attribs.href)
});
}
});
$(DescriptionSelector).map((index, elem) => {
if (index < response.searchResults.length) {
response.searchResults[index] = Object.assign(response.searchResults[index], {
description: getDescription(elem),
});
}
});
return response;
return `${exports.REQUEST_URL}?${params.toString()}`;
}
module.exports = search;
// UTILITY: Parses a link and formats it
function parseLink(link) {
if (!link || typeof link !== "string")
throw new Error(`Link must be a string received ${typeof link}`);
link = link.replace('/url?q=', '');
return link;
}
// html selectors for google.com search results
let defaultSelectors = {
DescriptionSelector: '#main > div > div > div > div:not(.v9i61e) > div.AP7Wnd',
LinkSelector: 'div.ZINbbc > div:nth-child(1) > a',
TitleSelector: 'div.ZINbbc > div:nth-child(1) > a > h3'
};
exports.defaultSelectors = defaultSelectors;
const defaultOptions = {
requestOptions: {},
safeMode: true,
selectors: defaultSelectors,
};
function search(query, options = defaultOptions) {
if (!query || typeof query !== "string")
throw new Error(`Query must be a string received ${typeof query}`);
if (options && typeof options !== "object")
throw new Error(`Options must be an object received ${typeof options}`);
const MergedOptions = Object.assign(Object.assign({}, defaultOptions), options);
const url = getURL(query, MergedOptions);
return new Promise((resolve, reject) => {
axios_1.default.get(url, MergedOptions.requestOptions || {})
.then(response => {
const selectors = MergedOptions.selectors || defaultSelectors;
const $ = (0, cheerio_1.load)(response.data);
const Parsed = [];
$(selectors.DescriptionSelector).each((i, elem) => {
const Description = $(elem).text();
Parsed.push({ Description });
});
$(selectors.LinkSelector).each((i, elem) => {
if (Parsed[i]) {
const Link = $(elem).attr("href");
if (Link) {
Parsed[i].Link = parseLink(Link);
}
else {
Parsed[i].Link = undefined;
}
}
});
$(selectors.TitleSelector).each((i, elem) => {
if (Parsed[i]) {
const Title = $(elem).text();
Parsed[i].Title = Title;
}
});
resolve(Parsed);
})
.catch(error => {
reject(error);
});
});
}
exports.search = search;
exports.default = search;
{
"name": "google-sr",
"version": "1.0.4",
"description": "Yet another library to do google a search, easy and simple",
"version": "2.0.0",
"description": "Yet another library to do google searches, easy and simple",
"repository": "typicalninja493/google-sr",
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"cheerio": "^1.0.0-rc.10",
"node-fetch": "^2.6.1",
"querystring": "^0.2.1"
"axios": "^0.26.0",
"cheerio": "^1.0.0-rc.10"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["google-sr", "google", "search"],
"keywords": [
"google-sr",
"google",
"search",
"google-search",
"simple"
],
"author": "typicalninja493",
"license": "ISC"
"license": "MIT"
}

@@ -7,17 +7,25 @@ ## Google-sr

## Usage
## Install
### with .then
#### Without safe mode
```
const search = require('google-sr');
search('node.js', { safe: false }).then(r => console.log(r))
npm install google-sr
```
#### With safe mode
* Typescript and Javascript compatible
## Usage
#### with `.then`
##### Without safe mode
```ts
import search from 'google-sr'
search('node.js', { safeMode: false }).then(r => console.log(r))
```
const search = require('google-sr');
##### With safe mode
```ts
import search from 'google-sr'
search('node.js').then(r => console.log(r))

@@ -28,10 +36,11 @@ ```

```
const search = require('google-sr');
```ts
import search from 'google-sr'
const res = await search('node.js')
// search('node.js', { safe: false }) for without safe mode
console.log(res)
// use an iife to use await
(async () => {
const result = await search('node.js')
console.log(result)
// search('node.js', { safeMode: false }) for without safe mode
})()
```

@@ -42,4 +51,8 @@

* Page - default:- **1**
* Selectors - `DescriptionSelector`, `linkSelector`, `TitleSelector`
* safe - default:- `true`
* Selectors - {
`DescriptionSelector`,
`linkSelector`,
`TitleSelector`
} - default: `DefaultSelectors`
* safeMode - default :- `true`

@@ -51,6 +64,7 @@ selectors default -

* selectors **MUST** be in the object options.selectors
ex- options.selectors.DescriptionSelector = 'some-selector'
# Support
Join my discord server for support: [discord](https://discord.gg/9s52pz6nWX)
Join my discord server for support: [discord](https://discord.gg/9s52pz6nWX)
# License
This repository and the code inside it is licensed under the MIT License. Read [LICENSE](https://github.com/typicalninja493/google-sr/blob/master/LICENSE) for more information.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc