mc_whitelist/whitelist_gen.py

77 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import json
import requests
import argparse
from ldap3 import Server, Connection, ALL
from ldap_cred import ldap_cred
class Person(dict):
def __init__(self, username, uuid):
dict.__init__(self, name=username, uuid=uuid)
def lookupLDAPUsers():
server = Server(ldap_cred['ldap_url'], get_info=ALL)
conn = Connection(server, ldap_cred['ldap_user'], ldap_cred['ldap_pass'],
auto_bind=True)
check = conn.search(ldap_cred['ldap_search_base'],
ldap_cred['ldap_filter'], attributes=['uid'])
if check is False:
print("Error performing LDAP search")
return []
res = []
for i in conn.entries:
res.append(str(i['uid']))
return res
def lookupMCPlayer(username):
url = "https://playerdb.co/api/player/minecraft/" + username
response = requests.get(url)
if (response.status_code != 200):
print("Error looking up {}, reponse code {}"
.format(username, response.status_code))
return None
uid = response.json()["data"]['player']["id"]
return Person(username, uid)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--filename",
help="Whitelist file location," +
"defaults to ./whitelist.json",
type=str)
args = parser.parse_args()
filename = args.filename
if filename is None:
filename = "whitelist.json"
print("Loading current whitelist")
with open(filename) as json_file:
players = json.load(json_file)
player_usernames = []
for p in players:
player_usernames.append(p['name'])
print("Loading players from LDAP")
ldap_players = lookupLDAPUsers()
for p in ldap_players:
if p not in player_usernames:
m = lookupMCPlayer(p)
if m is not None:
print("Added new player {}".format(p))
players.append(m)
else:
print("Could not add new player {}".format(p))
print("Writing player whitelist to file")
with open(filename, 'w') as outfile:
json.dump(players, outfile, indent=4)
main()