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
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!pip install -q specklepy
|
2 |
+
|
3 |
+
|
4 |
+
# lone github repro & add to sys paths
|
5 |
+
import gradio as gr
|
6 |
+
import sys
|
7 |
+
import time
|
8 |
+
import os
|
9 |
+
# delete (if it already exists) , clone repro
|
10 |
+
#!rm -rf RECODE_speckle_utils
|
11 |
+
#!git clone https://github.com/SerjoschDuering/RECODE_speckle_utils
|
12 |
+
#sys.path.append('/content/RECODE_speckle_utils')
|
13 |
+
|
14 |
+
# import from repro
|
15 |
+
"""
|
16 |
+
import speckle_utils
|
17 |
+
import data_utils
|
18 |
+
import plots_utils
|
19 |
+
import color_maps
|
20 |
+
"""
|
21 |
+
import requests
|
22 |
+
|
23 |
+
from specklepy.api.client import SpeckleClient
|
24 |
+
from specklepy.api.credentials import get_default_account, get_local_accounts
|
25 |
+
from specklepy.transports.server import ServerTransport
|
26 |
+
from specklepy.api import operations
|
27 |
+
from specklepy.objects.geometry import Polyline, Point
|
28 |
+
import specklepy
|
29 |
+
|
30 |
+
import pandas as pd
|
31 |
+
import requests
|
32 |
+
import datetime
|
33 |
+
import json
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
def extract_branch_info(stream_id, server_url=None, token=None):
|
41 |
+
if server_url and token:
|
42 |
+
client = SpeckleClient(host=server_url)
|
43 |
+
client.authenticate_with_token(token=token)
|
44 |
+
else:
|
45 |
+
account = get_default_account()
|
46 |
+
client = SpeckleClient(host=account.serverUrl)
|
47 |
+
client.authenticate(token=account.token)
|
48 |
+
|
49 |
+
branches = client.branch.list(stream_id, 100)
|
50 |
+
|
51 |
+
branch_info = []
|
52 |
+
|
53 |
+
for branch in branches:
|
54 |
+
branch_data = {
|
55 |
+
"Name": branch.name,
|
56 |
+
"description": branch.description,
|
57 |
+
"url": f"{server_url}/streams/{stream_id}/branches/{branch.name.replace('/', '%2F').replace('+', '%2B')}"
|
58 |
+
|
59 |
+
}
|
60 |
+
|
61 |
+
# Determine sub-variant
|
62 |
+
#if branch.name.startswith(("template_geometry", "graph_geometry")):
|
63 |
+
if '+' in branch.name:
|
64 |
+
slash_index = branch.name.rfind('/')
|
65 |
+
plus_index = branch.name.find('+', slash_index)
|
66 |
+
sub_variant = branch.name[slash_index + 1:plus_index]
|
67 |
+
else:
|
68 |
+
sub_variant = "default"
|
69 |
+
branch_data["sub-variant"] = sub_variant
|
70 |
+
|
71 |
+
# Get commits for the branch
|
72 |
+
|
73 |
+
#try:
|
74 |
+
commits = client.branch.get(stream_id, branch.name).commits.items
|
75 |
+
if commits:
|
76 |
+
latest_commit = commits[0]
|
77 |
+
branch_data["updated"] = latest_commit.createdAt
|
78 |
+
branch_data["commit_url"] = f"{server_url}/streams/{stream_id}/commits/{latest_commit.id}"
|
79 |
+
branch_data["commit_message"] = latest_commit.message
|
80 |
+
branch_data["author"] = latest_commit.authorName
|
81 |
+
#except Exception as e:
|
82 |
+
# print(f"Error fetching commits for branch {branch.name}: {str(e)}")
|
83 |
+
|
84 |
+
branch_info.append(branch_data)
|
85 |
+
|
86 |
+
return branch_info
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
def sync_to_notion(token, database_id, branch_data):
|
91 |
+
base_url = "https://api.notion.com/v1"
|
92 |
+
headers = {
|
93 |
+
"Authorization": f"Bearer {token}",
|
94 |
+
"Notion-Version": "2022-06-28",
|
95 |
+
"Content-Type": "application/json"
|
96 |
+
}
|
97 |
+
|
98 |
+
try:
|
99 |
+
# Fetch existing data from Notion database
|
100 |
+
response = requests.post(f"{base_url}/databases/{database_id}/query", headers=headers)
|
101 |
+
response.raise_for_status()
|
102 |
+
pages = response.json().get('results', [])
|
103 |
+
|
104 |
+
# Create a dictionary for the latest update time of each branch
|
105 |
+
latest_updates = {branch['Name']: branch.get('updated', '') for branch in branch_data}
|
106 |
+
|
107 |
+
# Status color mapping
|
108 |
+
status_colors = {
|
109 |
+
"outdated": "red",
|
110 |
+
"up-to-date": "green",
|
111 |
+
"empty": "gray"
|
112 |
+
}
|
113 |
+
|
114 |
+
# Process each branch and update or create Notion rows
|
115 |
+
for branch in branch_data:
|
116 |
+
# Find the corresponding page in Notion
|
117 |
+
page_id = None
|
118 |
+
existing_depends_on = []
|
119 |
+
for page in pages:
|
120 |
+
notion_name = page['properties']['Name']['title'][0]['text']['content'] if page['properties']['Name']['title'] else ''
|
121 |
+
if notion_name == branch['Name']:
|
122 |
+
page_id = page['id']
|
123 |
+
# Retain the existing value of "depends-on"
|
124 |
+
existing_depends_on = [dep['name'] for dep in page['properties'].get('depends-on', {}).get('multi_select', [])]
|
125 |
+
break
|
126 |
+
|
127 |
+
# Determine status based on dependencies
|
128 |
+
latest_update = latest_updates.get(branch['Name'], '')
|
129 |
+
outdated = False
|
130 |
+
if existing_depends_on:
|
131 |
+
for dep_name in existing_depends_on:
|
132 |
+
if dep_name in latest_updates and latest_updates[dep_name] > latest_update:
|
133 |
+
outdated = True
|
134 |
+
break
|
135 |
+
if not latest_update:
|
136 |
+
status_tag = "empty"
|
137 |
+
elif outdated:
|
138 |
+
status_tag = "outdated"
|
139 |
+
else:
|
140 |
+
status_tag = "up-to-date"
|
141 |
+
|
142 |
+
# Prepare data for updating or creating a page
|
143 |
+
print (branch.get("url", ""))
|
144 |
+
updated_value = branch.get("updated")
|
145 |
+
if isinstance(updated_value, datetime):
|
146 |
+
updated_isoformat = updated_value.isoformat()
|
147 |
+
else:
|
148 |
+
updated_isoformat = datetime.now().isoformat()
|
149 |
+
|
150 |
+
page_data = {
|
151 |
+
"properties": {
|
152 |
+
"Name": {"title": [{"text": {"content": branch['Name']}}]},
|
153 |
+
"status-tag": {"select": {"name": status_tag}}, # Assuming 'select' type
|
154 |
+
"status": {"rich_text": [{"text": {"content": "Status: " + status_tag}}]},
|
155 |
+
"url": {"url": branch.get("url", "")},
|
156 |
+
"updated": {"date": {"start": updated_isoformat}},
|
157 |
+
"description": {"rich_text": [{"text": {"content": str(branch.get("description", ""))}}]},
|
158 |
+
"sub-variant": {"rich_text": [{"text": {"content": branch.get("sub-variant", "")}}]},
|
159 |
+
"author": {"rich_text": [{"text": {"content": branch.get("author", "")}}]},
|
160 |
+
"commit_message": {"rich_text": [{"text": {"content": branch.get("commit_message", "")}}]},
|
161 |
+
"depends-on": {"multi_select": [{"name": d} for d in existing_depends_on]}
|
162 |
+
}
|
163 |
+
}
|
164 |
+
|
165 |
+
# Update an existing page or create a new one
|
166 |
+
if page_id:
|
167 |
+
update_url = f"{base_url}/pages/{page_id}"
|
168 |
+
response = requests.patch(update_url, headers=headers, data=json.dumps(page_data))
|
169 |
+
else:
|
170 |
+
create_page_data = {
|
171 |
+
"parent": {"database_id": database_id},
|
172 |
+
**page_data
|
173 |
+
}
|
174 |
+
response = requests.post(f"{base_url}/pages", headers=headers, data=json.dumps(create_page_data))
|
175 |
+
|
176 |
+
response.raise_for_status()
|
177 |
+
|
178 |
+
print("Data synced to Notion successfully.")
|
179 |
+
|
180 |
+
except requests.exceptions.HTTPError as errh:
|
181 |
+
print("Http Error:", errh)
|
182 |
+
except requests.exceptions.ConnectionError as errc:
|
183 |
+
print("Error Connecting:", errc)
|
184 |
+
except requests.exceptions.Timeout as errt:
|
185 |
+
print("Timeout Error:", errt)
|
186 |
+
except requests.exceptions.RequestException as err:
|
187 |
+
print("Something went wrong", err)
|
188 |
+
|
189 |
+
|
190 |
+
# Wrapper function for the Gradio interface
|
191 |
+
def run_sync():
|
192 |
+
# Define your token, database_id, and branch_data
|
193 |
+
token = "your_token"
|
194 |
+
database_id = "your_database_id"
|
195 |
+
branch_data = extract_branch_info(stream_id, server_url=None, token=None)
|
196 |
+
|
197 |
+
# Call your function
|
198 |
+
sync_to_notion(token, database_id, branch_data)
|
199 |
+
|
200 |
+
return "Sync completed successfully!"
|
201 |
+
|
202 |
+
# branch_data = extract_branch_info("287b605243", server_url="https://speckle.xyz/", token="52566d1047b881764e16ad238356abeb2fc35d8b42")
|
203 |
+
# sync_to_notion("secret_V2olY3GMsTLaC910Ym20xT9RnBfoDdrAwXb1ayBupXI", "402d60c9c1ef4980a3d85c5a4f4a0c97", branch_data)
|
204 |
+
|
205 |
+
# Predefined dictionaries of streams to update
|
206 |
+
streams = {
|
207 |
+
"2B_100": {
|
208 |
+
"stream": "https://speckle.xyz/streams/287b605243",
|
209 |
+
"notionDB": '402d60c9c1ef4980a3d85c5a4f4a0c97'
|
210 |
+
},
|
211 |
+
"2B_U100": {
|
212 |
+
"stream": "https://speckle.xyz/streams/ebcfc50abe",
|
213 |
+
"notionDB": '00c3a26a119f487fa028c1ed404f22ba'
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
# Function to run on button click
|
218 |
+
def update_streams():
|
219 |
+
speckle_token = os.environ.get("SPECKLE_TOKEN")
|
220 |
+
notion_token = os.environ.get("NOTION_TOKEN")
|
221 |
+
|
222 |
+
client = SpeckleClient(host="https://speckle.xyz/")
|
223 |
+
client.authenticate_with_token(token=speckle_token)
|
224 |
+
|
225 |
+
for key, value in streams.items():
|
226 |
+
stream_url = value["stream"]
|
227 |
+
notion_db = value["notionDB"]
|
228 |
+
stream_id = stream_url.split('/')[-1]
|
229 |
+
|
230 |
+
branch_data = extract_branch_info(stream_id, server_url=stream_url, token=speckle_token)
|
231 |
+
sync_to_notion(notion_token, notion_db, branch_data)
|
232 |
+
|
233 |
+
return "All streams updated successfully!"
|
234 |
+
|
235 |
+
# Create Gradio interface
|
236 |
+
iface = gr.Interface(
|
237 |
+
fn=update_streams,
|
238 |
+
inputs=gr.inputs.Button(label="Update Streams"),
|
239 |
+
outputs="text"
|
240 |
+
)
|
241 |
+
|
242 |
+
# Launch the app
|
243 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
specklepy
|
3 |
+
requests
|