First push

This commit is contained in:
Starthur 2022-10-15 13:00:01 +02:00
commit 4792d91de5
Signed by: Starthur
GPG Key ID: 68AD34A2CF658E32
4 changed files with 132 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
venv/
config.py

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# Get all users registred at an event
## API Cred
You must register a new API key on https://profile.intra.42.fr/oauth/applications/new and create a file
`config.py` with :
```python
CLIENT_ID_API42 = "<client_uid>"
CLIENT_SECRET_API42 = "<client_secret>"
```
For dependencies :
```
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
(Or just the last command if you don't care installing package everywhere on your system)
## Use
```
python event_user.py
```

105
event_user.py Normal file
View File

@ -0,0 +1,105 @@
import requests
import datetime
from beaupy import *
from beaupy.spinners import *
from prettytable import PrettyTable
from . import config
############
## CONFIG ##
############
CLIENT_ID_API42 = config.CLIENT_ID_API42
CLIENT_SECRET_API42 = config.CLIENT_SECRET_API42
ENDPOINT_API42 = "https://api.intra.42.fr/"
###############
## FUNCTIONS ##
###############
class EVENT:
infos = dict()
users = dict()
def get_api_cred() -> str:
payload = {
'grant_type' : 'client_credentials',
'client_id' : CLIENT_ID_API42,
'client_secret' : CLIENT_SECRET_API42
}
r = requests.post(ENDPOINT_API42 + "/oauth/token", params=payload)
if r.status_code != 200:
print("Can't connect to 42API. Can't check if login are correct")
exit(1)
return (r.json()["access_token"])
def get_event_info_by_id(api_cred: str, id: str) -> dict:
headers = {
'Authorization' : 'Bearer ' + api_cred
}
r = requests.get(ENDPOINT_API42 + "/v2/events/" + id, headers=headers)
return (r.json())
def get_event_info_by_begin_date(api_cred: str, user_date: str, user_hour: str) -> dict:
day, month, year = map(int, user_date.split('/'))
hour, minute = map(int, user_hour.split(':'))
iso_time = datetime.datetime(year, month, day, hour, minute).isoformat()
headers = {
'Authorization' : 'Bearer ' + api_cred
}
r = requests.get(ENDPOINT_API42 + "/v2/events/?filter[begin_at]=" + iso_time, headers=headers)
return (r.json())
def get_event_user(api_cred: str, id: str, nb_user: int) -> dict:
headers = {
'Authorization' : 'Bearer ' + api_cred
}
page = 1
while nb_user > 0:
r = requests.get(ENDPOINT_API42 + "/v2/events/" + str(id) + "/events_users?page[number]=" + str(page), headers=headers)
data = r.json()
for a in data:
EVENT.users[a["user"]["usual_full_name"]] = a["user"]
nb_user -= 30
page += 1
def print_user() -> None:
t = PrettyTable()
t.align = "l"
t.field_names = ["Full name", "Email", "Link intra"]
for user in EVENT.users.values():
t.add_row([user["displayname"], user["email"], f"https://profile.intra.42.fr/users/{user['login']}"])
print(t)
console.print("[blue]Retrieve stud who register to an event[/blue]")
console.print("Do you want to search event by id or by date ?")
search_choice = select(["By id", "By date"])
if "By id" in search_choice:
id = prompt("Enter event id")
EVENT.infos = get_event_info_by_id(get_api_cred(), id)
elif "By date" in search_choice:
console.print("[red]Warning ![/red] Date and hour must be UTC")
date_entry = prompt("Enter begin_date of the event in DD/MM/YYYY format")
hour_entry = prompt("Enter begin hour in HH:MM format")
EVENT.infos = get_event_info_by_begin_date(get_api_cred(), date_entry, hour_entry)[0]
console.clear()
console.print("[blue]Event infos :[blue]")
console.print(f"\t[green]Name:[/green] {EVENT.infos['name']}")
console.print(f"\t[green]Participants:[/green] {EVENT.infos['nbr_subscribers']}/{EVENT.infos['max_people']}")
console.print(f"\t[green]Begin at:[/green] {EVENT.infos['begin_at']}")
console.print(f"\t[green]End at:[/green] {EVENT.infos['end_at']}")
console.print("\n")
if confirm("Is this the correct event ?"):
spinner_animation = ['4⃣ 2⃣ 👌 ', '4⃣ 2⃣ 👌 ', '4⃣ 2⃣ 👌 ', '4⃣ 2⃣ 👌 ', '4⃣ 2⃣👌 ', '4⃣ 2⃣💩💩💩💩💩']
spinner = Spinner(spinner_animation, "Fetching 42API...")
spinner.start()
get_event_user(get_api_cred(), EVENT.infos["id"], EVENT.infos["nbr_subscribers"])
spinner.stop()
print_user()

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
beaupy==1.5.0
prettytable==3.4.1
requests==2.27.1