Skip to content

Instantly share code, notes, and snippets.

@GrapeApple0
Created October 8, 2024 13:30
Show Gist options
  • Select an option

  • Save GrapeApple0/2580f3d0bc8bab01462cff5fc5eac98a to your computer and use it in GitHub Desktop.

Select an option

Save GrapeApple0/2580f3d0bc8bab01462cff5fc5eac98a to your computer and use it in GitHub Desktop.
Ban spam by username
import requests
import datetime
import time
SERVER = "example.com"
TOKEN = "EXAMPLETOKEN"
old_ban_host = []
BANNED_SERVER = []
IGNORE_LIST = ["example.com"]
CANDIDATE_LIST = dict()
already_check=False
while True:
ban_user_list = []
ban_username_dict = {}
offset = 0
now = datetime.datetime.now()
while True:
param = {
'limit': 10,
'offset': offset,
'sort': '+createdAt',
'origin': 'remote',
'state': 'available',
'i': TOKEN
}
try:
res = requests.post(f"https://{SERVER}/api/admin/show-users",
headers={'Content-Type': 'application/json'},
json=param, timeout=30).json()
except:
break
l = len(res)
for user in res:
if "username" in user and len(user["username"]) == 10:
if str(user["avatarUrl"]).startswith(f"https://{SERVER}/identicon"):
dt = datetime.datetime.strptime(user["createdAt"], '%Y-%m-%dT%H:%M:%S.%fZ') + datetime.timedelta(hours=9)
span = datetime.timedelta(hours=12)
if already_check:
span = datetime.timedelta(minutes=1)
if datetime.datetime.now() - dt < span:
if 'description' not in user or user['description'] is None or user['description'] == "":
if user['name'] is None or user['name'] == "" or user['name'] == user['username']:
if user['host'] not in IGNORE_LIST:
if user['host'] not in CANDIDATE_LIST:
CANDIDATE_LIST[user['host']] = 1
else:
CANDIDATE_LIST[user['host']] += 1
if CANDIDATE_LIST[user['host']] > 3:
BANNED_SERVER.append(user['host'])
print(f"{user['username']}@{user['host']} {dt}")
ban_user_list.append(user["id"])
ban_username_dict[user["id"]] = user["username"]
else:
l=0
break
offset += 10
if l < 10:
break
print(f"Found {len(ban_user_list)} users.")
for user in ban_user_list:
requests.post(f'https://{SERVER}/api/admin/suspend-user',
headers={'Content-Type': 'application/json'},
json={'userId': user, 'i': TOKEN}, timeout=10)
print(f"Suspended {ban_username_dict[user]}")
notes = requests.post(f'https://{SERVER}/api/users/notes',
headers={'Content-Type': 'application/json'},
json={'userId': user, 'i': TOKEN}, timeout=10).json()
for note in notes:
requests.post(f'https://{SERVER}/api/notes/delete',
headers={'Content-Type': 'application/json'},
json={'noteId': note['id'], 'i': TOKEN}, timeout=10)
print(f"Deleted {ban_username_dict[user]}'s note:{note['id']}")
print(f"wait 15 seconds. Last checked: {datetime.datetime.now()}@{SERVER}")
time.sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment