diff --git a/cogs/google.py b/cogs/google.py new file mode 100644 index 0000000..10e9475 --- /dev/null +++ b/cogs/google.py @@ -0,0 +1,26 @@ +import discord +from discord.ext import commands +from decorators import help +from enums.help_categories import Category +from functions.scraping import google_search + + +class Google(commands.Cog): + def __init__(self, client): + self.client = client + + # Don't allow any commands to work when locked + def cog_check(self, ctx): + return not self.client.locked + + @commands.command(name="Google", aliases=["Gtfm", "Search"]) + @help.Category(Category.Other) + async def google(self, ctx, *query): + results, status = google_search(" ".join(query)) + + if results is None: + return await ctx.send("Er ging iets fout (Response {})".format(status)) + + +def setup(client): + client.add_cog(Google(client)) diff --git a/cogs/oneliners.py b/cogs/oneliners.py index a6e64b7..3b7c3e6 100644 --- a/cogs/oneliners.py +++ b/cogs/oneliners.py @@ -105,7 +105,7 @@ class Oneliners(commands.Cog): async def todo(self, ctx, *args): await ctx.send("https://trello.com/b/PdtsAJea/didier-to-do-list") - @commands.command(name="LMGTFY", aliases=["Dsfr", "Gtfm", "Google"], usage="[Query]") + @commands.command(name="LMGTFY", aliases=["Dsfr"], usage="[Query]") @help.Category(category=Category.Other) async def lmgtfy(self, ctx, *, query=None): if query: diff --git a/cogs/store.py b/cogs/store.py index dfca206..b9a00fb 100644 --- a/cogs/store.py +++ b/cogs/store.py @@ -84,6 +84,9 @@ class Store(commands.Cog): item_tuple = item break + if amount.lower() == "all": + amount = int(item_tuple[2]) + if int(item_tuple[2]) < amount: return await ctx.send("Je hebt niet zoveel {}s.".format(item_tuple[1])) diff --git a/functions/scraping.py b/functions/scraping.py new file mode 100644 index 0000000..b95108c --- /dev/null +++ b/functions/scraping.py @@ -0,0 +1,32 @@ +from requests import get +from urllib.parse import urlencode +from bs4 import BeautifulSoup + +# TODO add Football requests in here as well + + +def google_search(query): + """ + Function to get Google search results + """ + headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36' + } + + query = urlencode({"q": query}) + + resp = get("https://www.google.com/search?{}&num=10&hl=en".format(query), headers=headers) + print("got here") + 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 + + divs = bs.find_all('div', attrs={'class': 'g'}) + print(divs) + return list(getContent(d) for d in divs), 200