Initial commit

This commit is contained in:
2022-05-24 17:42:58 +02:00
commit 32c4e84bbb
2 changed files with 222 additions and 0 deletions

58
script.py Normal file
View File

@ -0,0 +1,58 @@
import os, re, datetime, gzip, shutil
LOG_FOLDER = "logs"
JOIN_PATTERN = re.compile(r"\[\d\d:\d\d:\d\d\] \[Server thread\/INFO\]: .+ joined the game")
LEAVE_PATTERN = re.compile(r"\[\d\d:\d\d:\d\d\] \[Server thread\/INFO\]: .+ left the game")
TIME_PATTERN = re.compile(r"\[\d\d:\d\d:\d\d\]")
TIME_THREAD_PATTERN = re.compile(r"\[\d\d:\d\d:\d\d\] \[Server thread\/INFO\]: ")
playtimes = dict()
jointimes = dict()
print("Checking log files ..")
for logfile_path in os.listdir(LOG_FOLDER):
# Unzip file if necessary
if logfile_path.endswith(".gz"):
actual_path = logfile_path.replace(".gz", "")
with gzip.open(os.path.join(LOG_FOLDER, logfile_path), "rb") as f_in:
with open(os.path.join(LOG_FOLDER, actual_path), "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
logfile_path = actual_path
# Iterate over log files
with open(os.path.join(LOG_FOLDER, logfile_path), "r", encoding="utf-8") as logfile:
# Iterate over log lines
for line in logfile.readlines():
time_str = re.search(TIME_PATTERN, line).group(0)
eventtime = datetime.datetime.strptime(time_str, "[%H:%M:%S]")
# User join message
if re.match(JOIN_PATTERN, line):
username = re.sub(TIME_THREAD_PATTERN, "", line).split()[0]
if username in jointimes:
print("Detected join without leave. Skipping ..")
jointimes[username] = eventtime
# User leave message
elif re.match(LEAVE_PATTERN, line):
username = re.sub(TIME_THREAD_PATTERN, "", line).split()[0]
try:
jointime = jointimes[username]
del jointimes[username]
if eventtime < jointime:
eventtime += datetime.timedelta(days=1)
playtime = eventtime - jointime
# Add time to playtimes
if username not in playtimes:
playtimes[username] = playtime
else:
playtimes[username] += playtime
except KeyError:
print("Deteced leave without join. Skipping ..")
print("Done!\n")
for key in playtimes.keys():
print(f"{key} : {playtimes[key]}")