106 lines
3.4 KiB
Python
106 lines
3.4 KiB
Python
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()
|