Created
June 20, 2023 12:27
-
-
Save mrhalix/1fcaec01c0198cf377c88c68be7212d7 to your computer and use it in GitHub Desktop.
IceWarp - Sort disabled users by their usage
This file contains 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
#----------------- | |
# I know this is a mess, but it works | |
#----------------- | |
import xml.etree.ElementTree as ET | |
def convert_quota(kb): | |
if kb > 1024 * 1024: | |
return str(round(kb / (1024 * 1024), 2)) + " GB" | |
elif kb > 1024: | |
return str(round(kb / 1024, 2)) + " MB" | |
# Emails list, open chrome dev tools and copy the response of the request | |
# API command: `GetAccountsInfoList`` | |
f = open("response.xml", "r") | |
res = f.read() | |
f.close() | |
# IceWarp response namespaces | |
namespace = {"ns": "admin:iq:rpc"} | |
root = ET.fromstring(res) | |
items = root.findall(".//ns:item", namespace) | |
users = {} | |
users_names = {} | |
for item in items: | |
displayemail = item.find("ns:displayemail", namespace).text | |
name = item.find("ns:name", namespace).text | |
accountstate_state = item.find("ns:accountstate/ns:state", namespace).text | |
quota = item.find("ns:quota/ns:mailboxsize", namespace).text | |
# calculate quota if user is not enabled | |
if accountstate_state == "1": | |
users[displayemail] = int(quota) | |
users_names[displayemail] = name | |
users = {k: v for k, v in sorted(users.items(), key=lambda item: item[1], reverse=True)} | |
csv = "email,name,quota\n" | |
for i in users: | |
csv += i+","+users_names[i]+","+convert_quota(users[i])+"\n" | |
# write results to file | |
print("writing to file") | |
output = open("users-usage.csv", "w") | |
output.write(csv) | |
output.close() | |
print(csv) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment