Last active
December 4, 2020 07:45
-
-
Save Eskimon/0cfd6172ec7200d512c84742dbfead2d to your computer and use it in GitHub Desktop.
AoC Discord webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import locale | |
from datetime import date | |
from urllib import request | |
from urllib.error import HTTPError | |
locale.setlocale(locale.LC_ALL, 'fr_FR') | |
WEBHOOK_URL = 'https://discordapp.com/api/webhooks/xxx/yyy' | |
## Les paramètres d'en-tête de la requête | |
headers = { | |
'Accept': 'application/json', | |
'Cookie': 'session=<your-session-cookie-here>', | |
} | |
req = request.Request(url='https://adventofcode.com/2020/leaderboard/private/view/<your-community-id>.json', | |
headers=headers, | |
method='GET') | |
result = {} | |
with request.urlopen(req) as response: | |
result = json.loads(response.read().decode('utf-8')) | |
# with open('test.json') as response: | |
# result = json.loads(response.read()) | |
## La payload | |
today = date.today() | |
# alphabetical ordering | |
names = [] | |
for key in result['members']: | |
names.append((result['members'][key]['name'], key)) | |
names = sorted(names, key=lambda item: item[0].lower()) | |
max_len = max([len(name[0]) for name in names]) | |
content = 'Progression le {}'.format(today.strftime('%d %B')) | |
content += '\n```plain' | |
for name in names: | |
pseudo = name[0].rjust(max_len, ' ') | |
stars = '' | |
for i in range(today.day): | |
try: | |
total = len(result['members'][name[1]]['completion_day_level'][str(i + 1)]) | |
if total == 2: | |
stars += '🌟' | |
elif total == 1: | |
stars += '⭐' | |
else: | |
stars += '⭕' | |
except KeyError: | |
if (i + 1) == today.day: | |
stars += '⏲️' | |
else: | |
stars += '⭕' | |
content += '\n{} {}'.format(pseudo, stars) | |
content += '\n```' | |
## Enfin on construit notre requête | |
headers = { | |
'Content-Type': 'application/json', | |
'user-agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11', | |
} | |
payload = { 'content': content, } | |
req = request.Request(url=WEBHOOK_URL, | |
data=json.dumps(payload).encode('utf-8'), | |
headers=headers, | |
method='POST') | |
## Puis on l'émet ! | |
try: | |
response = request.urlopen(req) | |
print(response.status) | |
print(response.reason) | |
print(response.headers) | |
except HTTPError as e: | |
print('ERROR') | |
print(e.reason) | |
print(e.hdrs) | |
print(e.file.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment