[TOOL/ADMIN] Whitelist Cleaner - Remove Inactive Players

Discussion in 'Bukkit Tools' started by TheBeast808, Mar 21, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    TheBeast808

    I made this the other day when somebody was asking for a way to remove old players from a whitelist because his list was getting too big.

    Requirements:
    - Python 2.7 (Preinstalled on most Linux distros)
    - Your server logs

    Instructions:
    Backup your whitelist file, just in case something goes wrong!
    1. Save this code as whitelist_cleaner.py
    Code:
    import time
    import os
     
    serverlog = raw_input("Path to log file: ")
    print ">>>>WARNING! BACK UP YOUR WHITELIST BEFORE USING THIS!<<<<"
    whitelist = raw_input("Path to whitelist file: ")
    temp = raw_input("Enter a path to a temp file to store data: ")
    print "All players who have not played since this date will be deleted"
    expiration_date = raw_input("Expiration date (Year-month-day ex:2012-03-18): ")
    exact_expiration = time.mktime(time.strptime(expiration_date,"%Y-%m-%d"))
    log_file = open(serverlog, "r")
    whitelist_file = open(whitelist, "r")
     
    start = time.time()
    good_players = []
    lastLoginsPerPlayer = {}
     
    def filter_logs(logs, temp):
        tempFile = open(temp, "a")
        for line in logs.readlines():
            if 'lost connection' in line:
                if '/' not in line:
                    tempFile.write(line)
     
    def getTrueTime(logTime):
        return time.mktime(time.strptime(logTime, "%Y-%m-%d"))
     
    def getLastLogins(temp, LLPP):
        tempFile = open(temp, "r")
        for line in tempFile.readlines():
            line = line.split(" ")
            player = line[3]
            currentTime = getTrueTime(line[0])
            try:
                oldTime = LLPP[player]
                if old < currentTime:
                    LLPP[player] = currentTime
            except:
                LLPP[player] = currentTime
             
    def writeNewWhiteList(good, whitelist_path):
        os.remove(whitelist_path)
        newWhiteList = open(whitelist_path, "a")
        for player in good:
            newWhiteList.write(player + "\n")
     
    filter_logs(log_file, temp)
    getLastLogins(temp, lastLoginsPerPlayer)
     
     
    for player in whitelist_file.readlines():
        player = player.strip("\n")
        try:
            last_logout = lastLoginsPerPlayer[player]
            if last_logout > exact_expiration:
                good_players.append(player)
        except:
            print "Looks like " + player + " never logged in... Are you sure your log file is complete?"
            good_players.append(player)
     
    writeNewWhiteList(good_players, whitelist)
    os.remove(temp)
    print "Time elapsed: " + str(time.time() - start) + " seconds."
    Or download it from here:
    <Edit by Moderator: Redacted mediafire url>

    2. Run it by double clicking or typing "python whitelist_cleaner.py" in CMD/Terminal
    3. It will prompt you for some paths to files. Just enter where your whitelist/server log is. For the temp file, it can be stored anywhere with any extension(ex "/Users/Beast/Desktop/temp.txt"). The temp file will be delete after the program is done running
    4. You will be asked to enter a date. Any players who have not logged in since that date will be deleted from the whitelist.
    5. Let it run. How long it needs to run will depend on the size of the whitelist and log file. My MacBook burned through a 4300 person whitelist and a 50.8mb server.log in 1.66 seconds, so it shouldn't take long.


    Questions:
    Q: What does "Looks like xxxxx never logged in... Are you sure your log file is complete?" mean?
    A: This means that according to the server logs, that player never logged into the server. Just to be safe, this script will NOT delete him from the whitelist, just incase it was a bug. This can be caused by two things. The first cause is that, maybe, the player didn't log in. The second possible cause is that the log file you supplied is not the complete history of the server.

    Q: I found a bug/glitch/it isn't working
    A: I've tested it and it works for me. If it doesn't work for you, post here with all the information about the error that you can.
     
    Last edited by a moderator: Nov 11, 2016
Thread Status:
Not open for further replies.

Share This Page