-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
175 lines (158 loc) · 6 KB
/
script.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
import subprocess
import yaml
from selenium import webdriver
from PIL import Image
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import pytesseract
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
prefs = {
"credentials_enable_service": False,
"profile.password_manager_enabled": False,
"download.default_directory": "C:\\x\\docs", # directory to store downloaded attachments
}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("--headless")
chrome_options.add_argument("window-size=1280x720")
driver = webdriver.Chrome(options=chrome_options)
def get_credentials(): # create a login.yml file with your own slcm user and password
conf = yaml.safe_load(open("login.yml"))
myslcmUsername = conf["slcm_user"]["username"]
myslcmPassword = conf["slcm_user"]["password"]
return myslcmUsername, myslcmPassword
def login(
url,
usernameId,
username,
passwordId,
password,
submit_buttonId,
captchaId,
imgCaptcha,
):
# driver.maximize_window()
driver.delete_all_cookies()
driver.get(url)
driver.find_element("id", usernameId).send_keys(username)
driver.find_element("id", passwordId).send_keys(password)
img = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, imgCaptcha))
)
driver.save_screenshot("screenshot.png")
image = Image.open("screenshot.png")
left = 828
top = 363
right = left + 250
bottom = top + 52
image = image.crop((left, top, right, bottom))
image.save("captcha.png")
image = Image.open("captcha.png")
image2 = image.resize((200, 120))
image2.save("captchafinal.png")
captcha_text = pytesseract.image_to_string(Image.open("captchafinal.png")) # Bypassing captcha
driver.find_element("id", captchaId).send_keys(captcha_text)
driver.find_element("id", submit_buttonId).click()
# driver.implicitly_wait(3) # seconds
# error_text = WebDriverWait(driver, 10).until(
# EC.presence_of_element_located((By.ID, errorId))
# )
# error_text = driver.find_element("id", errorId).text
# print("Error: ", error_text)
if captcha_text == "":
print("Error in generating captcha, trying again")
login(
"https://slcm.manipal.edu/",
"txtUserid",
username,
"txtpassword",
password,
"btnLogin",
"txtCaptcha",
"imgCaptcha",
)
else:
imp_docs()
def imp_docs():
test = WebDriverWait(driver, 20).until( # explicit wait due to varying internet connection speeds
EC.visibility_of_element_located(
(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div[2]/div[2]/div/div/div/div[2]/div[2]/table/tbody/tr[1]/td[4]/center/a",
)
)
)
driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div[2]/div[2]/div/div/div/div[2]/div[2]/table/tbody/tr[1]/td[4]/center/a",
).click()
# driver.get("https://slcm.manipal.edu/ImportantDocuments.aspx")
test = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[2]",
)
)
)
latest_title = driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[2]",
).text
with open("titles.txt", "r") as f:
last_line = f.readlines()[-1]
# print("Last Title: ", last_line)
if latest_title == last_line:
print("Latest Title: ", latest_title, "\nNo new updates")
driver.quit()
else:
with open("titles.txt", "a") as f:
f.write("\n")
latest_title = driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[4]/td[2]",
).text
f.write(latest_title)
latest_title = driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[3]/td[2]",
).text
f.write("\n")
f.write(latest_title)
latest_title = driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[2]",
).text
f.write("\n")
f.write(latest_title)
driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[4]/td[3]/a",
).click()
driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[3]/td[3]/a",
).click()
driver.find_element(
By.XPATH,
"/html/body/div[3]/form/div[5]/div/div/div/div/div/div/div[2]/div/div/table/tbody/tr[2]/td[3]/a",
).click()
# print("Latest Title: ", latest_title, "\nNew update found")
subprocess.run(["python", "mails.py"])
driver.quit()
# main.py
myslcmUsername, myslcmPassword = get_credentials()
login(
"https://slcm.manipal.edu/",
"txtUserid",
myslcmUsername,
"txtpassword",
myslcmPassword,
"btnLogin",
"txtCaptcha",
"imgCaptcha",
)