-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKovaaKsTablePull.py
111 lines (95 loc) · 3.57 KB
/
KovaaKsTablePull.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import requests
import csv
# Input in scenario names
SCENARIO_NAMES = [
'1wall 6targets small',
'1w4ts Voltaic',
'1w4ts reload',
]
CSV_File_Name = 'Test_X'
# Array setup
Leaderboard_ID = [0] * len(SCENARIO_NAMES)
# Request scenario path one time to get amount of pages on the scenarios page
session = requests.Session()
r = session.get(
"https://kovaaks.com/webapp-backend/scenario/popular"
"?page=0&max=100"
).json()
Max_Page = r['total'] // 100
# Iterate through all playlist pages
for i in range(Max_Page + 1):
r = session.get(
f"https://kovaaks.com/webapp-backend/scenario/popular"
f"?page={i}&max=100"
).json()
# Iterate through all "data" rows on each playlist page
for Data in r['data']:
# If scenario name is found, fill the corresponding index in the
# leaderboard ID array with the "leaderboardId"
try:
index = SCENARIO_NAMES.index(Data['scenarioName'])
Leaderboard_ID[index] = Data['leaderboardId']
print(
f"Scenario ID Found for: {SCENARIO_NAMES[index]}, "
f"{Leaderboard_ID[index]}"
)
except ValueError:
pass
# Exit loop if all leaderboard IDs have been found
if all(value != 0 for value in Leaderboard_ID):
break
session.close()
# Create dictionary
Score_Dic = {}
# Iterate through each leaderboard
for i in range(0, len(SCENARIO_NAMES)):
# Request leaderboard path one time to get amount of pages
# on each leaderboard
session = requests.Session()
r = session.get(
f"https://kovaaks.com/webapp-backend/leaderboard/scores/global?"
f"leaderboardId={Leaderboard_ID[i]}&page=0&max=100"
).json()
Max_Page = r['total'] // 100
# Iterate through all leaderboard pages
for ii in range(Max_Page + 1):
r = session.get(
f"https://kovaaks.com/webapp-backend/leaderboard/scores/global?"
f"leaderboardId={Leaderboard_ID[i]}&page={ii}&max=100"
).json()
print(
f"Leaderboard {i + 1} of {len(SCENARIO_NAMES)}. "
f"Page: {ii} of {Max_Page} data pull."
)
# Iterate through all "data" rows on each playlist page
# and send data to leaderboard column of relevant arrays
for Data in r['data']:
try:
Steam_Name = Data['steamAccountName']
# If Steam name (key) exists, fill in relevant score list
# for Steam name
if Steam_Name in Score_Dic and Score_Dic[Steam_Name][
i] is None:
Score_Dic[Steam_Name][i] = Data['score']
# If Steam name (key) does not exist, create new key for
# Steam name and fill in relevant score list for Steam name
elif Steam_Name not in Score_Dic:
Score_Dic[Steam_Name] = [None] * len(SCENARIO_NAMES)
Score_Dic[Steam_Name][i] = Data['score']
except KeyError:
pass
session.close()
# CSV data writing
with open('Leaderboard_Pull_For_' + CSV_File_Name + '.csv', 'w',
encoding='utf-8', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
# Write in a header row
Header = ["Steam Name"] + [
SCENARIO_NAMES[i] for i in range(len(SCENARIO_NAMES))
]
csvwriter.writerow(Header)
for key, value in Score_Dic.items():
try: # Sometimes Excel writes errors here, so do this:
csvwriter.writerow([key] + value)
except: # I don't know the error, so just do this for now
pass