Spaces:
Runtime error
Runtime error
File size: 5,772 Bytes
2b2653a a552ede 2b2653a 4249ced 8fe992b 2b2653a 9b5b26a 2b2653a a552ede 9b5b26a 2b2653a 9b5b26a 2b2653a 9b5b26a 6d70deb 2b2653a 6d70deb 2b2653a a552ede 6d70deb 2b2653a 6d70deb a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a a552ede 2b2653a 6d70deb a552ede 2b2653a 6d70deb 2b2653a 6d70deb 2b2653a 6d70deb 2b2653a 2cdab3c 2b2653a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
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.")
@tool
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}")
|