forked from 42Paris/hall-voice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-commands.py
executable file
·56 lines (44 loc) · 1.74 KB
/
check-commands.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
#!/usr/bin/env python3
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
import os, sys, json, time, argparse
def get_token():
client_id = os.environ['API42_ID']
client_secret = os.environ['API42_SECRET']
client = BackendApplicationClient(client_id=client_id)
api = OAuth2Session(client=client)
api.fetch_token(token_url='https://api.intra.42.fr/oauth/token', client_id=client_id, client_secret=client_secret)
return api
def check_login(api, login):
time.sleep(1)
response = api.get(f"https://api.intra.42.fr/v2/users/{login}").json()
if response:
response = api.get(f"https://api.intra.42.fr/v2/campus/1/products/15/commands?filter[owner_id]={response['id']}").json()
if response:
if args.verbose:
print(f"{login}: OK")
return True
else:
if args.verbose:
print(f"{login}: KO! User did not purchase Hall Voice Change on intra shop")
return False
else:
if args.verbose:
print(f"{login}: User does not exist")
return False
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', default=False, action='store_true', help='Verbose mode')
parser.add_argument('logins', nargs='*', default=[], help='Logins to check')
args = parser.parse_args()
if not args.logins:
sys.exit()
if args.verbose:
print('Logins:', args.logins, '\n')
token = get_token()
checks = [check_login(token, login) for login in args.logins]
if False in checks:
sys.exit(1)
except KeyboardInterrupt:
sys.exit()