devsnek / googlebot

google for discord, yo
Other
46 stars 10 forks source link

[Request/Question] Identical code but different results. Custom search engine setup perhaps? #9

Closed favna closed 7 years ago

favna commented 7 years ago

I'll just cut right to the case here. A week or so back I "ported" your google search code to my own bot so my server could have a "1 bot solution". Did a bunch of research on how it works with custom search engines etc. Anyway, I notice that your bot seemingly always returns different results to mine, and since my code is nigh the same except not modular it got me to the conclusion it nearly must be the setup of the custom search engine.

My custom search engine setup:

image

Example of a different result:

image

Note: for image search I have a different engine setup with Image Search enabled and schema.org "ImageObject"

Edit: My google search engine code:

const superagent = require('superagent');
const cheerio = require('cheerio');
const querystring = require('querystring');
const request = require("request");
const settings = require("./auth.json");
const googleapikey = settings.googleapikey;
const searchEngineKey = settings.searchEngineKey;

client.on("message", msg => {

var content = msg.content.toLowerCase();

    if (content.startsWith(delimiter + "google")) {
        let searchQuery = content.slice(8);
        let safe = 'high';
        let QUERY_PARAMS = {
            key: googleapikey,
            cx: searchEngineKey,
            safe,
            q: encodeURI(searchQuery),
        };

        msg.channel.sendMessage('`Searching...`').then((botMessage) => {
            return superagent.get(`https://www.googleapis.com/customsearch/v1?${querystring.stringify(QUERY_PARAMS)}`)
                .then((res) => {
                    if (res.body.queries.request[0].totalResults === '0') return Promise.reject(new Error('NO RESULTS'));
                    return botMessage.edit(res.body.items[0].link);
                })
                .catch(() => {
                    const SEARCH_URL = `https://www.google.com/search?safe=${safe}&q=${encodeURI(searchQuery)}`;
                    return superagent.get(SEARCH_URL).then((res) => {
                        const $ = cheerio.load(res.text);
                        let href = $('.r').first().find('a').first().attr('href');
                        if (!href) return Promise.reject(new Error('NO RESULTS'));
                        href = querystring.parse(href.replace('/url?', ''));
                        return botMessage.edit(href.q);
                    })
                })
                .catch((err) => {
                    console.error(err);
                    botMessage.edit('**No Results Found!**');
                });
        });
    }
}
ghost commented 7 years ago

Before Google bot does simple search it does knowledge-graph search. So if knowledge-graph errored it goes to search and your code goes to search instantly. https://github.com/GusCaplan/googlebot/blob/rewrite/src/events/message.js#L28-L38 Problem solved kthxbye

devsnek commented 7 years ago

it is also worth mentioning google skews results for cse like it does for a single user.

favna commented 7 years ago

Well I guess I will be including knowledgegraph in my code then. Thanks you two :) I'll close the issue with this comment.