Spaces:
Running
Running
import requests | |
import gradio as gr | |
from urllib.parse import urlparse, parse_qs | |
import pre | |
def id_check(request: gr.Request): | |
param = str(request.query_params) | |
if not "code=" in param: | |
return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown(info='Login to create preset') | |
code = param.split('code=') | |
print(code[1]) | |
user_id, username, avatar_url = get_discord_user_profile(str(code[1])) | |
if user_id == None: | |
return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown(info='Login to create preset') | |
if user_id == "412827396027187211": | |
return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=True), gr.Tab(visible=True), user_id, gr.Tab(visible=True), gr.Dropdown(info='') | |
print(user_id) | |
return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=False), gr.Tab(visible=False), user_id, gr.Tab(visible=True), gr.Dropdown(info='') | |
#1 | |
def get_discord_user_profile(code): | |
# Discordμ API μλν¬μΈνΈ | |
token_url = "https://discord.com/api/oauth2/token" | |
user_url = "https://discord.com/api/users/@me" | |
try: | |
# ν ν° μμ²μ μν λ°μ΄ν° | |
data = { | |
'client_id': client_id, # client_id μ€μ | |
'client_secret': client_secret, # client_secret μ€μ | |
'grant_type': 'authorization_code', | |
'code': code, | |
'redirect_uri': redirect_uri # redirect_uri μ€μ | |
} | |
headers = { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
# μ‘μΈμ€ ν ν° μμ² | |
token_response = requests.post(token_url, data=data, headers=headers) | |
token_response.raise_for_status() # μ€λ₯ λ°μμ μμΈ λ°μ | |
access_token = token_response.json().get('access_token') | |
# μ¬μ©μ μ 보 μμ² | |
auth_headers = { | |
'Authorization': f'Bearer {access_token}' | |
} | |
user_response = requests.get(user_url, headers=auth_headers) | |
user_response.raise_for_status() # μ€λ₯ λ°μμ μμΈ λ°μ | |
user_info = user_response.json() | |
user_id = user_info.get('id') | |
username = user_info.get('username') + '#' + user_info.get('discriminator') | |
avatar_hash = user_info.get('avatar') | |
avatar_url = f"https://cdn.discordapp.com/avatars/{user_id}/{avatar_hash}.png" if avatar_hash else None | |
return user_id, username, avatar_url | |
except requests.exceptions.RequestException as e: | |
print("Error to login") | |
# print(str(e)) # μ€λ₯ λ΄μ© μΆλ ₯μ μνλ€λ©΄ μ£Όμ ν΄μ | |
return None, None, None | |
# μ¬μ© μ: | |
client_id = "1239473790732472370" | |
client_secret = "C7RLdB4YArSM9jVk8tsznQnKOzwGKwsO" | |
# code = "THE_CODE_FROM_REDIRECT_URI" | |
#redirect_uri = "https://chohj06ms-objektify2.hf.space/" | |
redirect_uri = pre.login_uri | |