Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768691 unmodified line7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289#!pip install -q specklepy# lone github repro & add to sys pathsimport gradio as grimport osimport requestsfrom specklepy.api.client import SpeckleClientfrom specklepy.api.credentials import get_default_account, get_local_accountsfrom specklepy.transports.server import ServerTransportfrom specklepy.api import operationsfrom specklepy.objects.geometry import Polyline, Pointfrom huggingface_hub import webhook_endpoint, WebhookPayloadfrom fastapi import Requestimport requestsimport datetimeimport json# dictionary that collects tags that can be added to commit messages.# key -> column name in notion# value -> possible valuestag_dict = {"status-message":["WIP", "ReviewNeeded", "Final"]}# tag part seperator -> #+ , other_tagsdef extract_branch_info(stream_id, server_url=None, token=None):if server_url and token:client = SpeckleClient(host=server_url)client.authenticate_with_token(token=token)branches = client.branch.list(stream_id, 100)branch_info = []for branch in branches:print (branch.name)branch_data = {"Name": branch.name,"description": branch.description,"url": f"{server_url}/streams/{stream_id}/branches/{branch.name.replace('/', '%2F').replace('+', '%2B')}"}# Determine sub-variant#if branch.name.startswith(("template_geometry", "graph_geometry")):if '+' in branch.name:slash_index = branch.name.rfind('/')plus_index = branch.name.find('+', slash_index)sub_variant = branch.name[slash_index + 1:plus_index]else:sub_variant = "default"branch_data["sub-variant"] = sub_variant# Get commits for the branch#try:commits = client.branch.get(stream_id, branch.name).commits.itemsif commits:latest_commit = commits[0]1 unmodified linebranch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}"branch_data["commit_message"] = latest_commit.messagebranch_data["author"] = latest_commit.authorName# Check if the commit message contains '#+' and then extract tagsif '#+' in latest_commit.message:tags_part = latest_commit.message.split("#+")[1]branch_data["status-message"] = [tg for tg in tag_dict["status-message"] if tg in tags_part]else:branch_data["status-message"] = []#except Exception as e:# print(f"Error fetching commits for branch {branch.name}: {str(e)}")branch_info.append(branch_data)return branch_infodef update_select_options(database_id, headers, branch_names):update_payload = {"properties": {"depends-on": {"multi_select": {"options": [{"name": name} for name in branch_names]}}}}response = requests.patch(f"https://api.notion.com/v1/databases/{database_id}", headers=headers, json=update_payload)response.raise_for_status()def sync_to_notion(token, database_id, branch_data):base_url = "https://api.notion.com/v1"headers = {"Authorization": f"Bearer {token}","Notion-Version": "2022-06-28","Content-Type": "application/json"}# Extract all branch names for the "depends-on" optionsbranch_names = [branch['Name'] for branch in branch_data]# Update the "depends-on" multi-select options to match branch namesupdate_select_options(database_id, headers, branch_names)try:# Fetch existing data from Notion databaseresponse = requests.post(f"{base_url}/databases/{database_id}/query", headers=headers)response.raise_for_status()pages = response.json().get('results', [])# Create a dictionary for the latest update time of each branchbranch_details = {branch['Name']: {'updated': branch.get('updated', ''), 'commit_message': branch.get('commit_message', '')} for branch in branch_data}# Status color mappingstatus_colors = {"outdated": "red","up-to-date": "green","empty": "gray"}# Process each branch and update or create Notion rowsprint("branchData", branch_data)for branch in branch_data:# Find the corresponding page in Notionbranch_update_time = branch_details.get(branch['Name'], {}).get('updated', '')page_id = Noneexisting_depends_on = []for page in pages:notion_name = page['properties']['Name']['title'][0]['text']['content'] if page['properties']['Name']['title'] else ''if notion_name == branch['Name']:page_id = page['id']# Retain the existing value of "depends-on"existing_depends_on = [dep['name'] for dep in page['properties'].get('depends-on', {}).get('multi_select', [])]break# Determine status based on dependenciesstatus_tag = "up-to-date" # Default statusfor dependency_name in existing_depends_on:dependency_info = branch_details.get(dependency_name, {})branch_update_time = branch_details.get(branch['Name'], {}).get('updated', '')# Check if the dependency is more recent and if its commit message does not contain 'isConnected'if dependency_info.get('updated', '') > branch_update_time and "isConnected" not in dependency_info.get('commit_message', ''):status_tag = "outdated"break# If there's no update information, set status to 'empty'if not branch_update_time:status_tag = "empty"# Prepare data for updating or creating a pageprint (branch.get("url", ""))updated_value = branch.get("updated")if isinstance(updated_value, datetime.datetime): # Use datetime.datetime hereupdated_isoformat = updated_value.isoformat()else:updated_isoformat = datetime.datetime.now().isoformat() # Use datetime.datetime.now() here# Prepare status-message tags for multi-selectstatus_messages = branch.get("status-message", [])status_message_data = [{"name": sm} for sm in status_messages]url_clean = branch.get("url", "")url_clean = url_clean.replace("xyz//","xyz/")page_data = {"properties": {"Name": {"title": [{"text": {"content": branch['Name']}}]},"status-tag": {"select": {"name": status_tag}}, # Assuming 'select' type"status": {"rich_text": [{"text": {"content": "Status: " + status_tag}}]},"url": {"url": url_clean},"updated": {"date": {"start": updated_isoformat}},"description": {"rich_text": [{"text": {"content": str(branch.get("description", ""))}}]},"sub-variant": {"rich_text": [{"text": {"content": branch.get("sub-variant", "")}}]},"author": {"rich_text": [{"text": {"content": branch.get("author", "")}}]},"commit_message": {"rich_text": [{"text": {"content": branch.get("commit_message", "")}}]},"depends-on": {"multi_select": [{"name": d} for d in existing_depends_on]},"status-message": {"multi_select": status_message_data}}}# Update an existing page or create a new oneif page_id:update_url = f"{base_url}/pages/{page_id}"response = requests.patch(update_url, headers=headers, data=json.dumps(page_data))else:create_page_data = {"parent": {"database_id": database_id},**page_data}response = requests.post(f"{base_url}/pages", headers=headers, data=json.dumps(create_page_data))response.raise_for_status()print("Data synced to Notion successfully.")except requests.exceptions.HTTPError as errh:print("Http Error:", errh)except requests.exceptions.ConnectionError as errc:print("Error Connecting:", errc)except requests.exceptions.Timeout as errt:print("Timeout Error:", errt)except requests.exceptions.RequestException as err:print("Something went wrong", err)# Predefined dictionaries of streams to updatestreams = {"2B_100_batch": {"stream": "287b605243","notionDB": '402d60c9c1ef4980a3d85c5a4f4a0c97'},"2B_U100_batch": {"stream": "ebcfc50abe","notionDB": '00c3a26a119f487fa028c1ed404f22ba'}}# Function to run on button click@webhook_endpoint # Define a unique endpoint URLasync def update_streams(request: Request):# Read the request body as JSONpayload = await request.json()print(str(payload))# Accessing nested data in the 'event' objectspeckle_token = os.environ.get("SPECKLE_TOKEN")notion_token = os.environ.get("NOTION_TOKEN")#client = SpeckleClient(host="https://speckle.xyz")#client.authenticate_with_token(token=speckle_token)if 'payload' in payload and 'stream' in payload['payload']:stream_info = payload['payload']['stream']if 'name' in stream_info:stream_name = stream_info['name']print("update dat for: " + stream_name)# retrieve specific streamstream_id = streams[stream_name]["stream"]notion_db = streams[stream_name]["notionDB"]branch_data = extract_branch_info(stream_id, server_url="https://speckle.xyz/", token=speckle_token)sync_to_notion(notion_token, notion_db, branch_data)else:print("updating data for all streams")for key, value in streams.items():stream_id = value["stream"]notion_db = value["notionDB"]branch_data = extract_branch_info(stream_id, server_url="https://speckle.xyz/", token=speckle_token)sync_to_notion(notion_token, notion_db, branch_data)return "All streams updated successfully!""""# Create Gradio interfaceiface = gr.Interface(fn=update_streams,inputs=gr.components.Button(value="Update Streams"),outputs="text",)# Launch the appiface.launch()"""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051521 unmodified line5455565758596061626364656667686970717273747576# Speckle stream and authentication informationspeckle_token = os.getenv("SPECKLE_TOKEN")speckle_server_url = "https://speckle.xyz/"# WebSocket URIws_uri = "wss://onlinewebsocketserver.onrender.com"websocket = None# Connect to WebSocketasync def connect():global websockettry: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 websocketif websocket is None or websocket.closed:await connect()if websocket is None or websocket.closed:print("Failed to connect to WebSocket server.")returntry: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 = Nonedef 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.itemsif commits:latest_commit = commits[0]1 unmodified linebranch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}"branch_data["commit_message"] = latest_commit.messagebranch_data["author"] = latest_commit.authorNamebranch_info.append(branch_data)return branch_info@webhook_endpointasync 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 WebSocketbranch_data = extract_branch_info(stream_id, server_url=speckle_server_url, token=speckle_token)await send_data(branch_data)return "All streams updated successfully!"
app.py+42-255