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= BITLY_KEY=
CLEARBIT_KEY= CLEARBIT_KEY=
CLEVERBOT_KEY= CLEVERBOT_KEY=
CUSTOM_SEARCH_ID=
DEVIANTART_ID= DEVIANTART_ID=
DEVIANTART_SECRET= DEVIANTART_SECRET=
FACEPLUSPLUS_KEY= FACEPLUSPLUS_KEY=
+22 -14
View File
@@ -1,6 +1,6 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const request = require('node-superfetch'); const request = require('node-superfetch');
const { GOOGLE_KEY, CUSTOM_SEARCH_ID } = process.env; const cheerio = require('cheerio');
module.exports = class GoogleCommand extends Command { module.exports = class GoogleCommand extends Command {
constructor(client) { constructor(client) {
@@ -14,8 +14,7 @@ module.exports = class GoogleCommand extends Command {
{ {
name: 'Google', name: 'Google',
url: 'https://www.google.com/', url: 'https://www.google.com/',
reason: 'Custom Search API', reason: 'Search'
reasonURL: 'https://cse.google.com/cse/all'
}, },
{ {
name: 'LMGTFY', name: 'LMGTFY',
@@ -39,9 +38,8 @@ module.exports = class GoogleCommand extends Command {
async run(msg, { query }) { async run(msg, { query }) {
let href; let href;
const nsfw = msg.channel.nsfw || false;
try { try {
href = await this.customSearch(query, nsfw); href = await this.search(query, msg.channel.nsfw || false);
} catch { } catch {
href = `http://lmgtfy.com/?iie=1&q=${encodeURIComponent(query)}`; href = `http://lmgtfy.com/?iie=1&q=${encodeURIComponent(query)}`;
} }
@@ -49,16 +47,26 @@ module.exports = class GoogleCommand extends Command {
return msg.say(href); return msg.say(href);
} }
async customSearch(query, nsfw) { async search(query, nsfw) {
const { body } = await request const { text } = await request
.get('https://www.googleapis.com/customsearch/v1') .get('https://www.google.com/search')
.query({ .query({
key: GOOGLE_KEY, safe: nsfw ? 'images' : 'active',
cx: CUSTOM_SEARCH_ID, pws: 0,
safe: nsfw ? 'off' : 'active', filter: 0,
q: query q: query
}); })
if (!body.items) return null; .set({ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' });
return body.items[0].formattedUrl; 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", "name": "xiao",
"version": "132.1.0", "version": "132.1.1",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {