Ajustes para unificar dependencia de current_url.
This commit is contained in:
@@ -3,6 +3,7 @@ from googleapiclient.discovery import build
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import re
|
||||
|
||||
global Request, InstalledAppFlow, Credentials, service_account, build, os, sys, json, LISTA_BUSCAR_REEMPLAZAR, base_dir, libs_dir, SCOPES, CREDENTIALS_PATH, TOKEN_PATH, _load_json, get_google_services, replace_vars_doc, drive_service, docs_service, gdoc_id, lista_oficial_reemplazar, success
|
||||
|
||||
@@ -26,6 +27,30 @@ CREDENTIALS_PATH = '{gdoc_sa_json2}'
|
||||
IMPERSONATED_USER = '{gdoc_impersonated_user}'
|
||||
|
||||
|
||||
def rb_get_var(name, required=True, default=''):
|
||||
try:
|
||||
value = GetVar(name)
|
||||
except NameError:
|
||||
value = os.environ.get(name, default)
|
||||
|
||||
if value is None:
|
||||
value = ''
|
||||
|
||||
value = str(value).strip()
|
||||
if value == f'{{{name}}}':
|
||||
value = ''
|
||||
|
||||
if required and not value:
|
||||
raise RuntimeError(f'La variable Rocketbot "{name}" está vacía o no fue leída correctamente.')
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def extract_doc_id_from_url(url):
|
||||
match = re.search(r"/document/d/([a-zA-Z0-9_-]+)", url or "")
|
||||
return match.group(1) if match else ""
|
||||
|
||||
|
||||
def _load_json(path):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
@@ -92,11 +117,20 @@ def replace_vars_doc(docs_service, gdoc_id, replacements_values):
|
||||
drive_service, docs_service = get_google_services(CREDENTIALS_PATH, IMPERSONATED_USER)
|
||||
|
||||
# Variables de entrada (esto usualmente viene de Rocketbot via GetVar)
|
||||
gdoc_id = '{gdoc_id}'
|
||||
current_url = rb_get_var('current_url')
|
||||
gdoc_id = extract_doc_id_from_url(current_url)
|
||||
if not gdoc_id:
|
||||
raise RuntimeError('No pude extraer documentId desde current_url: ' + current_url)
|
||||
lista_oficial_reemplazar = {lista_oficial_reemplazar}
|
||||
|
||||
|
||||
try:
|
||||
try:
|
||||
SetVar('gdoc_id', gdoc_id)
|
||||
SetVar('gdoc_url_used', current_url)
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
# Ejecutar reemplazo si se cuenta con la información
|
||||
if gdoc_id and lista_oficial_reemplazar:
|
||||
success = replace_vars_doc(docs_service, gdoc_id, lista_oficial_reemplazar)
|
||||
@@ -114,4 +148,4 @@ except Exception as e:
|
||||
try:
|
||||
SetVar('error', {'error': 'Error al remplazar las variables en Google Docs', 'descripcion': str(e)})
|
||||
except NameError:
|
||||
pass
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user