-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournamenttool_swiss.py
74 lines (66 loc) · 2.15 KB
/
tournamenttool_swiss.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
67
68
69
70
71
72
73
74
import requests
import json
import datetime
import ndjson
import csv
from configparser import ConfigParser
#read parameter file
parser = ConfigParser()
parser.read("parameter_swiss.ini")
#parse the parameter file
configObject = parser["PARAMS"]
id_arena = configObject["arenaID"]
file_name = configObject["output_file_name"]
rank_bool = configObject["rank"]
username_bool = configObject["username"]
score_bool = configObject["score"]
tieBreak_bool = configObject["tieBreak"]
rating_bool = configObject["rating"]
performance_bool = configObject["performance"]
#download arena data of lichess
url = "https://lichess.org/api/swiss/" + id_arena + "/results"
param = dict()
resp = requests.get(url=url, params=param)
list_resp = resp.text.splitlines()
data = list(map(lambda x: json.loads(x), list_resp))
#create CSV file
columns = []
if rank_bool == "true":
columns.append("rank")
if username_bool == "true":
columns.append("username")
if score_bool == "true":
columns.append("score")
if tieBreak_bool == "true":
columns.append("tieBreak")
if rating_bool == "true":
columns.append("rating")
if performance_bool == "true":
columns.append("performance")
print(columns)
now = datetime.datetime.today()
time = now.strftime('%Y%m%d_%H%M')
with open(file_name + time + '.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(columns)
for i in data:
column = []
if rank_bool == "true":
rank = i.get("rank") + 1
column.append(rank)
if username_bool == "true":
username = i.get("username")
column.append(username)
if score_bool == "true":
score = i.get("score")//1000000/10
column.append(score)
if tieBreak_bool == "true":
tieBreak = i.get("tieBreak")
column.append(tieBreak)
if rating_bool == "true":
rating = i.get("rating")
column.append(rating)
if performance_bool == "true":
performance = i.get("performance")
column.append(performance)
writer.writerow(column)