Skip to content

Commit

Permalink
Update AoC leaderboard upload ot use new paste service
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Dec 1, 2023
1 parent 7ac6730 commit 327ae18
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions bot/exts/advent_of_code/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import arrow
import discord
from discord.ext import commands
from pydis_core.utils import paste_service

import bot
from bot.bot import SirRobin
Expand All @@ -19,9 +20,6 @@

log = logging.getLogger(__name__)

PASTE_URL = "https://paste.pythondiscord.com/documents"
RAW_PASTE_URL_TEMPLATE = "https://paste.pythondiscord.com/raw/{key}"

# Base API URL for Advent of Code Private Leaderboards
AOC_API_URL = "https://adventofcode.com/{year}/leaderboard/private/view/{leaderboard_id}.json"
AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"}
Expand Down Expand Up @@ -289,18 +287,20 @@ async def _fetch_leaderboard_data() -> dict[str, Any]:

async def _upload_leaderboard(leaderboard: str) -> str:
"""Upload the full leaderboard to our paste service and return the URL."""
async with aiohttp.request("POST", PASTE_URL, data=leaderboard) as resp:
try:
resp_json = await resp.json()
except Exception:
log.exception("Failed to upload full leaderboard to paste service")
return ""

if "key" in resp_json:
return RAW_PASTE_URL_TEMPLATE.format(key=resp_json["key"])

log.error(f"Unexpected response from paste service while uploading leaderboard {resp_json}")
return ""
file = paste_service.PasteFile(content=leaderboard)
try:
paste = await paste_service.send_to_paste_service(
files=[file],
http_session=aiohttp.ClientSession()
)
except paste_service.PasteTooLongError:
log.exception("Leaderboard too long to upload to paste site")
return ""
except paste_service.PasteUploadError:
log.exception("Failed to upload full leaderboard to paste service")
return ""

return paste.link


def _get_top_leaderboard(full_leaderboard: str) -> str:
Expand Down

0 comments on commit 327ae18

Please sign in to comment.