Skip to content

Commit 9f9012a

Browse files
authored
Add batch script to update hacker statuses (#308)
1 parent 2f5c1fc commit 9f9012a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

scripts/batch_update.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import requests
2+
import json
3+
import getpass
4+
5+
print("Enter target API")
6+
DEFAULT_API = 'https://api.mchacks.ca'
7+
8+
API_URL = input('Target API (Default '+DEFAULT_API+'):')
9+
10+
if API_URL == '':
11+
API_URL = DEFAULT_API
12+
13+
# Get credentials
14+
print("Enter credentials for", API_URL)
15+
username = input("Email: ")
16+
17+
s = requests.Session()
18+
19+
logged_in = False
20+
21+
while not logged_in:
22+
password = getpass.getpass("Password: ")
23+
CREDENTIALS = {
24+
'email': username,
25+
'password': password
26+
}
27+
r = s.post('{0}/api/auth/login'.format(API_URL), CREDENTIALS)
28+
29+
if r.status_code != 200:
30+
print("Incorrect password, please try again.")
31+
else:
32+
print('Logged in as ', username)
33+
logged_in = True
34+
35+
# Get information about batch actions
36+
37+
ACCEPT_ID_FILE = input("Path to file with hackerIDs:")
38+
39+
VALID_ACTIONS = ['Applied', 'Accepted', 'Waitlisted',
40+
'Confirmed', 'Cancelled', 'Checked-in']
41+
42+
INITIAL_STATUS = input(
43+
"Inital status required " + str(VALID_ACTIONS) + ":")
44+
45+
NEW_STATUS = input(
46+
"Status to update to " + str(VALID_ACTIONS) + ":")
47+
48+
49+
hackerIds = []
50+
with open(ACCEPT_ID_FILE, 'r') as f:
51+
rows = f.readlines()
52+
for r in rows:
53+
hackerIds.append(r.strip())
54+
55+
# remove duplicates
56+
hackerIds = list(set(hackerIds))
57+
58+
for index, ID in enumerate(hackerIds):
59+
# so that we aren't 0-based index
60+
index = index + 1
61+
r1 = s.get('{0}/api/hacker/{1}'.format(API_URL, ID))
62+
if r1.status_code != 200:
63+
print('({0}/{1}) ERROR cannot get {2}'.format(index, len(hackerIds), ID))
64+
else:
65+
hackerInfo = json.loads(r1.content)
66+
if hackerInfo['data']['status'] == INITIAL_STATUS:
67+
r2 = s.patch('{0}/api/hacker/status/{1}'.format(API_URL, ID),
68+
{"status": NEW_STATUS})
69+
if r2.status_code != 200:
70+
print(
71+
'({0}/{1}) ERROR cannot update status for {2}'.format(index, len(hackerIds), ID))
72+
else:
73+
print(
74+
'({0}/{1}) {2} {3}'.format(index, len(hackerIds), NEW_STATUS, ID))

0 commit comments

Comments
 (0)