-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscore_feed_parser.py
223 lines (161 loc) · 6.29 KB
/
score_feed_parser.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Take data from discord channel exporter for "scores-feed"
in Discord official PewPewLive server, and parse it
"""
import csv
import sys
from loguru import logger
logger.remove(0)
logger.add(sys.stderr, level="INFO")
# uncomment the next line for logging into file
# (performance drops significantly, tests only)
# logger.add("score_feed_parser.log", level="TRACE")
logger = logger.opt(colors=True)
platforms = {
"android": ["Android", ":android:", "<:android:"],
"ios": [":ios:", ""],
"windows": ["🪟"],
"web": ["🌐", "web"],
"unknown": ["❓"]
}
def get_data(file: str) -> list[list[str]]:
"""Get data from given file (only time,
content and reactions)"""
logger.debug("Getting data from <y>{}</>", file)
results = []
with open(file, "r", encoding="UTF-8") as file:
reader = csv.reader(file)
for row in reader:
time, content, attachments, reactions = row[2:]
results.append([time, content, reactions])
return results[1:] # remove headers
def parse_time(time: str) -> list[str]:
"""Parse time from given string"""
logger.debug("Parsing time: <w>{}</>", time)
return time.split("T")
def get_usernames(content: str) -> list:
"""Get usernames (1/2 player) and era from given content"""
logger.debug("Getting usernames: <w>{}</>", content)
if "(era 1)" in content:
era = 1
split_content = content.split()[2:] # remove "(era 1)"
else:
era = 2
split_content = content.split()
logger.debug("Got era: <y>{}</>, content: <w>{}</>",
era, split_content)
usernames = []
for raw_username in [split_content[0], split_content[2]]:
if raw_username[0] == "`" and raw_username[-1] == "`":
username = raw_username[1:-1]
else:
username = raw_username
usernames.append(username)
mode = 2
logger.debug("Got usernames (assuming 2p): <y>{}</>", usernames)
# case with 1p mode
if split_content[1] != "and":
usernames[1] = None
mode = 1
# edge case for older version of Score bot
if "(2 players mode)" in content:
mode = 2
logger.debug("Got usernames: <y>{}</>", usernames)
return [era, mode, *usernames]
def get_country_and_platform(content: str) -> list[str]:
"""Get country and platform from given content"""
logger.debug("Getting country and platform: <w>{}</>", content)
if content[-1] != ")":
return None, None
data = content[:-1].split("(")[-1] # country and platform
data = "".join([x for x in data if x != " "]) # remove spaces
data = data.split("-") # separate country/platform
if len(data) == 1:
country = data[0]
return country, None
country, platform = data
# print(platform)
logger.debug("Got country: <y>{}</>, platform: <y>{}</>",
country, platform)
for name, alias in platforms.items():
if platform in alias:
platform_name = name
break
logger.debug("Replaced <y>{}</> with <y>{}</>",
platform, platform_name)
return country, platform_name
def get_level(content: str) -> str:
"""Get level name from given content"""
logger.debug("Getting level name: <w>{}</>", content)
split_content = content.split()
in_word_index = split_content.index("in")
level_name_index = in_word_index + 1
level_name = []
for word in split_content[level_name_index:]:
if word[0] == "(":
break
level_name.append(word)
level_name = " ".join(level_name)
if level_name[0] == "`" and level_name[-1] == "`":
level_name = level_name[1:-1]
return "".join(level_name)
def get_score(content: str) -> str:
"""Get score from given content (final, 'increased to')"""
logger.debug("Getting player's final score: <w>{}</>", content)
split_content = content.split()
in_word_index = split_content.index("in")
level_score_index = in_word_index - 1
level_score = split_content[level_score_index]
return level_score
def parse_content(content: str) -> list[str]:
"""Parse content from given string"""
logger.debug("Parsing content: <w>{}</>", content)
# only work with last line, where the score itself is
logger.trace("Discrading everything except the last line")
content = content.split("\n")[-1]
era, mode, player1, player2 = get_usernames(content)
if mode == 1:
country, platform = get_country_and_platform(content)
else:
country, platform = None, None
level = get_level(content)
score = get_score(content)
return [era, mode, player1, player2, level, score, country, platform]
def parse_row(row: list[str]) -> list[str|int]:
"""Parse data from given row"""
logger.debug("Parsing row: <w>{}</>", row)
time, content, reactions = row
parsed_time = parse_time(time)
parsed_content = parse_content(content)
result = [*parsed_time, *parsed_content, reactions]
logger.debug("Parsed row, got <y>{}</>", result)
return result
def write_parsed(file: str, data: list[list[any]]) -> None:
"""Write parsed data to given file"""
logger.debug("Writing parsed data to <y>{}</>", file)
with open(file, "w", encoding="UTF-8") as file:
writer = csv.writer(file)
writer.writerows(data)
def main(data_file: str) -> list[list[str|int]]:
"""Parse data from given file"""
data = get_data(data_file)
data_len = len(data)
info_step = data_len // 100
if info_step == 0:
info_step = 1
parsed = []
for row in data:
try:
parsed.append(parse_row(row))
except Exception as e:
logger.warning(e)
# uncomment the following code for info on progress
# (slows down the process significantly, tests only)
#if data.index(row) % info_step == 0:
# percent = round((data.index(row) + 1) / data_len * 100)
# logger.info("Parsed <y>{}</>/{} (<y>{}%</>) rows",
# data.index(row) + 1, data_len, percent)
write_parsed("output.csv", parsed)
if __name__ == "__main__":
logger.debug('__name__=="__main__": running main()...')
args = sys.argv[1:]
main(args[0])