From 353a80e77e0d95a2489a4eb2dffbcb1ed6036be2 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 16 Jan 2020 12:10:47 -0500 Subject: [PATCH] Fix Google Scrape Search --- assets/json/user-agent.json | 3 +++ commands/search/google.js | 8 +++++--- package.json | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 assets/json/user-agent.json diff --git a/assets/json/user-agent.json b/assets/json/user-agent.json new file mode 100644 index 00000000..95154025 --- /dev/null +++ b/assets/json/user-agent.json @@ -0,0 +1,3 @@ +{ + "agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.118 Safari/537.36" +} diff --git a/commands/search/google.js b/commands/search/google.js index 2a1a1399..438170b2 100644 --- a/commands/search/google.js +++ b/commands/search/google.js @@ -2,6 +2,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); const cheerio = require('cheerio'); const querystring = require('querystring'); +const { agent } = require('../../assets/json/user-agent'); const { GOOGLE_KEY, CUSTOM_SEARCH_ID } = process.env; module.exports = class GoogleCommand extends Command { @@ -43,10 +44,10 @@ module.exports = class GoogleCommand extends Command { let href; const nsfw = msg.channel.nsfw || false; try { - href = await this.customSearch(query, nsfw); + href = await this.searchGoogle(query, nsfw); } catch (err) { try { - href = await this.searchGoogle(query, nsfw); + href = await this.customSearch(query, nsfw); } catch (err2) { href = `http://lmgtfy.com/?iie=1&q=${encodeURIComponent(query)}`; } @@ -61,7 +62,8 @@ module.exports = class GoogleCommand extends Command { .query({ safe: nsfw ? 'off' : 'on', q: query - }); + }) + .set('User-Agent', agent); const $ = cheerio.load(text); let href = $('.r').first().find('a').first().attr('href'); if (!href) return null; diff --git a/package.json b/package.json index 5465772d..decc7f4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "110.0.2", + "version": "110.0.3", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {