Spaces:
Sleeping
Sleeping
File size: 2,664 Bytes
53e7f37 ae0554f 53e7f37 ae0554f 53e7f37 3a080c7 53e7f37 3a080c7 53e7f37 3a080c7 626eb22 3a080c7 53e7f37 d14a2df 53e7f37 6fcd547 53e7f37 3a080c7 53e7f37 3a080c7 53e7f37 |
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 74 75 76 |
# 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
@webhook_endpoint
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!" |