-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
169 lines (149 loc) · 6.46 KB
/
bot.py
File metadata and controls
169 lines (149 loc) · 6.46 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
TODO: Creating a new account
1. Go to https://gmail.com/
2. Click on the "create account" drop down list and choose "for my personal use"
3. Fill the create new account form and click "next" button
4. On the next page, check if the "phone number" is optional. If the phone number is optional goto 4.1, else 4.2.
4.1. Fill the form and click "next" button
4.2. Leave this later
5. Agree the policy
"""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from time import sleep
from dotenv import load_dotenv
import os
import random
import string
from datetime import datetime
from utils import save_as_csv
load_dotenv()
"""
TODO: Login using existing account
1. Go to https://gmail.com/
2. Click on the "create account" drop down list and choose "for my personal use"
3. Fill the create new account form and click "next" button
4. On the next page, check if the "phone number" is optional. If the phone number is optional goto 4.1, else 4.2.
4.1. Fill the form and click "next" button
4.2. Leave this later
5. Agree the policy
"""
def generate_random_string(length=10, include_number:bool=False, include_symbol=False):
letters = string.ascii_letters
if include_number: letters += string.digits
if include_symbol: letters += "!@#$%^&*()_+=-"
return "".join(random.choice(letters) for _ in range(length))
class GmailBot:
def __init__(self) -> None:
self.driver = webdriver.Firefox()
self.driver.get("https://gmail.com")
def __del__(self) -> None:
# self.driver.close()
pass
def _check_default_language(self) -> bool:
try:
next_btn = self.driver.find_element(
By.XPATH, "//*[contains(text(), 'Next')]")
except NoSuchElementException:
next_btn = False
return bool(next_btn)
def _change_language_to_en(self):
# CLASS_NAME check the element by its classname
language_list_btn = self.driver.find_element(By.CLASS_NAME, "TquXA")
language_list_btn.click() # we could add the click() right after the line above]
for elem in self.driver.find_elements(By.XPATH, "//div[@data-value='en']"):
if "English (United States)" in elem.text: elem.click()
def _goto_signup_page(self):
sleep(3)
# XPATH checks the element by the screen, just like, ctrl + F
create_account_button = self.driver.find_element(
By.XPATH, "//*[contains(text(),'Create account')]")
create_account_button.click()
create_account_for_me_btn = self.driver.find_element(By.XPATH, "//*[contains(text(),'For my personal use')]") if self.driver.find_element(
By.XPATH, "//*[contains(text(),'For my personal use')]") else self.driver.find_element(By.XPATH, "//*[contains(text(),'For myself')]")
create_account_for_me_btn.click()
def _fill_sign_up_form(self, data:dict):
self.driver.find_element(By.ID, "firstName").send_keys(data['first_name'])
self.driver.find_element(By.ID, "lastName").send_keys(data['last_name'])
self.driver.find_element(By.ID, "username").send_keys(data['username'])
self.driver.find_element(By.NAME, 'Passwd').send_keys(data['password'])
self.driver.find_element(By.NAME, 'ConfirmPasswd').send_keys(data['password'])
def create_account(self, data: dict):
"""
If username is not given, it will generate username
If password is not given, it will generate password
"""
self._goto_signup_page()
if not self._check_default_language():
self._change_language_to_en()
if not data["username"]: data["username"] = generate_random_string()
if not data["password"]: data["password"] = generate_random_string(length=12, include_number=True, include_symbol=True)
self._fill_sign_up_form(data)
self._click_next_btn()
data["created_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.create_csv_file(data)
return data
def _click_next_btn(self):
self.driver.find_element(
By.XPATH, "//*[contains(text(), 'Next')]").click()
sleep(3)
def login(self, username: str, password: str, phone: int):
username_input = self.driver.find_element(By.ID, "identifierId")
username_input.clear()
username_input.send_keys(username)
self._click_next_btn()
password_input = self.driver.find_element(By.NAME, "Passwd")
password_input.send_keys(password)
self._click_next_btn()
'''try:
phone_num = self.driver.find_element(By.ID, "phoneNumberId")
except NoSuchElementException:
phone_num = False
if self.driver.find_element(By.ID, "phoneNumberId"):
phone_num = self.driver.find_element(By.ID, "phoneNumberId")
phone_num.send_keys(phone)
self._click_next_btn()'''
phone_num = self.driver.find_element(By.ID, "phoneNumberId")
phone_num.send_keys(phone)
self.driver.find_element(
By.XPATH, "//*[contains(text(), 'Next')]").click()
sleep(3)
def read_email(self):
"""completed"""
pass
#self.driver.find_element(By.ID, ":kn")
def create_csv_file(self, data:list|dict):
if type(data) != "list":
data = [data]
save_as_csv("users.csv",data)
return True
def send_email(self, message: str, title: str, to: str):
compose = self.driver.find_element(By.XPATH, "//*[contains(text(), 'Compose')]").click()
if compose == False:
self.login()
self.driver.find_element(By.XPATH, "//*[contains(text(), 'To')]").send_keys(to)
the_title = self.driver.find_element(By.NAME, "subjectbox").click()
the_title.send_keys(message)
the_message = self.driver.find_element(By.CLASS_NAME, "Am Al editable LW-avf tS-tW")
the_message.send_keys(title)
self.driver.find_element(By.XPATH, "//*[contains(text(), 'Send')]").click()
bot = GmailBot()
# user1 = bot.create_account({
# "first_name": "User",
# "last_name": "User",
# "username": None,
# "password": None
# })
# print(user1)
bot.login(
username = os.getenv("GMAIL_USERNAME"),
password = os.getenv("GMAIL_PASSWORD"),
phone = os.getenv("PHONE_NUMBER")
)
# bot.send_email(
# to = os.getenv("TO"),
# title = os.getenv("TITLE"),
# message = os.getenv("MESSAGE")
# )