133 lines
4.3 KiB
Python
133 lines
4.3 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
import httpx
|
|
import json
|
|
from app.env import CLIENT_NAME, API_BASE
|
|
from app.vrchat_context import VRChatContext
|
|
|
|
router = APIRouter()
|
|
|
|
def load_context():
|
|
VRChatContext.load()
|
|
|
|
@router.get("/users/me")
|
|
async def get_bot_users_profile():
|
|
load_context()
|
|
vrchat = VRChatContext.get()
|
|
if not vrchat:
|
|
raise HTTPException(status_code=401, detail="Token not found, please authenticate first")
|
|
|
|
auth_cookie = vrchat.auth_cookie
|
|
if not auth_cookie:
|
|
raise HTTPException(status_code=401, detail="Auth cookie missing in token")
|
|
|
|
headers = {"User-Agent": CLIENT_NAME}
|
|
cookies = {"auth": auth_cookie}
|
|
url = f"{API_BASE}/users/{vrchat.user_id}"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url, headers=headers, cookies=cookies)
|
|
|
|
if r.status_code != 200:
|
|
raise HTTPException(status_code=r.status_code, detail=f"Failed to fetch user (current bot) info: {r.text}")
|
|
|
|
return r.json()
|
|
|
|
@router.get("/users/{user_id}")
|
|
async def get_user(user_id: str):
|
|
load_context()
|
|
vrchat = VRChatContext.get()
|
|
if not vrchat:
|
|
raise HTTPException(status_code=401, detail="Token not found, please authenticate first")
|
|
|
|
auth_cookie = vrchat.auth_cookie
|
|
if not auth_cookie:
|
|
raise HTTPException(status_code=401, detail="Auth cookie missing in token")
|
|
|
|
headers = {"User-Agent": CLIENT_NAME}
|
|
cookies = {"auth": auth_cookie}
|
|
url = f"{API_BASE}/users/{user_id}"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url, headers=headers, cookies=cookies)
|
|
|
|
if r.status_code != 200:
|
|
raise HTTPException(status_code=r.status_code, detail=f"Failed to fetch user info: {r.text}")
|
|
|
|
return r.json()
|
|
|
|
@router.get("/users/{user_id}/friends/status")
|
|
async def get_user_friend_status(user_id: str):
|
|
load_context()
|
|
vrchat = VRChatContext.get()
|
|
if not vrchat:
|
|
raise HTTPException(status_code=401, detail="Token not found, please authenticate first")
|
|
|
|
auth_cookie = vrchat.auth_cookie
|
|
if not auth_cookie:
|
|
raise HTTPException(status_code=401, detail="Auth cookie missing in token")
|
|
|
|
headers = {"User-Agent": CLIENT_NAME}
|
|
cookies = {"auth": auth_cookie}
|
|
url = f"{API_BASE}/users/{user_id}/friendStatus"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url, headers=headers, cookies=cookies)
|
|
|
|
if r.status_code != 200:
|
|
raise HTTPException(status_code=r.status_code, detail=f"Failed to fetch user friend status info: {r.text}")
|
|
|
|
return r.json()
|
|
|
|
@router.get("/users/{user_id}/worlds")
|
|
async def get_user_worlds(user_id: str):
|
|
load_context()
|
|
vrchat = VRChatContext.get()
|
|
if not vrchat:
|
|
raise HTTPException(status_code=401, detail="Token not found, please authenticate first")
|
|
|
|
auth_cookie = vrchat.auth_cookie
|
|
if not auth_cookie:
|
|
raise HTTPException(status_code=401, detail="Auth cookie missing in token")
|
|
|
|
headers = {"User-Agent": CLIENT_NAME}
|
|
cookies = {"auth": auth_cookie}
|
|
params = {
|
|
"releaseStatus": "public",
|
|
"sort": "updated",
|
|
"order": "descending",
|
|
"userId": user_id,
|
|
"n": "100",
|
|
"offset": "0"
|
|
}
|
|
url = f"{API_BASE}/worlds"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url, headers=headers, cookies=cookies, params=params)
|
|
|
|
if r.status_code != 200:
|
|
raise HTTPException(status_code=r.status_code, detail=f"Failed to fetch user worlds info: {r.text}")
|
|
|
|
return r.json()
|
|
|
|
@router.get("/users/{user_id}/groups")
|
|
async def get_user_groups(user_id: str):
|
|
load_context()
|
|
vrchat = VRChatContext.get()
|
|
if not vrchat:
|
|
raise HTTPException(status_code=401, detail="Token not found, please authenticate first")
|
|
|
|
auth_cookie = vrchat.auth_cookie
|
|
if not auth_cookie:
|
|
raise HTTPException(status_code=401, detail="Auth cookie missing in token")
|
|
|
|
headers = {"User-Agent": CLIENT_NAME}
|
|
cookies = {"auth": auth_cookie}
|
|
url = f"{API_BASE}/users/{user_id}/groups"
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
r = await client.get(url, headers=headers, cookies=cookies)
|
|
|
|
if r.status_code != 200:
|
|
raise HTTPException(status_code=r.status_code, detail=f"Failed to fetch user groups info: {r.text}")
|
|
|
|
return r.json() |