-
Notifications
You must be signed in to change notification settings - Fork 0
/
virgool.py
149 lines (117 loc) · 5.04 KB
/
virgool.py
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
142
143
144
145
146
147
148
149
import os
import time
from getpass import getpass
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def login_to_virgool(email, password):
# Set up the web driver (in this example, we'll use Chrome)
driver = webdriver.Chrome()
# Navigate to Virgool.io
driver.get('https://virgool.io')
# Wait for the sign-in button to be clickable
sign_in_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '//a[@class="btn-text" and @href="https://virgool.io/login" and @title="ورود به حساب کاربری"]'))
)
# Click the sign-in button
sign_in_button.click()
# Find the email input field and enter the login email
email_input = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//input[@name="userInput"]'))
)
email_input.send_keys(email)
# Find the "Next" button and click it
next_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '//button[@type="submit" and contains(.,"ورود به حساب کاربری")]'))
)
next_button.click()
# Find the password input field and enter the password
password_input = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//input[@name="password"]'))
)
password_input.send_keys(password)
# Find the sign-in button and click it
sign_in_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '//button[@type="submit" and contains(.,"ورود به حساب کاربری")]'))
)
sign_in_button.click()
# Wait for the login process to complete
try:
WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//a[contains(@href, "/@")]'))
)
print('Login successful!')
except:
print('Login failed.')
driver.quit() # Quit the driver if login fails
return None
# Return the driver instance
return driver
def create_new_post(driver, title, text):
# Find the new post button and click it
new_post_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/section/header/section/section/div[2]/section/div[1]/div/a'))
)
new_post_button.click()
print('Creating a new post...')
#input title
title_input = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="react-app"]/section/div[1]/div[1]/input'))
)
title_input.send_keys(title)
#input text
text_input = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="react-app"]/section/div[1]/div[2]/div/div[1]/div[2]/div/div/div/div'))
)
text_input.send_keys(text)
# simple text to use as an input:
# The quick brown fox jumps over the lazy dog.
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.
# Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit.
# Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
# Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
publish_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="btn-publish"]'))
)
publish_button.click()
time.sleep(2)
exit_button = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="react-app"]/section/div[2]/div/div'))
)
exit_button.click()
time.sleep(2)
def home(driver):
home_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > section > header > section > section > div.container > section > div:nth-child(1) > a > svg'))
)
# Click the home button
home_button.click()
print("Successfully back to home")
time.sleep(5)
# Retrieve the email & password from the environment variable
virgool_email = os.environ.get('VIRGOOL_EMAIL')
virgool_password = os.environ.get('VIRGOOL_PASSWORD')
title = os.environ.get('VIRGOOL_TITLE')
text = os.environ.get('VIRGOOL_TEXT')
# If the environment variable is not set, prompt the user to enter the email & password
if not virgool_email:
virgool_email = input('Enter your Virgool.io email: ')
if not virgool_password:
virgool_password = getpass('Enter your Virgool.io password: ')
#mr4^2WZ7I1GDi8
if not title:
title = input('Enter a title: ')
if not text:
text = input('Enter your text: ')
# Login to Virgool.io
driver = login_to_virgool(virgool_email, virgool_password)
# Check if login was successful before proceeding
if driver is not None:
# Call the create_new_post method
create_new_post(driver, title, text)
if driver is not None:
home(driver)
# Close the browser
driver.quit()