From 710b7a4cdfe2d607a5ed28df0b709b6c03ba35ec Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 17 May 2023 00:42:20 +0200 Subject: [PATCH 1/2] Check if stud --- api.py | 34 ++++++++++++++++++++++++++++++++++ helloasso.py | 23 +++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 api.py diff --git a/api.py b/api.py new file mode 100644 index 0000000..de32adc --- /dev/null +++ b/api.py @@ -0,0 +1,34 @@ +import requests as rq +import json +from datetime import datetime + +def get_token(client_id: str, client_secret: str): + payload = {'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret} + r = rq.post("https://api.intra.42.fr/oauth/token", data=payload) + r_json = json.loads(r.text) + try: + return r_json['access_token'] + except KeyError: + return "An error occured while getting the token !" + +def get_token_from_env(): + with open('.env') as env_file: + data = json.load(env_file) + token = get_token(data['CLIENT_ID'], data['CLIENT_SECRET']) + return token + +def get(token: str, path: str): + headers = {'Authorization': 'Bearer ' + token} + r = rq.get("https://api.intra.42.fr/" + path, headers=headers) + return json.loads(r.text) + +############################################## + +def get_events_ids(token: str, name: str): + return [event['id'] for event in get(token, "/v2/events?campus_id=1&filter[name]=" + name)] + +def get_blackhole(token: str, uid: str): + print(uid) + blackhole_str = get(token, "/v2/cursus_users?filter[user_id]=" + str(uid) + "&filter[cursus_id]=21")[0]['blackholed_at'] + blackhole = datetime.strptime(blackhole_str, "%Y-%m-%dT%H:%M:%S.000Z") + return blackhole diff --git a/helloasso.py b/helloasso.py index b8553e3..125e2f8 100755 --- a/helloasso.py +++ b/helloasso.py @@ -1,9 +1,12 @@ #! /usr/bin/env python3 import sys +import json +import api from helloasso_api.oauth2 import OAuth2Api from helloasso_api.apiv5client import ApiV5Client + clientId = "" clientSecret = "" @@ -41,11 +44,24 @@ def fetchItems(self): while total > 0: total = res["pagination"]["totalCount"] res = self._client.call( - f"organizations/bde-42/forms/event/wed/items?continuationToken={res['pagination']['continuationToken']}" + f"organizations/bde-42/forms/event/wed/items?withDetails=true&continuationToken={res['pagination']['continuationToken']}" ).json() items["data"].extend(res["data"]) return items +TOKEN = api.get_token_from_env() + +def checkapi(login: str): + matching_users = api.get(TOKEN, '/v2/users?filter[login]=' + login) + return len(matching_users) != 0 + +def isstud(first_name: str, last_name: str, login: str): + if checkapi(login.lower()): + return "Stud verif ok" + else: + return "ALERTE non stud " + login + + auth = OAuth2Api( "api.helloasso.com", client_id=clientId, client_secret=clientSecret, timeout=99999 @@ -73,7 +89,10 @@ def fetchItems(self): print(len(res)) elif sys.argv[1] == "list": for item in res: - print(item["user"]["firstName"], item["user"]["lastName"]) + if 'customFields' in item: + print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " " + isstud(item["user"]["firstName"], item["user"]["lastName"], item["customFields"][0]["answer"])) + else: + print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " pas de CustomFields") else: print("Usage: helloasso.py ") sys.exit(1) From 8b3f23ca92591bd52d4258d6ee39d30565d7db01 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 18 May 2023 00:05:44 +0200 Subject: [PATCH 2/2] fix no customfields + client id and secret get in .env --- helloasso.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/helloasso.py b/helloasso.py index 125e2f8..4f18350 100755 --- a/helloasso.py +++ b/helloasso.py @@ -1,14 +1,14 @@ #! /usr/bin/env python3 import sys -import json import api from helloasso_api.oauth2 import OAuth2Api from helloasso_api.apiv5client import ApiV5Client +import dotenv - -clientId = "" -clientSecret = "" +dotenv.load() +clientId = dotenv.get('CLIENTHELLOASSO_ID') +clientSecret = dotenv.get('CLIENTHELLOASSO_SECRET') class MyApi: @@ -37,7 +37,7 @@ def fetchItems(self): "data": [], "total": 0, } - res = self._client.call("organizations/bde-42/forms/event/wed/items").json() + res = self._client.call("organizations/bde-42/forms/event/wed/items?withDetails=true").json() total = res["pagination"]["totalCount"] items["total"] = total items["data"].extend(res["data"]) @@ -49,15 +49,15 @@ def fetchItems(self): items["data"].extend(res["data"]) return items -TOKEN = api.get_token_from_env() +TOKEN = api.get_token(dotenv.get('CLIENT42_ID'), dotenv.get('CLIENT42_SECRET')) -def checkapi(login: str): +def checkapi(first_name: str, last_name: str, login: str): matching_users = api.get(TOKEN, '/v2/users?filter[login]=' + login) return len(matching_users) != 0 def isstud(first_name: str, last_name: str, login: str): - if checkapi(login.lower()): - return "Stud verif ok" + if checkapi(first_name, last_name, login.lower()): + return "Stud verif ok " + login else: return "ALERTE non stud " + login @@ -89,10 +89,7 @@ def isstud(first_name: str, last_name: str, login: str): print(len(res)) elif sys.argv[1] == "list": for item in res: - if 'customFields' in item: - print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " " + isstud(item["user"]["firstName"], item["user"]["lastName"], item["customFields"][0]["answer"])) - else: - print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " pas de CustomFields") + print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " " + isstud(item["user"]["firstName"], item["user"]["lastName"], item["customFields"][0]["answer"])) else: print("Usage: helloasso.py ") sys.exit(1)