dragon-bot/app/cogs/trackdays.py
Luke Robles 99776dbae6
All checks were successful
Build and push / changes (push) Successful in 13s
Build and push / Lint-Python (push) Successful in 2s
Build and push / Build-and-Push-Docker (push) Successful in 1m13s
Build and push / sync-argocd-app (push) Successful in 3s
Build and push / post-failure-to-discord (push) Has been skipped
Build and push / post-success-to-discord (push) Successful in 1s
Use ruff and conert stock to fstrings
2025-05-07 15:23:32 -07:00

142 lines
4.4 KiB
Python
Executable File

from discord.ext import commands
import discord
import requests
from bs4 import BeautifulSoup
import datetime
class TrackDays(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
@staticmethod
def get_tracks():
"""
returns a dictionary of all tracks and their URLs
"""
url = "https://www.trackpinata.com/"
response = requests.get(url).text
soup = BeautifulSoup(response, "html.parser")
return {
x.find("h2", class_="thumbnail-header").text: x["href"]
for x in soup.find("div", class_="track-list w-dyn-items").find_all("a")
}
async def get_all_tracks(ctx: discord.AutocompleteContext):
"""
returns a list of all the cali tracks for use in auto-complete
"""
return TrackDays.get_tracks().keys()
@commands.slash_command(
guild_ids=None,
name="trackdays",
description="Look up upcoming trackdays",
contexts={
discord.InteractionContextType.guild,
discord.InteractionContextType.bot_dm,
discord.InteractionContextType.private_channel,
},
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install,
},
)
async def trackdays_lookup(
self,
ctx: discord.ApplicationContext,
track: discord.Option(
str,
autocomplete=discord.utils.basic_autocomplete(get_all_tracks),
description="Track to look up days for",
),
):
tracks = self.get_tracks()
track_url = tracks.get(track)
if not track_url:
await ctx.respond(f"Track {track} not found")
return
base_url = "https://www.trackpinata.com"
full_url = f"{base_url}{track_url}"
await ctx.defer()
data = {}
response = requests.get(full_url).text
soup = BeautifulSoup(response, "html.parser")
months = soup.find_all("h4", class_="month")
# Create a month order mapping
month_order = {
"January": 1,
"February": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"August": 8,
"September": 9,
"October": 10,
"November": 11,
"December": 12,
}
current_month = datetime.datetime.now().month
datetime.datetime.now().year
for month in months:
month_name = month.text
month_num = month_order[month_name]
# Skip if month is in the past
if month_num < current_month:
continue
data[month_name] = {"events": []}
events = month.find_next(
"div", class_="collection-list-wrapper w-dyn-list"
).find_all("a", class_="list-item w-inline-block")
for event in events:
data[month_name]["events"].append(
{
"reg_url": base_url + event["href"],
"provider": event.find("div", class_="text-block").text,
"day": event.find("div", class_="text-block-5").text
+ " "
+ month_name
+ " "
+ event.find("div", class_="text-block-6").text,
}
)
embed = discord.Embed(description="", color=discord.Color.blue(), type="rich")
embed.set_author(name=track)
embed.set_thumbnail(
url="https://t4.ftcdn.net/jpg/02/80/57/05/360_F_280570531_y52gDRp2ce9YSno3tfuIqKoRcEbn5Eau.jpg"
)
for month in sorted(data.keys(), key=lambda x: month_order[x]):
if data[month]["events"]: # Only add field if there are events
embed.add_field(
name=f"🏁 {month} 🏁\n---------",
value="\n".join(
f"[{event['provider']}: {event['day']}]({event['reg_url']})"
for event in data[month]["events"]
),
inline=False,
)
embed.set_footer(text="Pulled from https://www.trackpinata.com")
await ctx.send_followup(embed=embed)
def setup(bot):
bot.add_cog(TrackDays(bot))