Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ris): add support for direct image upload #33

Merged
merged 4 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ console.log(images);

// Reverse Image Search
const reverse = await google.search('https://i.pinimg.com/236x/92/16/d9/9216d9a222ef65eb6eabfff1970180d1.jpg', { ris: true });

// Or simply search the image directly (more accurate)
const my_awesome_image = fs.readFileSync('./wow.png');
const reverse = await google.search(my_awesome_image, { ris: true });

console.log(reverse.results);
```

Expand Down
53 changes: 38 additions & 15 deletions lib/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,47 @@ const Time = require('./nodes/Time');
const PAA = require('./nodes/PAA');
const PAS = require('./nodes/PAS');

const FormData = require('form-data');

/**
* Search a given query on Google.
* @param {string} query - search query
* @param {string | object} query - search query
* @param {object} [options] search options
* @param {boolean} [options.ris] - use reverse image search
* @param {boolean} [options.safe] - safe search
* @param {number} [options.page] - pagination
* @param {object} [options.additional_params] - parameters that will be passed to Google
*/
async function search(query, options = {}) {
const _query = query.trim().split(/ +/).join('+');

let response;
const ris = options.ris || false;
const safe = options.safe || false;
const page = options.page ? options.page * 10 : 0;
const additional_params = options.additional_params || null;

const url = encodeURI(
ris ?
`${Constants.URLS.W_GOOGLE}searchbyimage?image_url=${_query}`:
`${Constants.URLS.GOOGLE}search?q=${_query}&ie=UTF-8&aomd=1${(safe ? '&safe=active' : '')}&start=${page}`
);

const response = await Axios.get(url, {
params: additional_params,
headers: Utils.getHeaders({ mobile: true })
}).catch((err) => err);

if (typeof query === 'object' && ris) {
response = await uploadImage(query);
} else {
const _query = query.trim().split(/ +/).join('+');

const url = encodeURI(
ris ?
`${Constants.URLS.W_GOOGLE}searchbyimage?image_url=${_query}`:
`${Constants.URLS.GOOGLE}search?q=${_query}&ie=UTF-8&aomd=1${(safe ? '&safe=active' : '')}&start=${page}`
);

response = await Axios.get(url, {
params: additional_params,
headers: Utils.getHeaders({ mobile: true })
}).catch((err) => err);
}

if (response instanceof Error)
throw new Utils.SearchError('Could not execute search', {
status_code: response?.status || 0, message: response?.message
});

const $ = Cheerio.load(Utils.refineData(response.data));

const results = {};
Expand Down Expand Up @@ -81,6 +89,21 @@ async function search(query, options = {}) {
return results;
}

async function uploadImage(buffer) {
const form_data = new FormData();

form_data.append('encoded_image', buffer);

const response = await Axios.post(`${Constants.URLS.GIS}searchbyimage/upload`, form_data, {
headers: {
...form_data.getHeaders(),
...Utils.getHeaders({ mobile: true })
}
});

return response;
}

/**
* Google image search.
*
Expand Down
102 changes: 100 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "googlethis",
"version": "1.3.0",
"version": "1.4.0",
"description": "A simple yet powerful module to retrieve organic search results and much more from Google.",
"main": "lib/index.js",
"author": "LuanRT <[email protected]> (https://github.com/LuanRT)",
Expand All @@ -25,6 +25,7 @@
"dependencies": {
"axios": "^0.21.1",
"cheerio": "1.0.0-rc.10",
"form-data": "^4.0.0",
"unraw": "^2.0.1"
},
"repository": {
Expand Down