dragon-bot/app/excuse.py
Luke R 928b1cad07
All checks were successful
Build and push / changes (push) Successful in 3s
Build and push / Lint-Python (push) Successful in 3s
Build and push / Build-and-Push-Docker (push) Successful in 20s
Build and push / sync-argocd-app (push) Successful in 2s
Build and push / post-status-to-discord (push) Successful in 2s
make excuse use beautiful soup
2025-02-26 15:11:13 -08:00

29 lines
915 B
Python
Executable File

from bs4 import BeautifulSoup
import requests
def get_excuse():
"""
Fetches a random developer excuse from devexcuses.com
Returns:
str: A formatted excuse string wrapped in code block
"""
url = "http://www.devexcuses.com"
response = requests.get(url, headers={"Cache-Control": "no-cache"})
response.raise_for_status() # Raise exception for bad responses
soup = BeautifulSoup(response.content, features="html.parser")
excuse_element = soup.find("p", {"class": "excuse"})
if not excuse_element or not excuse_element.contents:
return "```Failed to get an excuse. Maybe that's your excuse?```"
# Extract the excuse text more reliably using the <a> tag inside the <p>
excuse_link = excuse_element.find("a")
excuse_text = (
excuse_link.text.strip() if excuse_link else excuse_element.text.strip()
)
return f"```{excuse_text}```"