Created
June 10, 2017 00:22
-
-
Save mdiller/bf209ac01fb3ff900cfb813ae710fb3c to your computer and use it in GitHub Desktop.
updates the botdata.json file to the new format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import OrderedDict | |
userinfo_defaults = OrderedDict([ | |
("steam32", None), | |
("intro", ""), | |
("outro", "") | |
]) | |
guildinfo_defaults = OrderedDict([ | |
("voicechannel", None), | |
("reactions", False), | |
("invalidcommands", False), | |
("banned_users", []), | |
("ttschannel", []) | |
]) | |
def write_json(filename, data): | |
text = json.dumps(data, indent="\t") | |
with open(filename, "w+") as f: | |
f.write(text) # Do it like this so it doesnt break mid-file | |
def read_json(filename): | |
with open(filename) as f: | |
return json.load(f, object_pairs_hook=OrderedDict) | |
filename = "botdata.json" | |
data = read_json(filename) | |
if "serverinfo" in data and "guildinfo" not in data: | |
data["guildinfo"] = data["serverinfo"] | |
del data["serverinfo"] | |
for user in data["userinfo"]: | |
if "steam64" in user: | |
if "steam32" not in user and user["steam64"]: | |
user["steam32"] = user["steam64"] - 76561197960265728 | |
del user["steam64"] | |
if "banned" in user: | |
del user["banned"] | |
# Delete default values | |
for key in userinfo_defaults: | |
if key in user: | |
if user[key] == userinfo_defaults[key]: | |
del user[key] | |
for guild in data["guildinfo"]: | |
# Delete default values | |
for key in guildinfo_defaults: | |
if key in guild: | |
if guild[key] == guildinfo_defaults[key]: | |
del guild[key] | |
# Remove un-needed entries (all default values) | |
data["userinfo"] = list(filter(lambda d: len(d) > 1, data["userinfo"])) | |
data["guildinfo"] = list(filter(lambda d: len(d) > 1, data["guildinfo"])) | |
write_json(filename, data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment