forked from Andcool-Systems/Discord-OpenGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
73 lines (62 loc) · 2.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import aiohttp
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
import uvicorn
from dotenv import load_dotenv
import os
load_dotenv()
app = FastAPI()
@app.get("/", response_class=RedirectResponse)
async def root():
return RedirectResponse(url="https://github.com/Andcool-Systems/Discord-OpenGraph/tree/main")
@app.get("/{uid}")
async def uid(uid: str, request: Request):
if uid == 'favicon.ico':
return
user_data = None
async with aiohttp.ClientSession("https://discord.com") as session:
async with session.get(f"/api/v10/users/{uid}/profile", headers={"Authorization": os.getenv("TOKEN")}) as response:
if response.status == 200:
user_data = (await response.json())['user']
if not user_data:
async with session.get(f"/api/v10/users/{uid}", headers={"Authorization": os.getenv("TOKEN")}) as response:
if response.status == 200:
user_data = await response.json()
if not user_data:
return JSONResponse({
'status': 'error',
'message': 'user not found'
},
status_code=404
)
if request.headers.get('accept') == 'application/json':
response_data = {
"global_name": user_data.get('global_name', user_data.get('username')),
"username": user_data.get('username'),
"banner_color": user_data.get('banner_color', None),
"avatar": f"https://cdn.discordapp.com/avatars/{uid}/{user_data.get('avatar')}",
"bio": user_data.get('bio', None),
"url": f"https://discord.com/users/{uid}"
}
return JSONResponse(content=response_data)
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<meta property="og:title" content="{user_data.get('global_name', user_data.get('username'))}">
<meta name="theme-color" content="{user_data.get('banner_color', '#2563eb')}">
<meta property="og:url" content="https://discord.com/users/{uid}" />
<meta property="og:site_name" content="Discord" />
<meta property="og:image" content="https://cdn.discordapp.com/avatars/{uid}/{user_data.get('avatar')}?size=1024" />
<meta property="og:description" content="{user_data.get('bio', '')}" />
</head>
<body>
</body>
<script>
window.location.replace("https://discord.com/users/{uid}");
</script>
</html>
"""
return HTMLResponse(content=html_content)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8765)