-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeluser.py
executable file
·180 lines (132 loc) · 4.38 KB
/
deluser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python3 -i
import json, os, sys
from glob import glob
from nbt import nbt
from datetime import date
import sqlite3
spigdir = '/home/mc/spigot'
datadir = spigdir + '/worlds/world/playerdata/'
essdir = spigdir + '/plugins/Essentials/userdata/'
cachefile = spigdir + '/usercache.json'
plandb = spigdir + '/plugins/Plan/database.db'
with open(cachefile, 'r') as f:
cached_users = json.loads(f.read())
### loading data
def users_from_usercache():
users = {}
for user in cached_users:
uuid = user['uuid']
users[uuid] = {'name': user['name']}
return users
def users_from_playerdata():
users = {}
for player in glob(datadir + '*.dat'):
uuid = player.replace(datadir, '').replace('.dat', '')
users[uuid] = {}
playernbt = nbt.NBTFile(player, 'rb')
try:
users[uuid]['name'] = playernbt['bukkit']['lastKnownName'].value
except KeyError:
pass
try:
first_date = date.fromtimestamp(
playernbt['bukkit']['firstPlayed'].value // 1000
)
last_date = date.fromtimestamp(
playernbt['bukkit']['lastPlayed'].value // 1000
)
users[uuid]['first_seen'] = first_date
users[uuid]['last_seen'] = last_date
users[uuid]['days'] = (last_date - first_date).days
except KeyError:
pass
return users
def delta(data, days):
if 'days' in data:
return data['days'] <= days
else:
return True
def find_users(days=None):
users = users_from_playerdata()
for uuid, data in users_from_usercache().items():
user = users.setdefault(uuid, {})
user.update(data)
if days:
users = {k: v for k, v in users.items() if delta(v, days)}
return users
def print_users(days=None):
users = find_users(days=days)
for uuid, data in users.items():
print(
uuid,
data.get('name').ljust(20),
data.get('first_seen'),
data.get('last_seen'),
' ',
data.get('days'),
)
### removing
def remove_usercache(uuids):
with open(spigdir + '/usercache-backup.json', 'w') as f:
f.write(json.dumps(cached_users))
cached_users[:] = [x for x in cached_users if not x['uuid'] in uuids]
with open(spigdir + '/usercache.json', 'w') as f:
f.write(json.dumps(cached_users))
print('removed users from usercache')
def remove_playerdata_essentials(uuids):
for uuid in uuids:
remove_file(uuid + '.dat', datadir)
remove_file(uuid + '.yml', essdir)
def remove_file(filename, dir):
if not os.path.exists(dir + 'removed'):
os.mkdir(dir + 'removed')
if os.path.exists(dir + filename):
os.rename(dir + filename, dir + '/removed/' + filename)
print('removed ' + filename + ' from ' + dir)
# https://github.com/plan-player-analytics/Plan/blob/master/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/commands/RemovePlayerTransaction.java
def remove_plan(uuids):
con = sqlite3.connect(plandb)
tables = [
'plan_geolocations',
'plan_nicknames',
'plan_world_times',
'plan_sessions',
'plan_ping',
'plan_user_info',
'plan_users',
'plan_extension_user_table_values',
'plan_extension_user_values',
'plan_extension_groups',
]
rows = [f''{uuid}'' for uuid in uuids]
for table in tables:
cur = con.execute(f'DELETE FROM {table} WHERE uuid IN ({", ".join(rows)})')
print(f'rows deleted from {table}: {cur.rowcount}')
count = 0
cur = con.execute(
f'DELETE FROM plan_kills WHERE killer_uuid IN ({", ".join(rows)})'
)
count += cur.rowcount
cur = con.execute(
f'DELETE FROM plan_kills WHERE victim_uuid IN ({", ".join(rows)})'
)
count += cur.rowcount
print(f'rows deleted from plan_kills: {count}')
con.commit()
con.close()
users = find_users()
def remove_users(names=[], uuids=[]):
print('\nto remove:')
for uuid in users:
name = users[uuid]['name']
if name in names:
uuids.append(uuid)
print(uuid, name)
print()
remove_plan(uuids)
print()
remove_playerdata_essentials(uuids)
print()
remove_usercache(uuids)
if __name__ == '__main__':
remove_users(names=sys.argv[1:])