Change google to scraping

This commit is contained in:
Dragon Fire
2021-03-11 19:11:03 -05:00
parent 815605ac2a
commit 015b3e963f
3 changed files with 23 additions and 16 deletions
-1
View File
@@ -38,7 +38,6 @@ ANILIST_USERNAME=
BITLY_KEY=
CLEARBIT_KEY=
CLEVERBOT_KEY=
CUSTOM_SEARCH_ID=
DEVIANTART_ID=
DEVIANTART_SECRET=
FACEPLUSPLUS_KEY=
+22 -14
View File
@@ -1,6 +1,6 @@
const Command = require('../../structures/Command');
const request = require('node-superfetch');
const { GOOGLE_KEY, CUSTOM_SEARCH_ID } = process.env;
const cheerio = require('cheerio');
module.exports = class GoogleCommand extends Command {
constructor(client) {
@@ -14,8 +14,7 @@ module.exports = class GoogleCommand extends Command {
{
name: 'Google',
url: 'https://www.google.com/',
reason: 'Custom Search API',
reasonURL: 'https://cse.google.com/cse/all'
reason: 'Search'
},
{
name: 'LMGTFY',
@@ -39,9 +38,8 @@ module.exports = class GoogleCommand extends Command {
async run(msg, { query }) {
let href;
const nsfw = msg.channel.nsfw || false;
try {
href = await this.customSearch(query, nsfw);
href = await this.search(query, msg.channel.nsfw || false);
} catch {
href = `http://lmgtfy.com/?iie=1&q=${encodeURIComponent(query)}`;
}
@@ -49,16 +47,26 @@ module.exports = class GoogleCommand extends Command {
return msg.say(href);
}
async customSearch(query, nsfw) {
const { body } = await request
.get('https://www.googleapis.com/customsearch/v1')
async search(query, nsfw) {
const { text } = await request
.get('https://www.google.com/search')
.query({
key: GOOGLE_KEY,
cx: CUSTOM_SEARCH_ID,
safe: nsfw ? 'off' : 'active',
safe: nsfw ? 'images' : 'active',
pws: 0,
filter: 0,
q: query
});
if (!body.items) return null;
return body.items[0].formattedUrl;
})
.set({ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' });
const $ = cheerio.load(text);
const links = [];
$('body').find('h3').each((i, h3) => {
if ($(h3).parent()) {
const href = $(h3).parent().attr('href');
if (href) {
links.push(href);
}
}
});
return links[0];
}
};
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "132.1.0",
"version": "132.1.1",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {