Spaces:
Runtime error
Runtime error
| import time | |
| import os | |
| from selenium.webdriver.common.by import By | |
| from selenium.webdriver.common.keys import Keys | |
| from selenium.webdriver.support.ui import WebDriverWait | |
| from selenium.webdriver.support import expected_conditions as EC | |
| # Assuming you have SmolAgent defined or imported somewhere like this: | |
| # from your_smol_agent_module import SmolAgent | |
| # And assuming 'tool' is from langchain: | |
| from langchain.tools import tool | |
| # --- Configuration (Consider environment variables for production) --- | |
| USERNAME = os.getenv("SUBSTACK_USERNAME", "pradeepp@rocketmail.com") # Replace default or set env var | |
| PASSWORD = os.getenv("SUBSTACK_PASSWORD", "Y2ktest1") # Replace default or set env var | |
| if USERNAME == "your_email@example.com" or PASSWORD == "your_password": | |
| print("WARNING: Using default USERNAME/PASSWORD. Please set SUBSTACK_USERNAME and SUBSTACK_PASSWORD environment variables for production.") | |
| def my_custom_tool(arg1:str, arg2:str)-> str: | |
| """A tool that automates logging into Substack and creating a post. | |
| Args: | |
| arg1: A placeholder argument. | |
| arg2: Another placeholder argument. | |
| """ | |
| # Define the agent | |
| agent = SmolAgent( | |
| browser="chrome", # Uses Chrome WebDriver | |
| headless=False, # Set to True to run in background (required for server environments) | |
| maximize=True, | |
| ) | |
| # Get the underlying Selenium WebDriver instance for explicit waits if SmolAgent doesn't provide them | |
| driver = agent.driver if hasattr(agent, 'driver') else None | |
| if not driver: | |
| print("WARNING: SmolAgent instance does not expose a 'driver' attribute for explicit waits.") | |
| try: | |
| print("🌐 Opening Substack login page...") | |
| agent.goto("https://tensorhealth.substack.com/") | |
| # Wait for "Sign in" link and click it | |
| print("🔗 Clicking 'Sign in'...") | |
| if driver: | |
| WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sign in"))).click() | |
| else: | |
| agent.click_text("Sign in") | |
| time.sleep(3) # Fallback if no explicit wait available | |
| # Enter Email/Username | |
| print("📧 Entering email...") | |
| email_field = None | |
| if driver: | |
| email_field = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "email"))) | |
| if email_field: | |
| email_field.send_keys(USERNAME) | |
| email_field.send_keys(Keys.RETURN) | |
| else: | |
| agent.type(USERNAME, By.NAME, "email") | |
| agent.press(Keys.RETURN) | |
| time.sleep(3) # Fallback | |
| # Enter Password | |
| print("🔑 Entering password...") | |
| password_field = None | |
| if driver: | |
| password_field = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "password"))) | |
| if password_field: | |
| password_field.send_keys(PASSWORD) | |
| password_field.send_keys(Keys.RETURN) | |
| else: | |
| agent.type(PASSWORD, By.NAME, "password") | |
| agent.press(Keys.RETURN) | |
| time.sleep(5) # Fallback | |
| # Open 'Dashboard' dropdown (assuming it appears after login) | |
| print("📊 Opening 'Dashboard'...") | |
| if driver: | |
| WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Dashboard"))).click() | |
| else: | |
| agent.click_text("Dashboard") | |
| time.sleep(5) # Fallback | |
| # Select 'TensorHealth' | |
| print("📈 Selecting 'TensorHealth'...") | |
| if driver: | |
| WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "TensorHealth"))).click() | |
| else: | |
| agent.click_text("TensorHealth") | |
| time.sleep(2) # Fallback | |
| # Click 'New Post' dropdown | |
| print("📝 Clicking 'New Post'...") | |
| if driver: | |
| WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "New Post"))).click() | |
| else: | |
| agent.click_text("New Post") | |
| time.sleep(5) # Fallback | |
| # Select 'Text Post' | |
| print("📄 Selecting 'Text Post'...") | |
| if driver: | |
| WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Text Post"))).click() | |
| else: | |
| agent.click_text("Text Post") | |
| time.sleep(2) # Fallback | |
| # Paste "Opinion AI" into contenteditable div | |
| print("✍️ Pasting 'Opinion AI'...") | |
| text_area = None | |
| if driver: | |
| text_area = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[@contenteditable='true']"))) | |
| if text_area: | |
| text_area.send_keys("Opinion AI") | |
| else: | |
| agent.type("Opinion AI", By.XPATH, "//div[@contenteditable='true']") | |
| print("✅ Text successfully entered!") | |
| return "Text successfully entered into Substack post!" | |
| except Exception as e: | |
| print(f"❌ Error occurred: {e}") | |
| return f"Failed to perform Substack automation: {e}" | |
| finally: | |
| # Keep browser open for review (only if headless=False) | |
| if not agent.headless: | |
| print("Browser will close in 10 seconds...") | |
| time.sleep(10) | |
| print("🧹 Quitting browser agent.") | |
| agent.quit() | |
| # Example of how you might call this tool (if not directly invoked by an agent framework) | |
| if __name__ == "__main__": | |
| # # This part would typically be handled by your LangChain agent's invocation | |
| # # For standalone testing: | |
| # # Make sure you have the environment variables set for USERNAME and PASSWORD | |
| # # or temporarily define them in this script for testing. | |
| result = my_custom_tool("dummy_arg1", "dummy_arg2") | |
| print(f"Tool execution result: {result}") | |