From 9d4e682cf996a258e6de1d1e91d07224affe5b61 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Sun, 10 Nov 2024 21:14:35 -0800 Subject: [PATCH] Reduce copy pasted code for getting the ship dimensions --- app/star_citizen.py | 47 ++++++++++++--------------------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/app/star_citizen.py b/app/star_citizen.py index 78b10c23..aecbb827 100755 --- a/app/star_citizen.py +++ b/app/star_citizen.py @@ -413,42 +413,19 @@ async def get_ship(ship_name): embed.add_field(name="**Expedite Fee**", value=expedite_fee, inline=True) embed.add_field(name="-------", value="", inline=False) - try: - length = ( - soup.find( - "div", {"class": "template-dimensions-label"}, string="Length" + dimensions = ["Length", "Width", "Height"] + for dimension in dimensions: + try: + value = ( + soup.find( + "div", {"class": "template-dimensions-label"}, string=dimension + ) + .findNext("div", {"class": "template-dimensions-data"}) + .text ) - .findNext("div", {"class": "template-dimensions-data"}) - .text - ) - except Exception: - length = "N/A" - - embed.add_field(name="**Length**", value=length, inline=True) - - try: - width = ( - soup.find("div", {"class": "template-dimensions-label"}, string="Width") - .findNext("div", {"class": "template-dimensions-data"}) - .text - ) - except Exception: - width = "N/A" - - embed.add_field(name="**Width**", value=width, inline=True) - - try: - height = ( - soup.find( - "div", {"class": "template-dimensions-label"}, string="Height" - ) - .findNext("div", {"class": "template-dimensions-data"}) - .text - ) - except Exception: - height = "N/A" - - embed.add_field(name="**Height**", value=height, inline=True) + except Exception: + value = "N/A" + embed.add_field(name=f"**{dimension}**", value=value, inline=True) embed.add_field( name="**Link**", value="%s%s" % (wiki_url, wiki_ship_name), inline=False