Spaces:
Sleeping
Sleeping
File size: 4,393 Bytes
bd36d21 53e7f37 c7ea683 ae0554f 53e7f37 ae0554f d33d53d 53e7f37 d33d53d 3a080c7 c7ea683 3a080c7 c7ea683 3a080c7 c7ea683 3a080c7 380db35 50d1740 380db35 50d1740 380db35 50d1740 380db35 50d1740 e2cfceb 50d1740 380db35 53e7f37 d14a2df 53e7f37 6fcd547 c7ea683 380db35 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
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)
def extract_team_name(object_data):
# Try to get the team name directly
data_field = object_data['data']['stream']['object']['data']
try:
nested_data_str = data_field['data']
nested_data = json.loads(nested_data_str)
team_name = nested_data.get('teamName')
if team_name:
return team_name
except (KeyError, json.JSONDecodeError):
pass
# If not found, check for referencedId and fetch further data
if '@Data' in data_field:
referenced_id = data_field['@Data'].get('referencedId')
if referenced_id:
referenced_object_data = fetch_referenced_object(speckle_token, api_url, stream_id, referenced_id)
print ("__________", referenced_object_data)
if referenced_object_data:
nested_data_str = referenced_object_data['data']['stream']['object']['data']['data']
nested_data = json.loads(nested_data_str)
return nested_data.get('teamName')
return None
@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))
team_name = extract_team_name(object_data)
if team_name:
print(f"Team Name: {team_name}")
await send_data({"teamName": team_name})
else:
print("Team name not found in the object data.")
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()
"""
|