Merge pull request #30 from stijndcl/google

Google search & small fixes
pull/35/head 2.3.0
Stijn De Clercq 2021-01-24 13:15:04 +01:00 committed by GitHub
commit ee683404fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 3 deletions

53
cogs/google.py 100644
View File

@ -0,0 +1,53 @@
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"], 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))
# Filter out all Nones
elements = list(filter(lambda x: x is not None, results))
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)
# Cut excess results out
if len(elements) > 10:
elements = elements[:10]
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))

View File

@ -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:

View File

@ -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]))

View File

@ -28,7 +28,7 @@ class Xp(commands.Cog):
embed = discord.Embed(colour=discord.Colour.blue())
embed.set_author(name=target.display_name, icon_url=target.avatar_url)
embed.add_field(name="Aantal Berichten", value="{} ({}%)".format(int(target_stats[11]), perc))
embed.add_field(name="Aantal Berichten", value="{:,} ({}%)".format(int(target_stats[11]), perc))
embed.add_field(name="Level", value=str(xp.calculate_level(target_stats[12])))
embed.add_field(name="XP", value="{:,}".format(int(target_stats[12])))
embed.set_footer(text="*Sinds Didier 2.0 Launch")

View File

@ -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.",

View File

@ -0,0 +1,45 @@
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})
# 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):
"""
Function to find links & titles in the HTML of a <div> element
"""
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"})
return list(getContent(d) for d in divs), 200

View File

@ -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