forked from tonikolaba/microsoft-rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing_bot.py
145 lines (127 loc) Β· 5.18 KB
/
bing_bot.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
from driver_manager import WebDriverConf
from wonderwords import RandomWord
from config.config import BROWSER, DEVICE, EMAIL, PASSWORD , LOGIN_URL, BING_URL, RANDOMWORDS_API_URL
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests, re, time
class BingSearchBot:
def __init__(self, email, password, login_url, bing_url):
self.email = email
self.password = password
self.login_url = login_url
self.bing_url = bing_url
self.randomwords_api_url = randomwords_api_url
self.browser = None
def login_to_microsoft_account(self):
try:
self.browser.get(self.login_url)
# Find and input email
try:
email_input = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, "i0116"))
)
email_input.send_keys(self.email)
email_input.click()
# Wait for login button to become clickable
login_button = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.ID, "idSIButton9"))
)
# Click login button
login_button.click()
except Exception as e:
print(f"Error logging in: {e}")
return
#Find and input password
'''
try:
password_input = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.ID, "i0118"))
)
password_input.send_keys(self.password)
password_input.click()
# Wait for login button to become clickable
login_button = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.ID, "idSIButton9"))
)
# Click login button
login_button.click()
except Exception as e:
print(f"Error logging in: {e}")
return
'''
# Choose other way, not MFA
try:
login_button = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.ID, "idA_PWD_SwitchToCredPicker"))
)
login_button.click()
except Exception as e:
print(f"Error choosing other way: {e}")
return
try:
elements = WebDriverWait(self.browser, 10).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".___b9iavz0.f10pi13n.f17n1hoa"))
)
elements[0].click()
except Exception as e:
print(f"Error choosing other way: {e}")
return
print("Please Enter your localy password MANUALY??? ")
time.sleep(10)
print("Stay singed in? ")
# O = id: declineButton 1 = id : acceptButton
try:
login_button = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.ID, "acceptButton"))
)
login_button.click()
except Exception as e:
print(f"Error staying signed in: {e}")
return
except Exception as e:
print(f"Error logging in: {e}")
# Open Bing
self.browser.get(self.bing_url)
def search_on_bing(self):
print("Start Search on Bing automatically")
# Define a function to get random words
get_random_words = RandomWord()
for i in range(90):
# Get random words
random_words = get_random_words.random_words(3)
search_word = ""
for word in random_words:
# Add regex pattern to each word
regex_word = re.sub(r'([.*+?^${}()|\[\]\/\\])', r'\\\1', word)
search_word += regex_word + " "
# Find the search input field
try:
search_input = WebDriverWait(self.browser, 10).until(
EC.presence_of_element_located((By.NAME, "q"))
)
# Clear the search input field
search_input.clear()
# Enter the random word to search for
search_input.send_keys(search_word)
# Submit the search query
search_input.submit()
# Wait for 2 seconds before the next search
time.sleep(7)
except Exception as e:
print(f"Error searching: {e}")
def run(self):
wd = WebDriverConf(BROWSER, DEVICE)
self.browser = wd.get_driver()
self.login_to_microsoft_account()
self.search_on_bing()
self.browser.quit
if __name__ == "__main__":
# Load environment variables from .env file
email = EMAIL
password = PASSWORD
login_url = LOGIN_URL
bing_url = BING_URL
randomwords_api_url = RANDOMWORDS_API_URL
bing_bot = BingSearchBot(email, password, login_url, bing_url)
bing_bot.run()