76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
import urllib.request
|
|
import json
|
|
import ssl
|
|
import sys
|
|
import io
|
|
|
|
if sys.platform == 'win32':
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
|
|
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
|
|
|
|
ctx = ssl.create_default_context()
|
|
ctx.check_hostname = False
|
|
ctx.verify_mode = ssl.CERT_NONE
|
|
|
|
APPWRITE_ENDPOINT = "https://appwrite.almeira.com.co/v1"
|
|
PROJECT_ID = "69b84df5003bd8957c5c"
|
|
DATABASE_ID = "sneakers_multistore_db"
|
|
COLLECTION_ID = "products"
|
|
API_KEY = "standard_cce3cdb866ea6433888d40cedd24dc45987372308fdc3dd0697de08648c0e5c7e04b02c3bcee271403fd656b00a5820f04c08b51631284a6e8a3582d8aed2aa33234f3c29cc777e9b162eb54810b789251b0d006a9a3b081ebc2da53b9baa94614b124b3508ad849fd9a4187cc6094f506d4b57f409b69d9562a74df594f3ec3"
|
|
|
|
headers = {
|
|
"X-Appwrite-Project": PROJECT_ID,
|
|
"X-Appwrite-Key": API_KEY,
|
|
"Content-Type": "application/json",
|
|
}
|
|
|
|
print("1. Obteniendo productos de Appwrite...")
|
|
url = f"{APPWRITE_ENDPOINT}/databases/{DATABASE_ID}/collections/{COLLECTION_ID}/documents?limit=100"
|
|
req = urllib.request.Request(url, headers=headers)
|
|
|
|
with urllib.request.urlopen(req, context=ctx, timeout=20) as res:
|
|
data = json.loads(res.read().decode('utf-8'))
|
|
docs = data.get('documents', [])
|
|
print(f" -> Total documentos encontrados: {len(docs)}")
|
|
|
|
def resolve_collection(doc):
|
|
name = (doc.get('name') or '').lower()
|
|
cat = (doc.get('category') or '').lower()
|
|
desc = (doc.get('description') or '').lower()
|
|
full = f"{name} {cat} {desc}"
|
|
|
|
if any(w in full for w in ['sandali', 'chancl', 'slide', 'croc', 'pantufl', 'ojot', 'adilette']):
|
|
return 'Sandalias'
|
|
if any(w in full for w in ['gorra', 'cap', 'snapback', 'bucket', 'sombrero']):
|
|
return 'Gorras'
|
|
if any(w in full for w in ['bolso', 'cartera', 'tote', 'handbag']):
|
|
return 'Bolsos'
|
|
if any(w in full for w in ['carriel', 'bandolera', 'riñonera', 'rinonera', 'pechera', 'crossbody', 'canguro']):
|
|
return 'Carrieles'
|
|
if any(w in full for w in ['malet', 'morral', 'mochila', 'backpack', 'tula']):
|
|
return 'Maletines'
|
|
if any(w in full for w in ['camiseta', 'buzo', 'hoodie', 'chaqueta', 'pantalon', 'short', 'ropa', 'streetwear']):
|
|
return 'Ropa'
|
|
if any(w in full for w in ['billetera', 'cinturon', 'correa', 'reloj', 'gafas']):
|
|
return 'Accesorios'
|
|
return 'Zapatillas'
|
|
|
|
updated_count = 0
|
|
for doc in docs:
|
|
doc_id = doc['$id']
|
|
current_cat = doc.get('category') or ''
|
|
target_cat = resolve_collection(doc)
|
|
|
|
if current_cat != target_cat:
|
|
update_url = f"{APPWRITE_ENDPOINT}/databases/{DATABASE_ID}/collections/{COLLECTION_ID}/documents/{doc_id}"
|
|
payload = json.dumps({"data": {"category": target_cat}}).encode('utf-8')
|
|
update_req = urllib.request.Request(update_url, data=payload, headers=headers, method='PATCH')
|
|
try:
|
|
with urllib.request.urlopen(update_req, context=ctx, timeout=10) as patch_res:
|
|
print(f" [OK] {doc.get('name')[:35]}: '{current_cat}' -> '{target_cat}'")
|
|
updated_count += 1
|
|
except Exception as e:
|
|
print(f" [ERROR en {doc_id}]: {e}")
|
|
|
|
print(f"\n✅ Total productos actualizados en Appwrite: {updated_count}")
|