big rewrite of the star citizen function. No longer using the API, just scraping the wiki

This commit is contained in:
Luke Robles 2023-04-28 13:06:19 -07:00
parent 4c1989fc30
commit 0f172b7f43
3 changed files with 90 additions and 44 deletions

View File

@ -14,7 +14,7 @@ class StarCitizen(commands.Cog):
) )
@option( @option(
name="ship", name="ship",
description="Ship you want info on, supports relatively fuzzy matching, eg. 890 instead of 890 jump", description="Ship you want info on, must be the exact name of the ship, eg Aegs Avenger",
required=True, required=True,
) )
async def star_citizen(self, ctx: commands.Context, ship): async def star_citizen(self, ctx: commands.Context, ship):

View File

@ -9,5 +9,4 @@ openai
owotext owotext
pandas pandas
requests requests
rsi-scraper
wolframalpha wolframalpha

View File

@ -1,5 +1,4 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from rsi_scraper import Ship
import requests import requests
import discord import discord
import pprint import pprint
@ -9,63 +8,111 @@ async def get_ship(ship_name):
base_url = "https://robertspaceindustries.com" base_url = "https://robertspaceindustries.com"
wiki_url = "https://starcitizen.tools/" wiki_url = "https://starcitizen.tools/"
try: # try:
ship = await Ship(name=ship_name).get_ships_pages_async() # # ship = await Ship(name=ship_name).get_ships_pages_async()
ship = ship[0] # # ship = ship[0]
except IndexError: # except IndexError:
embed = discord.Embed(description="", color=discord.Color.red(), type="rich") # embed = discord.Embed(description="❌", color=discord.Color.red(), type="rich")
embed.add_field( # embed.add_field(
name="**Could not find that ship**", # name="**Could not find that ship**",
value="You gave me %s. Did you spell it right? (Its also possible my shitty code isnt working right)" # value="You gave me %s. Did you spell it right? (Its also possible my shitty code isnt working right)"
% ship_name, # % ship_name,
inline=True, # inline=True,
) # )
return embed # return embed
# prints for debug # prints for debug
# pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(ship) # pp.pprint(ship)
ship_picture = ship["media"][0]["images"]["wallpaper_thumb"] # wiki_ship_name = ship["url"].split("/")[-1].replace("-", "_")
wiki_ship_name = ship["url"].split("/")[-1].split("-")[0] wiki_ship_name = ship_name.replace(" ", "_").title()
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)"} headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0"
}
response = requests.get(wiki_url + wiki_ship_name, headers=headers).text response = requests.get(wiki_url + wiki_ship_name, headers=headers).text
soup = BeautifulSoup(response, "html.parser") soup = BeautifulSoup(response, "html.parser")
try:
ingame_price = soup.find(
"div", {"class": "data-buycost infobox-data infobox-col2"}
)
children = ingame_price.findChildren("div")
price_lambda = list(filter(lambda x: ("aUEC" in x.text), children))
ingame_price = price_lambda[0].text
except Exception:
ingame_price = "No in-game price available"
pledge_price = soup.find(
"div", {"class": "data-pledgecost infobox-data infobox-col4"}
)
children = pledge_price.findChildren("div")
real_money_price = list(filter(lambda x: ("$" in x.text), children))
embed = discord.Embed( embed = discord.Embed(
description="-------", color=discord.Color.blue(), type="rich" description="-------", color=discord.Color.blue(), type="rich"
) )
embed.set_thumbnail(url=ship_picture)
embed.set_author(name="Star Citizen API Info about the %s" % ship["name"]) ship_image = (soup.find("a", {"class": "mw-file-description"})).img["src"]
embed.set_thumbnail(url=ship_image)
embed.set_author(name="Star Citizen API Info about the %s" % ship_name)
try:
ingame_price = (
soup.find("div", {"class": "data-buycost infobox-data infobox-col2"})
.find("div", {"class": "infobox-data__value"})
.text
)
except Exception:
ingame_price = "No in-game price available"
embed.add_field(name="**Ingame Price**", value=ingame_price, inline=True) embed.add_field(name="**Ingame Price**", value=ingame_price, inline=True)
embed.add_field(
name="**Pledge Price**", value=real_money_price[0].text, inline=True pledge_price = (
soup.find("div", {"class": "data-pledgecost infobox-data infobox-col4"})
.find("div", {"class": "infobox-data__value"})
.text
) )
embed.add_field(name="**Pledge Price**", value=pledge_price, inline=True)
# embed.add_field(name="-------", value="", inline=False)
# embed.add_field(name="**Description**", value=ship["description"], inline=False)
embed.add_field(name="-------", value="", inline=False) embed.add_field(name="-------", value="", inline=False)
embed.add_field(name="**Description**", value=ship["description"], inline=False)
embed.add_field(name="-------", value="", inline=False) stowage_capacity = (
embed.add_field( soup.find("tr", {"class": "data-stowagespace infobox-data infobox-col2"})
name="**Cargo Capacity**", value="%s SCU" % ship["cargocapacity"], inline=True .findChildren("td")[0]
.text
) )
embed.add_field(name="**Crew Size**", value=ship["max_crew"], inline=True)
embed.add_field(name="**Stowage Capacity**", value=stowage_capacity, inline=True)
cargo_capacity = (
soup.find("tr", {"class": "data-cargocapacity infobox-data infobox-col4"})
.findChildren("td")[0]
.text
)
embed.add_field(name="**Cargo Capacity**", value=cargo_capacity, inline=True)
crew_size = (
soup.find("tr", {"class": "data-crew infobox-data infobox-col4"})
.findChildren("td")[0]
.text
)
embed.add_field(name="**Crew Size**", value=crew_size, inline=True)
embed.add_field(name="-------", value="", inline=False) embed.add_field(name="-------", value="", inline=False)
claim_time = (
soup.find("div", {"class": "data-claimtime infobox-data infobox-col4"})
.find("div", {"class": "infobox-data__value"})
.text
)
expedited_claim_time = (
soup.find("div", {"class": "data-expeditetime infobox-data infobox-col4"})
.find("div", {"class": "infobox-data__value"})
.text
)
expedited_fee = (
soup.find("div", {"class": "data-expeditecost infobox-data infobox-col2"})
.find("div", {"class": "infobox-data__value"})
.text
)
embed.add_field(name="**Insurance claim time**", value=claim_time, inline=True)
embed.add_field(
name="**Expedite claim time**", value=expedited_claim_time, inline=True
)
embed.add_field(name="**Expedite Fee**", value=expedited_fee, inline=True)
embed.add_field( embed.add_field(
name="**Link**", value="%s%s" % (wiki_url, wiki_ship_name), inline=False name="**Link**", value="%s%s" % (wiki_url, wiki_ship_name), inline=False
) )