-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
305 lines (250 loc) · 15.2 KB
/
Main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import mysql.connector
from selenium.common.exceptions import NoSuchElementException
from Sign_In_Notifications import *
from mySQL_ProductLinks import *
from Identify_Post_and_Country import *
from Return_Commands import *
from Command_Type import *
from Product_Specifications import *
from Maintenance_and_Service_Guide import *
from Software_and_Drivers import *
from Main_Product_Page import *
from Open_Close import *
from Input_Submit import *
# --- Product Class Definition ---
class Product:
def __init__(self, driver, full_product_name, final_answer, software_link, specifications_link, maintenance_link):
self.full_product_name = full_product_name
self.final_answer = final_answer
self.software_link = software_link
self.specifications_link = specifications_link
self.maintenance_link = maintenance_link
# --- Setters ---
def set_software_link(self, url):
self.software_link = url
def set_specifications_link(self, url):
self.specifications_link = url
def set_maintenance_link(self, url):
self.maintenance_link = url
# --- Methods to retrieve URLs ---
def find_software_link(self, driver):
return Software_and_Drivers_Link(driver)
def find_specifications_link(self, driver, identifier):
return Product_Specifications_Link(driver, identifier)
def find_maintenance_link(self, driver):
return Maintenance_and_Service_Guide_Link(driver)
# --- Getters ---
def get_software_link(self):
return self.software_link
def get_specifications_link(self):
return self.specifications_link
def get_maintenance_link(self):
return self.maintenance_link
def get_full_product_name(self):
return self.full_product_name
# --- MySQL Sign In and cursor object creation for a MySQL database located on the local host ---
mysql_credentials_path = ""
s = open(mysql_credentials_path, "r")
user = s.readline()
password = s.readline()
database_name = ""
db = mysql.connector.connect(
host = "localhost",
user = "%s" % user,
password = "%s" % password,
database = "%s" % database_name
)
mycursor = db.cursor()
# --- MySQL Sign In and cursor object creation for a MySQL database located on Microsoft Azure ---
'''
azure_mysql_creds_path = ""
s = open(azure_mysql_creds_path, "r")
server = s.readline()
user = s.readline()
password = s.readline()
database_name = ""
db = mysql.connector.connect(
host = "%s" % server,
user = "%s" % user,
password = "%s" % password,
database = "%s" % database_name
)
mycursor = db.cursor()
'''
# --- Driver Creation ---
webdriver_path = ""
# driver = webdriver.Edge(webdriver_path)
driver = webdriver.Chrome(webdriver_path)
driver.get('https://h30434.www3.hp.com/t5/notificationfeed/page')
driver.maximize_window()
# Declares the Product dictionary, which will act as a list of instances of the "Product" class
Device = {}
# Signs into the support community
Sign_In_Notifications(driver)
# Opens HP Support's Main Support Page to close intrusive popups once
Main_Product_Page_Close(driver)
while True:
# Identifies notification list
notification_list = driver.find_element_by_id("notificationList")
ul = notification_list.find_element_by_xpath(".//ul")
# Finds unread mentions
driver.implicitly_wait(5)
unread_mention_list = ul.find_elements_by_xpath(".//li[@class='lia-notification-feed-item lia-notification-mentions lia-notification-unread lia-component-notificationfeed-widget-notification-feed-item']") #lia-notification-feed-item lia-notification-mentions lia-component-notificationfeed-widget-notification-feed-item
if (len(unread_mention_list) == 0):
pass
else:
# Loops through unread mentions
for mention in unread_mention_list:
user_element = mention.find_element_by_xpath(".//div/div/div[2]/div/span")
driver.execute_script("arguments[0].scrollIntoView();", user_element)
user_text = user_element.text
# Checks if the user mentioning the bot is EddyK
if user_text == 'EddyK':
# Initializes the final answer
final_answer = ""
# Opens forum thread with the unread mention in a new tab
forum_thread_link = mention.find_element_by_xpath(".//div/div/div[2]/div/a")
forum_thread_link.send_keys(Keys.CONTROL + Keys.ENTER)
driver.switch_to.window(driver.window_handles[1])
# Returns the country of the original poster, if available, and clicks on 'Reply' of the post containing the unread mention
country = Identify_Post_and_Country(driver)
# Returns the full command and identifies the individual commands from the mention
full_command = Return_Commands(driver)
individual_commands = full_command.split(",")
# Checks if the command is only a support command
if (len(full_command.split(" ")) == 1):
# Support Function Only
pass
else:
# Finds the identifier in the full command
i = full_command.split(",")
for j in i:
k = j.split(" ")
if Command_Type(k[1]) == 1:
identifier = " ".join(k[2:])
break
# Opens the Main Product Page in browser handle 2 and returns the full product name
full_product_name, final_answer = Main_Product_Page(driver, identifier, final_answer)
# Checks if the full product name exists in the MySQL database
if Check_If_Exist(mycursor, full_product_name):
# Retrieves all links from the SQL database
software_link, specifications_link, maintenance_link = SQL_Get_Links(mycursor, full_product_name)
Device[identifier] = Product(driver, full_product_name, final_answer, software_link, specifications_link, maintenance_link)
# SQL Database Command Switcher
for individual_command in individual_commands:
split_individual_command = individual_command.split(" ")
command_number = Command_Type(split_individual_command[1])
# Pass the 'product' command
if command_number == 1:
pass
# Specifications
elif command_number == 2:
if Device[identifier].get_specifications_link == None:
Device[identifier].final_answer += '<p>Product Specifications for the %s were not found.<p>' % Device[identifier].get_full_product_name()
else:
Open_URL(driver, Device[identifier].get_specifications_link())
if len(split_individual_command) == 2:
Product_Specifications_Answer(driver, Device[identifier], "all", Device[identifier].get_specifications_link(), Device[identifier].get_full_product_name())
else:
Product_Specifications_Answer(driver, Device[identifier], split_individual_command[2:], Device[identifier].get_specifications_link(), Device[identifier].get_full_product_name())
# Maintenance
elif command_number == 3:
if Device[identifier].get_maintenance_link == None:
Device[identifier].final_answer += '<p>The Maintenance and Service Guide for the %s was not found.<p>' % Device[identifier].get_full_product_name()
else:
if command_number == 3:
if len(split_individual_command) == 2:
Maintenance_and_Service_Guide_Answer(driver, Device[identifier], "No Page", Device[identifier].get_maintenance_link(), Device[identifier].get_full_product_name())
else:
Maintenance_and_Service_Guide_Answer(driver, Device[identifier], split_individual_command[2], Device[identifier].get_maintenance_link(), Device[identifier].get_full_product_name())
# Software
elif command_number == 4:
if Device[identifier].get_maintenance_link == None:
Device[identifier].final_answer += '<p>The Software and Drivers page for the %s was not found.<p>' % Device[identifier].get_full_product_name()
else:
Open_URL(driver, Device[identifier].get_software_link())
Software_and_Drivers_Answer(driver, Device[identifier], split_individual_command[2:], Device[identifier].get_software_link(), Device[identifier].get_full_product_name())
elif command_number == 5:
# Support
pass
else:
Device[identifier].final_answer += "<p>The command '<i>%s</i>' was not recognized.</p>" % (split_individual_command[1])
else:
# Creates new product instance and assigns it to the dictionary device, which is accessible through the identifier
Device[identifier] = Product(driver, full_product_name, final_answer, "", "", "")
specifications_request_is_found = False
maintenance_request_is_found = False
software_request_is_found = False
# Selenium Command Switcher
for individual_command in individual_commands:
split_individual_command = individual_command.split(" ")
command_number = Command_Type(split_individual_command[1])
# Pass the 'product' command
if command_number == 1:
pass
# Specifications
elif command_number == 2:
specifications_request_is_found = True
if len(split_individual_command) == 2:
specs_arguments = "all"
else:
specs_arguments = split_individual_command[2:]
# Maintenance
elif command_number == 3:
maintenance_request_is_found = True
if command_number == 3:
if len(split_individual_command) == 2:
maintenance_argument = "No Page"
else:
maintenance_argument = split_individual_command[2]
# Software
elif command_number == 4:
software_request_is_found = True
software_arguments = split_individual_command[2:]
elif command_number == 5:
# Support
pass
else:
Device[identifier].final_answer += "<p>The command '<i>%s</i>' was not recognized.</p>" % (split_individual_command[1])
# Opens the Specifications page, saves it, and leaves the tab open
Device[identifier].set_specifications_link(Device[identifier].find_specifications_link(driver, identifier))
if specifications_request_is_found:
if Device[identifier].get_specifications_link == None:
Device[identifier].final_answer += '<p>Product Specifications for the %s were not found.<p>' % Device[identifier].get_full_product_name()
else:
Product_Specifications_Answer(driver, Device[identifier], specs_arguments, Device[identifier].get_specifications_link(), Device[identifier].get_full_product_name())
# Closes New Tab
Close_Current_Tab(driver)
# Opens the Maintenance and Service Guide page, saves it, and leaves the tab open
Device[identifier].set_maintenance_link(Device[identifier].find_maintenance_link(driver))
if maintenance_request_is_found:
if Device[identifier].get_maintenance_link == None:
Device[identifier].final_answer += '<p>The Maintenance and Service Guide for the %s was not found.<p>' % Device[identifier].get_full_product_name()
else:
Maintenance_and_Service_Guide_Answer(driver, Device[identifier], maintenance_argument, Device[identifier].get_maintenance_link(), Device[identifier].get_full_product_name())
# Closes New Tab
Close_Current_Tab(driver)
# Opens the Software and Drivers page in the same tab, saves it, and reverts it back to the default main product page
Device[identifier].set_software_link(Device[identifier].find_software_link(driver))
if software_request_is_found:
if Device[identifier].get_maintenance_link == None:
Device[identifier].final_answer += '<p>The Software and Drivers page for the %s was not found.<p>' % Device[identifier].get_full_product_name()
else:
Software_and_Drivers_Answer(driver, Device[identifier], software_arguments, Device[identifier].get_software_link(), Device[identifier].get_full_product_name())
SQL_Store_Links(mycursor, db, Device[identifier].get_full_product_name(), Device[identifier].get_software_link(), Device[identifier].get_specifications_link(), Device[identifier].get_maintenance_link())
driver.close()
driver.switch_to.window(driver.window_handles[1])
# Submit the answer
Input_Submit(driver, Device[identifier])
driver.close()
# Reset to first browser handle
driver.switch_to.window(driver.window_handles[0])
# Refresh the notifications page to check for new notifications
driver.refresh()