78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Import suppliers from NocoDB into PocketBase"""
|
||
|
|
import requests, json, time, sys
|
||
|
|
|
||
|
|
PB_URL = "https://pocketbase-gnqc88suy8fqk6wyryjjdpvl.169.239.180.130.sslip.io"
|
||
|
|
NOCODB_API = "https://nocodb.apphub.co.za/api/v3/data/putj028icfsplze/md9msyn5qco5ttu/records"
|
||
|
|
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfcGJfdXNlcnNfYXV0aF8iLCJleHAiOjE3ODAyNjMwODcsImlkIjoiazA0NXlmZXplOTJja3g5IiwicmVmcmVzaGFibGUiOnRydWUsInR5cGUiOiJhdXRoIn0.4tUpls9uCAgcvwTvbWBrcl8_NLxgS__oaWZNRAEGsDo"
|
||
|
|
|
||
|
|
HEADERS = {"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"}
|
||
|
|
|
||
|
|
# Get existing supplier names
|
||
|
|
try:
|
||
|
|
existing = requests.get(f"{PB_URL}/api/collections/suppliers/records?perPage=500", headers=HEADERS).json()
|
||
|
|
existing_names = {r.get("suppliername", "").strip() for r in existing.get("items", []) if r.get("suppliername")}
|
||
|
|
except:
|
||
|
|
existing_names = set()
|
||
|
|
|
||
|
|
print(f"Existing suppliers in PocketBase: {len(existing_names)}")
|
||
|
|
|
||
|
|
created = 0
|
||
|
|
skipped = 0
|
||
|
|
|
||
|
|
# Use NocoDB MCP-style approach - manually construct records from known data
|
||
|
|
# We'll fetch via direct API with the auth header
|
||
|
|
page = 1
|
||
|
|
while True:
|
||
|
|
try:
|
||
|
|
resp = requests.get(
|
||
|
|
f"{NOCODB_API}?pageSize=100&page={page}&fields=suppliername,email,suppliernumber,supplieraddress,coordinates,closingtime,supply_tags",
|
||
|
|
headers={"xc-token": "PUT_TOKEN_HERE"} # NocoDB needs its own token
|
||
|
|
)
|
||
|
|
if resp.status_code != 200:
|
||
|
|
print(f"Page {page}: HTTP {resp.status_code} — stopping")
|
||
|
|
break
|
||
|
|
|
||
|
|
data = resp.json()
|
||
|
|
records = data.get("records", [])
|
||
|
|
if not records:
|
||
|
|
break
|
||
|
|
|
||
|
|
for r in records:
|
||
|
|
f = r["fields"]
|
||
|
|
name = (f.get("suppliername") or "").strip()
|
||
|
|
if not name or name in existing_names:
|
||
|
|
skipped += 1
|
||
|
|
continue
|
||
|
|
|
||
|
|
payload = {"suppliername": name}
|
||
|
|
for k in ["supplieraddress", "coordinates", "email", "suppliernumber", "closingtime", "supply_tags"]:
|
||
|
|
v = f.get(k)
|
||
|
|
if v and str(v).strip():
|
||
|
|
payload[k] = str(v).strip()
|
||
|
|
|
||
|
|
if len(payload) <= 1:
|
||
|
|
skipped += 1
|
||
|
|
continue
|
||
|
|
|
||
|
|
create = requests.post(f"{PB_URL}/api/collections/suppliers/records", headers=HEADERS, json=payload)
|
||
|
|
if create.status_code == 200:
|
||
|
|
created += 1
|
||
|
|
existing_names.add(name)
|
||
|
|
else:
|
||
|
|
skipped += 1
|
||
|
|
|
||
|
|
time.sleep(0.05)
|
||
|
|
|
||
|
|
print(f"Page {page}: processed {len(records)} records ({created} created, {skipped} skipped so far)")
|
||
|
|
page += 1
|
||
|
|
|
||
|
|
if not data.get("next"):
|
||
|
|
break
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error: {e}")
|
||
|
|
break
|
||
|
|
|
||
|
|
print(f"\nDone! Created: {created}, Skipped (existing/no data): {skipped}")
|