# -*- coding: utf-8 -*- from requests import Session from enum import IntEnum, unique from functools import wraps, lru_cache class Basic: def __init__(self, proxy=None): self.session = Session() self.session.proxies = proxy def csrf_token(self): with self.session.post("https://auth.roblox.com/v2/login") as r: return r.headers["X-CSRF-TOKEN"] def post(self, url, data=None, json=None, **kwargs): kwargs.update(headers={"X-CSRF-TOKEN": self.csrf_token()}) return self.session.post(url, data, json, **kwargs) def close(self): self.session.close() def __enter__(self): return self def __exit__(self, *args): self.close() def null_check(message): def decorator(func): @wraps(func) def wrap(*args, **kwargs): value = func(*args, **kwargs) if value == "null": raise Exception(message) return value return wrap return decorator def error_check(func): @wraps(func) def decorator(*args, **kwargs): json = func(*args, **kwargs) if "errors" in json: raise Exception(json["errors"]) return json return decorator class Authenticated(Basic): def __init__(self, cookie, proxy=None): super().__init__(proxy) self.session.cookies[".ROBLOSECURITY"] = cookie @lru_cache() @null_check("Invalid cookie") def current_user(self): with self.session.get("https://assetgame.roblox.com/Game/GetCurrentUser.ashx") as r: return r.text class PlaceAPI(Authenticated): def __init__(self, cookie, proxy=None): super().__init__(cookie, proxy) self.session.headers["User-Agent"] = "Roblox/WinInet" @null_check("Invalid ticket recieved") def auth_ticket(self, place): headers = { "RBX-For-Gameauth": True, "Referer": f"https://www.roblox.com/games/{place}" } with self.session.get("https://www.roblox.com/game-auth/getauthticket", headers=headers) as r: return r.text def place_launch(self, place, game): params = dict(request="RequestGameJob", placeId=place, gameId=game, isPlayTogetherGame=False) with self.session.get("https://assetgame.roblox.com/game/PlaceLauncher.ashx", params=params) as r: return r.json() @unique class Status(IntEnum): OFFLINE = 0 ONLINE = 1 PLAYING = 2 DEVELOPING = 3 class UserAPI(Authenticated): def __init__(self, cookie, proxy=None): super().__init__(cookie, proxy) @error_check def user_presence(self, *args): with self.post("https://presence.roblox.com/v1/presence/users", json=dict(userIds=args)) as r: return r.json() @error_check def username_info(self, user): with self.session.get("http://api.roblox.com/users/get-by-username", params=dict(username=user)) as r: return r.json()