From 83f28d9e21dea688c56516eac66cbabe7c142664 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sun, 24 Jan 2021 13:08:58 +0100 Subject: [PATCH] Change discord version to 1.6.0, Google search (fixes #18) --- cogs/google.py | 27 ++++++++++++++++++++++++++- files/help.json | 1 + functions/scraping.py | 22 +++++++++++++++------- requirements.txt | 2 +- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/cogs/google.py b/cogs/google.py index 10e9475..9248318 100644 --- a/cogs/google.py +++ b/cogs/google.py @@ -13,14 +13,39 @@ class Google(commands.Cog): def cog_check(self, ctx): return not self.client.locked - @commands.command(name="Google", aliases=["Gtfm", "Search"]) + @commands.command(name="Google", aliases=["Gtfm", "Search"], usage="[Query]", case_insensitive=True) @help.Category(Category.Other) async def google(self, ctx, *query): + if not query: + return await ctx.reply("Je hebt geen query opgegeven.", mention_author=True) + results, status = google_search(" ".join(query)) if results is None: return await ctx.send("Er ging iets fout (Response {})".format(status)) + elements = list(filter(lambda x: x is not None, results)) + + if len(elements) > 10: + elements = elements[:10] + + embed = discord.Embed(colour=discord.Colour.blue()) + embed.set_author(name="Google Search") + + # Empty list of results + if len(elements) == 0: + embed.description = "Geen resultaten gevonden." + return await ctx.reply(embed=embed, mention_author=False) + + links = [] + + for index, (link, title) in enumerate(elements): + links.append("{}: [{}]({})".format(index + 1, title, link)) + + embed.description = "\n".join(links) + + await ctx.reply(embed=embed, mention_author=False) + def setup(client): client.add_cog(Google(client)) diff --git a/files/help.json b/files/help.json index 09a7e78..41f088d 100644 --- a/files/help.json +++ b/files/help.json @@ -41,6 +41,7 @@ "github": "Stuurt de GitHub link van [Persoon] indien je iemand tagt, zoniet een lijst van GitHubs van mensen uit deze Discord.\nIndien je jouw eigen GitHub hier graag bij zou zetten, gebruik je ``GitHub Add`` of kan ke een DM sturen naar DJ STIJN.", "github add": "Voegt jouw eigen GitHub toe aan de lijst.\nZowel ``github.com/username`` als ``username`` werken.", "give": "Geef [Persoon] [Aantal] van jouw eigen Didier Dinks.", + "google": "Geeft de eerste 10 zoekresultaten voor [Query] op Google.", "hangman": "Raad [Letter]. Indien je geen letter opgeeft, toont het de status van de huidige Hangman game indien er een bezig is.", "hangman start": "Start een nieuwe Hangman game indien er nog geen bezig is. Indien je geen woord opgeeft, wordt er een willekeurig woord gekozen.\n**Indien je wel een woord opgeeft, werkt dit enkel in DM.**", "hangman guess": "Probeer het woord te raden.", diff --git a/functions/scraping.py b/functions/scraping.py index b95108c..7b41bce 100644 --- a/functions/scraping.py +++ b/functions/scraping.py @@ -15,18 +15,26 @@ def google_search(query): query = urlencode({"q": query}) - resp = get("https://www.google.com/search?{}&num=10&hl=en".format(query), headers=headers) - print("got here") + # Get 20 results in case some of them are None + resp = get("https://www.google.com/search?{}&num=20&hl=en".format(query), headers=headers) + if resp.status_code != 200: return None, resp.status_code bs = BeautifulSoup(resp.text, "html.parser") def getContent(element): - link = element.find('a', href=True) - title = element.find('h3') - return link, title + link = element.find("a", href=True) + title = element.find("h3") + if link is None or title is None: + return None + + sp = title.find("span") + + if sp is None: + return None + + return link["href"], sp.text + divs = bs.find_all("div", attrs={"class": "g"}) - divs = bs.find_all('div', attrs={'class': 'g'}) - print(divs) return list(getContent(d) for d in divs), 200 diff --git a/requirements.txt b/requirements.txt index 9942ebb..1d65e0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ dotenv==0.14.0 beautifulsoup4==4.9.1 -discord.py==1.5.0 +discord.py==1.6.0 psycopg2==2.8.5 psycopg2-binary==2.8.5 python-dateutil==2.6.1