난 정말 최고야 멋있어
42 남은 인원 계산기? 제작기 본문
#! /usr/bin/python3
from sys import exec_prefix
import requests
import json
from time import sleep
from bs4 import BeautifulSoup
from getpass import getpass
G_MAX = 149
G_MIN = G_MAX - 5
S_MAX = 150
S_MIN = S_MAX - 5
id = input("id : ")
pw = getpass("pw : ")
s = requests.Session()
res = s.get('https://cluster.42seoul.io/api/user/login')
soup = BeautifulSoup(res.text, 'html.parser')
auth_token = soup.select_one('#new_user > input[type=hidden]:nth-child(2)')["value"]
post_data = {
"utf8" : "✓",
"authenticity_token" : auth_token,
"user[login]" : id,
"user[password]": pw,
"commit": "Sign+in"
}
print('/////// first get ////////')
print(f'auth token = {auth_token}')
res = s.post('https://signin.intra.42.fr/users/sign_in' ,post_data)
soup = BeautifulSoup(res.text, 'html.parser')
client_id = soup.select_one('#client_id')
if client_id == None:
print('Wrong Id/PW')
exit(1)
auth_token = soup.select_one('#container > div > main > div.actions > form:nth-child(1) > input[type=hidden]:nth-child(2)')["value"]
client_id = client_id["value"]
post_data = {
"utf8" : "✓",
"authenticity_token" : auth_token,
"client_id" : client_id,
"redirect_uri": "https://cluster.42seoul.io/api/user/login/callback",
"state": "",
"response_type": "code",
"scope": "public",
"commit": "Authorize"
}
print('/////// second post ////////')
print(f'auth token = {auth_token}')
print(f'client_id = {client_id}')
print('=====================================')
res = s.post('https://api.intra.42.fr/oauth/authorize', post_data)
data = {'login': '', 'card': None, 'gaepo': 0, 'seocho': 0, 'isAdmin': False}
i = 0
last_gaepo = G_MAX
last_seocho = S_MAX
while True:
try:
res = s.get('https://cluster.42seoul.io/api/user/status')
data = json.loads(res.text)
gaepo_count = int(data["gaepo"])
seocho_count = int(data["seocho"])
if gaepo_count >= G_MIN and gaepo_count != G_MAX + 1 and last_gaepo != gaepo_count:
requests.post('https://discord.com/api/webhooks/HOOK_ID1/HOOK_TOKEN1',
data={"content" : f"{G_MAX - gaepo_count}명 남았따!!!!"})
if seocho_count >= S_MIN and seocho_count != S_MAX + 1 and last_seocho != seocho_count:
requests.post('https://discord.com/api/webhooks/HOOK_ID2/HOOK_TOKEN2',
data={"content" : f"{S_MAX - seocho_count}명 남았따!!!!"})
if last_gaepo != gaepo_count or last_seocho != seocho_count:
requests.patch('https://discord.com/api/webhooks/HOOK_ID3/HOOK_TOKEN3/messages/MESSAGE_ID',
data={"content" : f"개포 현재 인원: {gaepo_count} / {G_MAX}\n서초 현재 인원: {seocho_count} / {S_MAX}"})
last_gaepo = gaepo_count
last_seocho = seocho_count
print('\033[2J') # clear screen
print(f"[INFO] Server is Running" + "." * ((i % 3) + 1))
i += 1
print(f"gaepo Count : {gaepo_count} / {G_MAX}")
print(f'seocho Count : {seocho_count} / {S_MAX}')
sleep(3)
except:
pass
간단한 코드다
피들러를 이용해서 post data 를 전송할때 어떤 요소를 보내는지 확인을 해 파이썬으로 간단한 코드를 작성을 하였다
참고로 노드js 와 달리 파이썬의 requests 에서는 post 요청을 보낼때 기본적으로 form-data 형식으로 보내는것 같다