Skip to content

Commit 2d4d314

Browse files
committed
better defaults and added webhook content limit
1 parent 8662f2f commit 2d4d314

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ RUN pip install --upgrade pip && pip install requests
44

55
COPY ./advent_of_code_notify.py ./advent_of_code_notify.py
66

7-
ENTRYPOINT ["python"]
87
CMD ["/advent_of_code_notify.py"]

advent_of_code_notify.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
#!/usr/bin/python3
1+
#!/usr/bin/env python3
22

33
# advent_of_code_notify.py
44
# Send a webhook notification when someone from an Advent Of Code
55
# leaderboard solves a puzzle
66
#
77
# tofran, dec 2020
8+
# https://github.com/tofran/advent-of-code-leaderboard-notifier
89

910
import os
1011
import requests
1112
import json
13+
from datetime import date
1214

1315
LEADERBOARD_ENDPOINT_TEMPLATE = (
1416
"https://adventofcode.com/"
15-
"{year}/leaderboard/private/view/{leaderboard_id}.json"
17+
"{year}/leaderboard/private/view/{leaderboard_id}{extension}"
1618
)
1719

1820
CACHE_FILE = os.getenv("CACHE_FILE", "./cache.json")
1921

20-
ADVENT_OF_CODE_YEAR = int(os.getenv("ADVENT_OF_CODE_YEAR", "2020"))
22+
23+
def get_default_year():
24+
today = date.today()
25+
if today.month == 12:
26+
return today.year
27+
return today.year - 1
28+
29+
30+
ADVENT_OF_CODE_YEAR = int(os.getenv("ADVENT_OF_CODE_YEAR", get_default_year()))
2131
ADVENT_OF_CODE_LEADERBOARD_ID = os.getenv("ADVENT_OF_CODE_LEADERBOARD_ID")
2232
ADVENT_OF_CODE_SESSION_ID = os.getenv("ADVENT_OF_CODE_SESSION_ID")
2333

@@ -27,10 +37,15 @@
2737
assert ADVENT_OF_CODE_SESSION_ID, "ADVENT_OF_CODE_SESSION_ID missing"
2838
assert WEBHOOK_URL, "WEBHOOK_URL missing"
2939

30-
LEADERBOARD_ENDPOINT = LEADERBOARD_ENDPOINT_TEMPLATE.format(
31-
year=ADVENT_OF_CODE_YEAR,
32-
leaderboard_id=ADVENT_OF_CODE_LEADERBOARD_ID,
33-
)
40+
WEBHOOK_MAX_CONTENT_LENGTH = int(os.getenv("WEBHOOK_MAX_CONTENT_LENGTH", "2000"))
41+
42+
43+
def get_leaderboard_enpoint(as_json_api=True):
44+
return LEADERBOARD_ENDPOINT_TEMPLATE.format(
45+
year=ADVENT_OF_CODE_YEAR,
46+
leaderboard_id=ADVENT_OF_CODE_LEADERBOARD_ID,
47+
extension=".json" if as_json_api else "",
48+
)
3449

3550

3651
def get_cached_leaderboard():
@@ -52,7 +67,7 @@ def save_cached_leaderboard(data):
5267

5368
def fetch_leaderboard():
5469
response = requests.get(
55-
LEADERBOARD_ENDPOINT,
70+
get_leaderboard_enpoint(as_json_api=True),
5671
cookies={
5772
"session": ADVENT_OF_CODE_SESSION_ID
5873
}
@@ -77,11 +92,16 @@ def get_name(leaderboard, member_id):
7792

7893

7994
def send_webhook_notification(content):
95+
if len(content) > WEBHOOK_MAX_CONTENT_LENGTH:
96+
content = "The diff is too big, check the leaderbord: {}".format(
97+
get_leaderboard_enpoint(as_json_api=False)
98+
)
99+
80100
requests.post(
81101
WEBHOOK_URL,
82-
data=json.dumps({
102+
json={
83103
"content": content,
84-
}),
104+
},
85105
headers={"Content-Type": "application/json"}
86106
).raise_for_status()
87107

0 commit comments

Comments
 (0)