Fix error with beautifulsoup connection, add result image and text

pull/129/head
bobloy 4 years ago
parent ea0cb8c51b
commit 71aa4c3048

@ -1,9 +1,13 @@
import logging
import aiohttp import aiohttp
import discord import discord
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from redbot.core import commands from redbot.core import commands
from redbot.core.commands import Cog from redbot.core.commands import Cog
log = logging.getLogger("red.fox_v3.chatter")
class LoveCalculator(Cog): class LoveCalculator(Cog):
"""Calculate the love percentage for two users!""" """Calculate the love percentage for two users!"""
@ -28,18 +32,23 @@ class LoveCalculator(Cog):
url = "https://www.lovecalculator.com/love.php?name1={}&name2={}".format( url = "https://www.lovecalculator.com/love.php?name1={}&name2={}".format(
x.replace(" ", "+"), y.replace(" ", "+") x.replace(" ", "+"), y.replace(" ", "+")
) )
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession(headers={"Connection": "keep-alive"}) as session:
async with session.get(url) as response: async with session.get(url) as response:
log.debug(f"{response=}")
assert response.status == 200
soup_object = BeautifulSoup(await response.text(), "html.parser") soup_object = BeautifulSoup(await response.text(), "html.parser")
try: try:
description = ( description = (
soup_object.find("div", attrs={"class": "result__score"}) soup_object.find("div", class_="result__score").get_text().strip()
.get_text()
.strip()
) )
except: except:
description = "Dr. Love is busy right now" description = "Dr. Love is busy right now"
result_image = soup_object.find("img", class_="result__image").get("src")
result_text = soup_object.find("div", class_="result-text").get_text()
result_text = " ".join(result_text.split())
try: try:
z = description[:2] z = description[:2]
z = int(z) z = int(z)
@ -47,11 +56,12 @@ class LoveCalculator(Cog):
emoji = "" emoji = ""
else: else:
emoji = "💔" emoji = "💔"
title = "Dr. Love says that the love percentage for {} and {} is:".format(x, y) title = f"Dr. Love says that the love percentage for {x} and {y} is: {emoji} {description} {emoji}"
except: except:
emoji = ""
title = "Dr. Love has left a note for you." title = "Dr. Love has left a note for you."
description = emoji + " " + description + " " + emoji em = discord.Embed(title=title, description=result_text, color=discord.Color.red())
em = discord.Embed(title=title, description=description, color=discord.Color.red()) if result_image:
em.set_image(url=f"https://www.lovecalculator.com/{result_image}")
await ctx.send(embed=em) await ctx.send(embed=em)

Loading…
Cancel
Save