Spaces:
Sleeping
Sleeping
# Speckle stream and authentication information | |
speckle_token = os.getenv("SPECKLE_TOKEN") | |
speckle_server_url = "https://speckle.xyz/" | |
# WebSocket URI | |
ws_uri = "wss://onlinewebsocketserver.onrender.com" | |
websocket = None | |
# Connect to WebSocket | |
async def connect(): | |
global websocket | |
try: | |
websocket = await websockets.connect(ws_uri) | |
print("Connected to the WebSocket server") | |
except Exception as e: | |
print(f"Connection failed: {e}") | |
async def send_data(data): | |
global websocket | |
if websocket is None or websocket.closed: | |
await connect() | |
if websocket is None or websocket.closed: | |
print("Failed to connect to WebSocket server.") | |
return | |
try: | |
await websocket.send(json.dumps(data)) | |
print(f"Sent: {data}") | |
response = await websocket.recv() | |
response_data = json.loads(response) | |
print(f"Received: {response_data}") | |
except Exception as e: | |
print(f"Failed to send or receive data: {e}") | |
websocket = None | |
def extract_branch_info(stream_id, server_url=None, token=None): | |
client = SpeckleClient(host=server_url) | |
client.authenticate_with_token(token=token) | |
branches = client.branch.list(stream_id, 100) | |
branch_info = [] | |
for branch in branches: | |
branch_data = { | |
"Name": branch.name, | |
"description": branch.description, | |
"url": f"{server_url}/streams/{stream_id}/branches/{branch.name.replace('/', '%2F').replace('+', '%2B')}" | |
} | |
commits = client.branch.get(stream_id, branch.name).commits.items | |
if commits: | |
latest_commit = commits[0] | |
branch_data["updated"] = latest_commit.createdAt | |
branch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}" | |
branch_data["commit_message"] = latest_commit.message | |
branch_data["author"] = latest_commit.authorName | |
branch_info.append(branch_data) | |
return branch_info | |
async def update_streams(request: Request): | |
payload = await request.json() | |
print(f"Received webhook payload: {payload}") | |
if 'payload' in payload and 'stream' in payload['payload']: | |
stream_info = payload['payload']['stream'] | |
if 'id' in stream_info: | |
stream_id = stream_info['id'] | |
print(f"Fetching data for stream: {stream_id}") | |
# Extract branch info and send it via WebSocket | |
branch_data = extract_branch_info(stream_id, server_url=speckle_server_url, token=speckle_token) | |
await send_data(branch_data) | |
return "All streams updated successfully!" |