-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (67 loc) · 2.91 KB
/
main.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
import os
import sys
import json
import requests
from dotenv import load_dotenv
load_dotenv()
roblox_token = os.environ.get('ROBLOSECURITY')
user_id = os.environ.get('USERID')
exempt_games = os.environ.get('GAMES_EXEMPT').split(',')
exempt_keywords = os.environ.get('KEYWORDS_EXEMPT').split(',')
if not roblox_token or not user_id:
print('All environment variables must be entered.')
sys.exit(1)
def main() -> None:
s = requests.Session()
# Set roblosecurity token
s.cookies['.ROBLOSECURITY'] = roblox_token
# Add CSRF token to actually make the request work. See https://stackoverflow.com/a/69855008 .
header = s.post('https://catalog.roblox.com/')
s.headers['X-CSRF-TOKEN'] = header.headers['X-CSRF-TOKEN']
cursor = ''
page = 0
badge_ids = []
while True:
r = s.get(f'https://badges.roblox.com/v1/users/{user_id}/badges?sortOrder=Desc&limit=100&cursor={cursor}')
content = json.loads(r.text)
data = content['data']
cursor = content['nextPageCursor'] or None
for badge in data:
found_match = False
badge_data = {
'id': badge['id'],
'name': badge['name'],
'description': badge['description'],
'gameID': badge['awarder']['id']
}
# Checks if the game the badge was awarded from is exempt from badge removal
if str(badge_data['gameID']) in exempt_games:
print(f'Badge ID #{badge_data['id']} was exempt from badge removal.')
continue
# Checks if the name or description of the badge is exempt from badge removal based on keywords
for word in exempt_keywords:
if word in badge_data['name'] or word in badge_data['description']:
found_match = True
if found_match:
print(f'Badge ID #{badge_data['id']} was exempt from badge removal.')
continue
badge_ids.append(badge_data)
page += 1
print(f'Scraped badges page #{page}. Approximate badges found: {(page * 100):,}.')
if not cursor:
break
total_badges = len(badge_ids)
deleted_badges_count = 0
print(f'Total badges found: {total_badges}')
print('Deleting badges...')
for badge in badge_ids:
badge_id = badge['id']
r = s.delete(f'https://badges.roblox.com/v1/user/badges/{badge_id}')
if r.status_code == 200:
deleted_badges_count += 1
print(f'Deleted Badge ID #{badge_id}. Total badges deleted: {deleted_badges_count}. {(total_badges - deleted_badges_count):,} badges remain.')
else:
print(f'Something went wrong deleting Badge ID #{badge_id}. Open a github issue and include json content of the request above.')
print(f'Deleted badges. {total_badges - deleted_badges_count} badges remain.')
if __name__ == '__main__':
main()