From 9ef5836fa80138b8cd71229ac6c484bf3570ec8a Mon Sep 17 00:00:00 2001 From: bobloy Date: Thu, 30 Apr 2020 21:02:45 -0400 Subject: [PATCH 1/3] WIP fix to aiohttp payload error --- lovecalculator/lovecalculator.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lovecalculator/lovecalculator.py b/lovecalculator/lovecalculator.py index 3a83cab..2038ccc 100644 --- a/lovecalculator/lovecalculator.py +++ b/lovecalculator/lovecalculator.py @@ -22,18 +22,19 @@ class LoveCalculator(Cog): x = lover.display_name y = loved.display_name - url = "https://www.lovecalculator.com/love.php?name1={}&name2={}".format( - x.replace(" ", "+"), y.replace(" ", "+") - ) + url = f"https://www.lovecalculator.com/love.php?name1={x}&name2={y}" + async with aiohttp.ClientSession() as session: async with session.get(url) as response: - soup_object = BeautifulSoup(await response.text(), "html.parser") - try: - description = ( - soup_object.find("div", attrs={"class": "result__score"}).get_text().strip() - ) - except: - description = "Dr. Love is busy right now" + resp = await response.text() + + soup_object = BeautifulSoup(resp, "html.parser") + try: + description = soup_object.find("div", attrs={"class": "result__score"}).get_text().strip() + img = soup_object.find("img", attrs={"class": "result__image"})['src'] + except: + description = "Dr. Love is busy right now" + img = None try: z = description[:2] @@ -48,5 +49,5 @@ class LoveCalculator(Cog): title = "Dr. Love has left a note for you." description = emoji + " " + description + " " + emoji - em = discord.Embed(title=title, description=description, color=discord.Color.red()) + em = discord.Embed(title=title, description=description, color=discord.Color.red(), url=img) await ctx.send(embed=em) From 71aa4c3048c409265816214ac40fc099f84f7604 Mon Sep 17 00:00:00 2001 From: bobloy Date: Mon, 31 Aug 2020 11:30:18 -0400 Subject: [PATCH 2/3] Fix error with beautifulsoup connection, add result image and text --- lovecalculator/lovecalculator.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lovecalculator/lovecalculator.py b/lovecalculator/lovecalculator.py index 1b7e5c1..d43146d 100644 --- a/lovecalculator/lovecalculator.py +++ b/lovecalculator/lovecalculator.py @@ -1,9 +1,13 @@ +import logging + import aiohttp import discord from bs4 import BeautifulSoup from redbot.core import commands from redbot.core.commands import Cog +log = logging.getLogger("red.fox_v3.chatter") + class LoveCalculator(Cog): """Calculate the love percentage for two users!""" @@ -28,18 +32,23 @@ class LoveCalculator(Cog): url = "https://www.lovecalculator.com/love.php?name1={}&name2={}".format( 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: + log.debug(f"{response=}") + assert response.status == 200 soup_object = BeautifulSoup(await response.text(), "html.parser") try: description = ( - soup_object.find("div", attrs={"class": "result__score"}) - .get_text() - .strip() + soup_object.find("div", class_="result__score").get_text().strip() ) except: 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: z = description[:2] z = int(z) @@ -47,11 +56,12 @@ class LoveCalculator(Cog): emoji = "❤" else: 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: - emoji = "" title = "Dr. Love has left a note for you." - description = emoji + " " + description + " " + emoji - em = discord.Embed(title=title, description=description, color=discord.Color.red()) + em = discord.Embed(title=title, description=result_text, color=discord.Color.red()) + if result_image: + em.set_image(url=f"https://www.lovecalculator.com/{result_image}") + await ctx.send(embed=em) From c74736966733b0e8bf34436d718213253227a503 Mon Sep 17 00:00:00 2001 From: bobloy Date: Mon, 31 Aug 2020 11:37:46 -0400 Subject: [PATCH 3/3] Merge missing changes, better error handling --- lovecalculator/lovecalculator.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lovecalculator/lovecalculator.py b/lovecalculator/lovecalculator.py index 4425cf7..95e9f97 100644 --- a/lovecalculator/lovecalculator.py +++ b/lovecalculator/lovecalculator.py @@ -36,14 +36,16 @@ class LoveCalculator(Cog): async with session.get(url) as response: assert response.status == 200 resp = await response.text() + log.debug(f"{resp=}") soup_object = BeautifulSoup(resp, "html.parser") - try: - description = ( - soup_object.find("div", class_="result__score").get_text().strip() - ) - except: + + description = soup_object.find("div", class_="result__score").get_text() + + if description is None: description = "Dr. Love is busy right now" + else: + description = description.strip() result_image = soup_object.find("img", class_="result__image").get("src") @@ -61,6 +63,11 @@ class LoveCalculator(Cog): except: title = "Dr. Love has left a note for you." - em = discord.Embed(title=title, description=result_text, color=discord.Color.red(), url=f"https://www.lovecalculator.com/{result_image}") + em = discord.Embed( + title=title, + description=result_text, + color=discord.Color.red(), + url=f"https://www.lovecalculator.com/{result_image}", + ) await ctx.send(embed=em)