Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 21 additions & 5 deletions helloasso.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#! /usr/bin/env python3

import sys
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:
Expand Down Expand Up @@ -34,18 +37,31 @@ 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"])
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(dotenv.get('CLIENT42_ID'), dotenv.get('CLIENT42_SECRET'))

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(first_name, last_name, login.lower()):
return "Stud verif ok " + login
else:
return "ALERTE non stud " + login



auth = OAuth2Api(
"api.helloasso.com", client_id=clientId, client_secret=clientSecret, timeout=99999
Expand Down Expand Up @@ -73,7 +89,7 @@ def fetchItems(self):
print(len(res))
elif sys.argv[1] == "list":
for item in res:
print(item["user"]["firstName"], item["user"]["lastName"])
print(item["user"]["firstName"] + " " + item["user"]["lastName"] + " " + isstud(item["user"]["firstName"], item["user"]["lastName"], item["customFields"][0]["answer"]))
else:
print("Usage: helloasso.py <total | list>")
sys.exit(1)