Upload files to "/"
This commit is contained in:
161
RecNet.py
Normal file
161
RecNet.py
Normal file
@@ -0,0 +1,161 @@
|
||||
from enum import Enum
|
||||
import httpx
|
||||
import json
|
||||
import dto
|
||||
|
||||
RecNetDomain = "rec.net"
|
||||
|
||||
auth = None
|
||||
refreshToken = None
|
||||
clientSecret = "54oc5C4BH7jpqp42KhChkNZOLP0U3yTx"
|
||||
|
||||
c = httpx.Client(http1=False, http2=True)
|
||||
|
||||
class Service(Enum):
|
||||
Accounts = "accounts"
|
||||
AI = "ai"
|
||||
API = "api"
|
||||
Auth = "auth"
|
||||
BugReporting = "bugreporting"
|
||||
Cards = "cards"
|
||||
CDN = "cdn"
|
||||
Chat = "chat"
|
||||
Clubs = "clubs"
|
||||
CMS = "cms"
|
||||
Commerce = "commerce"
|
||||
Data = "data"
|
||||
DataCollection = "datacollection"
|
||||
Discovery = "discovery"
|
||||
Econ = "econ"
|
||||
GameLogs = "gamelogs"
|
||||
Geo = "geo"
|
||||
Images = "img"
|
||||
Leaderboard = "leaderboard"
|
||||
Link = "link"
|
||||
Lists = "lists"
|
||||
Matchmaking = "match"
|
||||
Moderation = "moderation"
|
||||
Notifications = "notify"
|
||||
PlatformNotifications = "platformnotifications"
|
||||
PlayerSettings = "playersettings"
|
||||
RoomComments = "roomcomments"
|
||||
Rooms = "rooms"
|
||||
Storage = "storage"
|
||||
Strings = "strings"
|
||||
StringsCDN = "strings-cdn"
|
||||
Studio = "studio"
|
||||
Thorn = "thorn"
|
||||
Videos = "videos"
|
||||
WWW = "www"
|
||||
|
||||
class AvatarBodyType(Enum):
|
||||
Bean = 0
|
||||
FB = 1
|
||||
UpgradedBean = 2
|
||||
|
||||
class HTTPMethod(Enum):
|
||||
GET = "GET"
|
||||
HEAD = "HEAD"
|
||||
POST = "POST"
|
||||
PUT = "PUT"
|
||||
DELETE = "DELETE"
|
||||
PATCH = "PATCH"
|
||||
OPTIONS = "OPTIONS"
|
||||
TRACE = "TRACE"
|
||||
CONNECT = "CONNECT"
|
||||
|
||||
class RecNetError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return self.message
|
||||
|
||||
def __repr__(self):
|
||||
return f"RecNetError({self.message})"
|
||||
|
||||
def sendRequest(method: HTTPMethod, service: Service, path: str, useAuth=True, json=None, files=None, data=None, https: bool=True):
|
||||
"""
|
||||
Sends an HTTP request to a specified service and path using the given HTTP method.
|
||||
"""
|
||||
global auth, refreshToken
|
||||
if service == Service.CDN or service == Service.Images:
|
||||
useAuth = False
|
||||
scheme = "http"
|
||||
if https:
|
||||
scheme = "https"
|
||||
url = f"{scheme}://{service.value}.{RecNetDomain}/{path}"
|
||||
headers = {
|
||||
"User-Agent": "BestHTTP"
|
||||
}
|
||||
if auth and useAuth:
|
||||
headers.update({
|
||||
"Authorization": f"Bearer {auth}"
|
||||
})
|
||||
req = c.request(method.value, url, headers=headers, json=json, files=files, data=data)
|
||||
if req.status_code == 401:
|
||||
if auth:
|
||||
print("Access token expired, refreshing...")
|
||||
auth = None
|
||||
re = loginWithRefreshToken(refreshToken)
|
||||
if re.error is not None:
|
||||
raise RecNetError(f"Failed to refresh token: {re.error}")
|
||||
auth = re.accessToken
|
||||
refreshToken = re.refreshToken
|
||||
sendRequest(method, service, path, json=json, files=files, data=data, https=https)
|
||||
else:
|
||||
raise RecNetError("Unauthorized: No access token provided.")
|
||||
return req
|
||||
|
||||
def genAuthCode():
|
||||
"""
|
||||
Generates a device authorization code by sending a POST request to the authentication api.
|
||||
|
||||
Returns:
|
||||
dto.DeviceAuthorization: An object containing the device authorization response if successful.
|
||||
None: If the request fails or no response is received.
|
||||
|
||||
Side Effects:
|
||||
Prints an error message to the console if the device authorization response fails.
|
||||
"""
|
||||
payload = {"client_id": "recroom.studio", "client_secret": clientSecret}
|
||||
req = sendRequest(HTTPMethod.POST, Service.Auth, "connect/deviceauthorization", data=payload)
|
||||
if req is not None:
|
||||
return dto.DeviceAuthorization(req.json())
|
||||
else:
|
||||
print("Failed to get device authorization response.")
|
||||
return None
|
||||
|
||||
def loginWithDeviceCode(DeviceCode: str):
|
||||
payload = {"client_id": "recroom.studio", "client_secret": clientSecret, "grant_type": "urn:ietf:params:oauth:grant-type:device_code", "device_code": DeviceCode}
|
||||
req = sendRequest(HTTPMethod.POST, Service.Auth, "connect/token", data=payload)
|
||||
if req is not None:
|
||||
return dto.Login(req.json())
|
||||
else:
|
||||
print("Failed to login.")
|
||||
return None
|
||||
|
||||
def loginWithRefreshToken(refreshToken: str):
|
||||
payload = {"client_id": "recroom.studio", "client_secret": clientSecret, "grant_type": "refresh_token", "refresh_token": refreshToken}
|
||||
req = sendRequest(HTTPMethod.POST, Service.Auth, "connect/token", data=payload)
|
||||
print(refreshToken)
|
||||
if req is not None:
|
||||
return dto.Login(req.json())
|
||||
else:
|
||||
print("Failed to login with refresh token.")
|
||||
return None
|
||||
|
||||
class FileType(Enum):
|
||||
Unknown = 0
|
||||
SubRoomSave = 1
|
||||
Holotar = 2
|
||||
Image = 3
|
||||
Video = 4
|
||||
Invention = 5
|
||||
RoomMetadata = 6
|
||||
|
||||
def upload(fileType: FileType, data: bytes):
|
||||
payload = {'FileType': fileType.value}
|
||||
files = {"File": ("file.bin", data, "application/octet-stream"),}
|
||||
return dto.Upload(sendRequest(HTTPMethod.POST, Service.Storage, "upload", data=payload, files=files).json())
|
||||
Reference in New Issue
Block a user