Ajustes para unificar dependencia de current_url.
This commit is contained in:
@@ -4,6 +4,7 @@ from googleapiclient.discovery import build
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import re
|
||||
import base64
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
@@ -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)
|
||||
@@ -99,7 +124,17 @@ drive_service, gmail_service = get_drive_and_gmail_services(
|
||||
CREDENTIALS_PATH, IMPERSONATED_USER)
|
||||
|
||||
# Id del documento de Google Drive a enviar por email
|
||||
DOC_ID = '{gdoc_id}'
|
||||
CURRENT_URL = rb_get_var('current_url')
|
||||
DOC_ID = extract_doc_id_from_url(CURRENT_URL)
|
||||
if not DOC_ID:
|
||||
raise RuntimeError('No pude extraer documentId desde current_url: ' + CURRENT_URL)
|
||||
|
||||
try:
|
||||
SetVar('gdoc_id', DOC_ID)
|
||||
SetVar('gdoc_url_used', CURRENT_URL)
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
print(DOC_ID)
|
||||
|
||||
# Obtener nombre del documento de Google
|
||||
@@ -131,4 +166,4 @@ pdf_data = export_doc_as_pdf(drive_service, DOC_ID)
|
||||
result = send_email(gmail_service, to_emails, SUBJECT,
|
||||
BODY_TEXT, pdf_data, pdf_filename)
|
||||
print('Mensaje enviado. ID:', result.get('id'))
|
||||
SetVar('res_envio', True)
|
||||
SetVar('res_envio', True)
|
||||
|
||||
Reference in New Issue
Block a user