Skip to content

Instantly share code, notes, and snippets.

@CarloCattano
Last active October 2, 2023 11:27
Show Gist options
  • Save CarloCattano/ce147fd30924a9dedb5b26a55d99e08f to your computer and use it in GitHub Desktop.
Save CarloCattano/ce147fd30924a9dedb5b26a55d99e08f to your computer and use it in GitHub Desktop.
42 intra API example using python - check who is on campus and how much eval points they got

You will need your intra API keys secret and UID Follow the recomendations here:

https://api.intra.42.fr/apidoc/guides/getting_started

https://profile.intra.42.fr/oauth/applications/new

Hope it serves as inspiration for something cooler

You need the requests module

pip install requests
#!/usr/bin/env python3

import time
import requests
import json

UID = "u-YOUR-UID"
SECRET = "s-YOUR-SECRET"

def post42(url, payload):
    url = "https://api.intra.42.fr" + url
    payload = payload
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    return response.json()

def get42(url, payload):
    url = "https://api.intra.42.fr" + url
    payload = payload
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()

wtoken = post42("/oauth/token", {"grant_type": "client_credentials", \
                                "client_id": UID, "client_secret": SECRET})
campus_users = []
temp = []

campus_users_general = '/v2/campus/51'
campus_users_total = get42(campus_users_general, {"access_token": wtoken["access_token"]})
total_users = campus_users_total["users_count"]

total_pages = ( total_users // 100 + 1 ) + 1

print(f"Total users: {total_users} Total pages: {total_pages}")
print("Please wait while data from API is retrieved...")

for i in range(1, total_pages):  
    campus_users += get42("/v2/campus/51/users?page[number]=" + str(i) + "&page[size]=100", \
                                                   {"access_token": wtoken["access_token"]})

for user in campus_users:
    temp.append({
        "login": user.get("login"),  
        "correction_point":  user.get("correction_point"),
        "pool_year": user.get("pool_year"),
        "location": user.get("location"),  
        "updated_at": user.get("updated_at").split("T")[1].split(".")[0],
        "wallet": user.get("wallet")
    })

for entry in temp:
    if entry["location"] is not None and entry["pool_year"] == '2022':
        print(f"\033[91m{entry['login']:<10}\033[0m Updated: {entry['updated_at']:<8} Eval Points: {str(entry['correction_point']):<5} Location: {str(entry['location']):<10} Wallet: {str(entry['wallet']):<10}")
    elif entry["location"] is not None and entry["pool_year"] == '2023':
        print(f"\033[92m{entry['login']:<10}\033[0m Updated: {entry['updated_at']:<8} Eval Points: {str(entry['correction_point']):<5} Location: {str(entry['location']):<10} Wallet: {str(entry['wallet']):<10}")

then just simply run

python3 42online.py

You should get something like

asfsafff   Updated: 08:11:12 Eval Points: 1     Location: c4b5c1     Wallet: 50
asgga      Updated: 07:29:12 Eval Points: 6     Location: c4b12c2    Wallet: 20
cdcdddad   Updated: 07:20:48 Eval Points: 6     Location: c0c7c1     Wallet: 85
ddccdcdc   Updated: 07:56:11 Eval Points: 7     Location: c4b10c1    Wallet: 165
eggouot    Updated: 08:17:12 Eval Points: 12    Location: c4b11c3    Wallet: 245
ehsger     Updated: 07:23:13 Eval Points: 12    Location: c4b12c3    Wallet: 50
shasssos   Updated: 07:53:13 Eval Points: 9     Location: c4b5c6     Wallet: 115
sshrlixh   Updated: 07:47:14 Eval Points: 5     Location: c4b8c2     Wallet: 245
sbaumenn   Updated: 07:47:15 Eval Points: 7     Location: c4b9c1     Wallet: 235
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment