Skip to content

Commit

Permalink
set cookie within header
Browse files Browse the repository at this point in the history
  • Loading branch information
FirePlank committed Nov 18, 2023
1 parent a426f7f commit 5223fdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 11 additions & 8 deletions bot/extensions/adventofcode/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import Optional
from typing import Optional, Union
from zoneinfo import ZoneInfo

import discord
Expand All @@ -12,8 +12,10 @@
LEADERBOARD_ID = settings.aoc.leaderboard_id
LEADERBOARD_CODE = settings.aoc.leaderboard_code
API_URL = f"https://adventofcode.com/{YEAR}/leaderboard/private/view/{LEADERBOARD_ID}.json"
AOC_REQUEST_HEADER = {"User-Agent": "Tech With Tim Discord Bot https://github.com/SylteA/Discord-Bot"}
AOC_SESSION_COOKIE = {"session": settings.aoc.session_cookie}
AOC_REQUEST_HEADERS = {
"User-Agent": "Tech With Tim Discord Bot https://github.com/SylteA/Discord-Bot",
"Cookie": f"session={settings.aoc.session_cookie}",
}


def home_embed():
Expand Down Expand Up @@ -80,15 +82,16 @@ def set_name_to_anonymous(cls, val: Optional[str]) -> str:
return val


async def fetch_leaderboard(local: bool = False) -> str:
async def fetch_leaderboard(local: bool = False) -> Union[str, dict]:
url = f"https://adventofcode.com/{YEAR}/leaderboard"
if local:
url += f"/private/view/{LEADERBOARD_ID}.json"

http.session.cookie_jar.update_cookies(AOC_SESSION_COOKIE)
async with http.session.get(url, headers=AOC_REQUEST_HEADER, raise_for_status=True) as resp:
async with http.session.get(url, headers=AOC_REQUEST_HEADERS, raise_for_status=True) as resp:
if resp.status == 200:
response = await resp.text()
http.session.cookie_jar.clear()
if local:
response = await resp.json()
else:
response = await resp.text()

return response
2 changes: 0 additions & 2 deletions bot/extensions/adventofcode/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import re
from datetime import datetime
from zoneinfo import ZoneInfo
Expand Down Expand Up @@ -31,7 +30,6 @@ async def home(self, interaction: core.InteractionType, _button: ui.Button):
)
async def local_leaderboard(self, interaction: core.InteractionType, button: ui.Button):
leaderboard = await fetch_leaderboard(local=True)
leaderboard = json.loads(leaderboard)

members = [Member(**member_data) for member_data in leaderboard["members"].values()]

Expand Down

0 comments on commit 5223fdc

Please sign in to comment.