disenosla2pc/generador-l2pc
0
1import re2import os3 4db_path = r'app/database.py'5with open(db_path, 'r', encoding='utf-8') as f:6 text = f.read()7 8# 1. Nuevas funciones para manejar el Token de Usuario en Drive9new_auth_logic = r'''10def _get_drive_credentials():11 """Obtiene credenciales de Usuario (OAuth2) para Drive si el token existe, 12 de lo contrario cae al Service Account."""13 token_path = os.path.join('config', 'token_drive.json')14 scopes = ["https://www.googleapis.com/auth/drive"]15 16 if os.path.exists(token_path):17 try:18 from google.oauth2.credentials import Credentials19 from google.auth.transport.requests import Request20 creds = Credentials.from_authorized_user_file(token_path, scopes)21 if creds and creds.expired and creds.refresh_token:22 creds.refresh(Request())23 logging.info("Credenciales de DRIVE cargadas desde Token de Usuario (OAuth2).")24 return creds25 except Exception as e:26 logging.error(f"Error cargando token_drive.json: {e}")27 28 # Fallback al service account29 return _get_google_credentials()30 31def _get_drive_service():32 creds = _get_drive_credentials()33 if creds:34 from googleapiclient.discovery import build35 return build('drive', 'v3', credentials=creds)36 return None37'''38 39if '_get_drive_credentials' not in text:40 text = text.replace('def _get_google_credentials():', new_auth_logic + '\ndef _get_google_credentials():')41 42# 2. Actualizar las funciones de Drive para usar el nuevo servicio43# Reemplazamos las instanciaciones manuales dentro de las funciones (subir_imagen_drive_v2, etc.)44text = text.replace("drive_service = build('drive', 'v3', credentials=creds)", "drive_service = _get_drive_service()")45 46with open(db_path, 'w', encoding='utf-8') as f:47 f.write(text)48print("SUCCESS: database.py updated for OAuth2 support.")49 