-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard.py
executable file
·66 lines (51 loc) · 1.88 KB
/
scoreboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import requests
import os
from os import getenv
from tabulate import tabulate
from dotenv import load_dotenv
load_dotenv()
class ascii_scoreboard:
def __init__(self):
self.url = "https://ctf.uia.no"
self.token = getenv("ctfd_token")
self.s = requests.session()
self.s.headers.update({"Content-Type": "application/json"})
self.s.headers.update({"Authorization": f"Token {self.token}"})
self.api = lambda route: self.s.get(f"{self.url}/api/v1/{route}").json()["data"]
def getTeams(self):
return self.api("teams")
def getOnsiteList(self):
return [
(data["pos"], data["name"], data["score"])
for data in self.getScoreboard()
if data["account_id"] in self.getOnsite()
]
def getOffsiteList(self):
return [
(data["pos"], data["name"], data["score"])
for data in self.getScoreboard()
if data["account_id"] not in self.getOnsite()
]
def formatScoreboard(self):
HEADER = ["SITE POS", "POS", "NAME", "SCORE"]
print("\t=== OFFSITE ===")
offsite = self.re_indexTeam(self.getOffsiteList())
print(tabulate(offsite, headers=HEADER), "\n")
print("\t=== ONSITE ===")
onsite = self.re_indexTeam(self.getOnsiteList())
print(tabulate(onsite, headers=HEADER))
def re_indexTeam(self, site_list: list):
return [[key + 1, val[0], val[1], val[2]] for key, val in enumerate(site_list)]
def getScoreboard(self):
return self.api("scoreboard")
def getOnsite(self):
onsite = [
(team["fields"][0]["value"], team["id"])
for team in self.api("teams")
if team["fields"][0]["value"] == True
]
return [teams[-1] for teams in onsite]
score = ascii_scoreboard()
(score.formatScoreboard())
quit(137)