Spaces:
Sleeping
Sleeping
File size: 3,574 Bytes
bd36d21 53e7f37 c7ea683 ae0554f 53e7f37 ae0554f d33d53d 53e7f37 d33d53d 3a080c7 c7ea683 3a080c7 c7ea683 3a080c7 c7ea683 3a080c7 53e7f37 d14a2df 53e7f37 6fcd547 c7ea683 3a080c7 c7ea683 31c7a45 c7ea683 3a080c7 c7ea683 bd36d21 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import os
import json
import requests
from fastapi import Request
import websockets
import asyncio
from huggingface_hub import webhook_endpoint
# Speckle stream and authentication information
speckle_token = os.getenv("SPECKLE_TOKEN")
api_url = "https://speckle.xyz/graphql"
stream_id = "1dab2d05eb"
branch_name = "scenario_sycer"
# WebSocket URI
ws_uri = "wss://onlinewebsocketserver.onrender.com"
# Connect to WebSocket and send data
async def send_data(data):
websocket = None
try:
websocket = await websockets.connect(ws_uri)
print("Connected to the WebSocket server")
await websocket.send(json.dumps(data))
print(f"Sent: {data}")
except Exception as e:
print(f"Failed to send data: {e}")
finally:
if websocket:
await websocket.close()
print("WebSocket connection closed.")
def send_graphql_query(speckleToken, apiUrl, query):
headers = {
"Authorization": f"Bearer {speckleToken}",
"Content-Type": "application/json",
"Accept": "application/json"
}
response = requests.post(apiUrl, headers=headers, json={"query": query})
if response.status_code == 200:
return response.json()
else:
print(f"HTTP Error: {response.status_code}, {response.text}")
return None
def construct_commit_query(stream_id, branch_name):
return f"""
{{
stream(id: "{stream_id}") {{
branch(name: "{branch_name}") {{
commits(limit: 1) {{
items {{
id
message
referencedObject
}}
}}
}}
}}
}}
"""
def fetch_referenced_object(speckleToken, apiUrl, stream_id, object_id):
query = f"""
{{
stream(id: "{stream_id}") {{
object(id: "{object_id}") {{
id
data
}}
}}
}}
"""
return send_graphql_query(speckleToken, apiUrl, query)
@webhook_endpoint
async def update_streams(request: Request):
payload = await request.json()
print(f"Received webhook payload: {payload}")
commit_query = construct_commit_query(stream_id, branch_name)
result = send_graphql_query(speckle_token, api_url, commit_query)
if result and 'data' in result:
commit_data = result['data']['stream']['branch']['commits']['items'][0]
referenced_object_id = commit_data['referencedObject']
print(f"Referenced Object ID: {referenced_object_id}")
object_data = fetch_referenced_object(speckle_token, api_url, stream_id, referenced_object_id)
if object_data:
print(json.dumps(object_data, indent=2))
# Extract the 'data' field, which is a JSON string
nested_data_str = object_data['data']['stream']['object']['data']['data']
# Parse the JSON string to a dictionary
nested_data = json.loads(nested_data_str)
# Access the 'teamName' field
team_name = nested_data.get('teamName', 'N/A')
print(f"Team Name: {team_name}")
# Send the team name to the WebSocket server
await send_data(team_name)
else:
print("Failed to retrieve the referenced object data.")
else:
print("Failed to retrieve commit data.")
return "Data sent successfully!"
# Uncomment below if you want to use Gradio for a manual interface
"""
import gradio as gr
iface = gr.Interface(
fn=update_streams,
inputs=gr.components.Button(value="Update Streams"),
outputs="text",
)
iface.launch()
"""
|