-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvu.py
342 lines (266 loc) · 10.3 KB
/
vu.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/python3
from bs4 import BeautifulSoup
from time import sleep
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import requests
import datetime
import pytz
import json
import re
import os
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
def fixAndReturnDates(start, end):
"""Since the script runs on Gitlab - Time is in different time zone
causing the script to provide wrong alerts or wrong dates/times.
This function converts the time in Asia/Karachi timezone.
Args:
start ([str]): Starting date of the assigned task
end ([str]): Ending date of the assigned task
"""
start = datetime.datetime.strptime(
"-".join(start.split(",")[::-1]), "%d-%m-%Y"
).strftime("%d-%m-%Y")
end = (
datetime.datetime.strptime("-".join(end.split(",")[::-1]), "%d-%m-%Y")
- datetime.timedelta(days=1)
).strftime("%d-%m-%Y")
dateToday = datetime.datetime.now(pytz.timezone("Asia/Karachi")).strftime(
"%d-%m-%Y"
)
# print(start, end, dateToday)
return (start, end, dateToday)
def returnRequestDetailsOnFailure(url, customString, requestObj):
"""For debugging request/response being sent/recieved from the
VU application.
Args:
url ([str]): URL the request sent to
customString ([str]): Indicating where it's happening
requestObj ([object]): request object containing all methods
"""
string = f"\n[!] {customString}: {url}\n"
string += f"[!] Status Code: {requestObj.status_code}\n"
string += f"[!] Response Headers: {requestObj.headers.items()}\n"
string += f"[!] Some text of the body: {requestObj.text[:200]}\n"
print(string)
def fetch_recaptcha_response():
"""
Fetches g-recaptcha-response *now* required for login
Previously it was implemented but devs weren't checking the parameter on backend but
it seems now they've added a strict check which can no longer be bypassed so we'll
just fetch it from selenium
"""
chrome_service = Service(ChromeDriverManager().install())
chrome_options = Options()
options = [
"--headless",
"--disable-gpu",
"--window-size=1920,1200",
"--ignore-certificate-errors",
"--disable-extensions",
"--no-sandbox",
"--disable-dev-shm-usage"
]
for option in options:
chrome_options.add_argument(option)
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
driver.get("https://vulms.vu.edu.pk/LMS_LP.aspx")
# To fetch the captcha response
sleep(5)
captcha_text = driver.find_element(By.ID, "g-recaptcha-response").get_attribute('value')
print(captcha_text)
return(captcha_text)
def loginIntoWebApplication(studentId, password):
"""Login into the application and return the sesion to use for
further requests
Args:
studentId ([str]): Username/VU ID
password ([str]): Login password
Returns:
[object]: Requests' Session object
"""
loginURL = "https://vulms.vu.edu.pk:443/LMS_LP.aspx"
session = requests.session()
getLoginParameters = session.get(
loginURL,
# proxies = {
# 'http': '127.0.0.1:8080',
# 'https': '127.0.0.1:8080',
# },
verify=False,
)
if getLoginParameters.status_code == 200:
soup = BeautifulSoup(getLoginParameters.text, "html.parser")
viewstate = soup.find_all("input", {"id": "__VIEWSTATE"})[0]["value"]
eventvalid = soup.find_all("input", {"id": "__EVENTVALIDATION"})[0][
"value"
]
g_recaptcha_response = fetch_recaptcha_response()
# Login form won't work without these params
# print(f"[%] __VIEWSTATE: {viewstate}")
# print(f"[%] __EVENTVALIDATION: {eventvalid}")
# Loggin in with the viewstate and eventvalidation parameters.
login = session.post(
loginURL,
# proxies = {
# 'http': '127.0.0.1:8080',
# 'https': '127.0.0.1:8080',
# },
verify=False,
data={
"__VIEWSTATE": viewstate,
"__EVENTVALIDATION": eventvalid,
"txtStudentID": studentId,
"txtPassword": password,
"cbKeepMeLogin": "on",
"ibtnLogin": "Sign In",
"g-recaptcha-response": g_recaptcha_response
},
)
if login.status_code == 200:
cookies = dict(login.cookies)
if "stdUserName" in cookies:
# print(f"\n[#] User ({cookies['stdUserName']}) logged in!")
# Need to give /Home.aspx a hit with some token without that, the cookies don't work lol! (found after hella debuggin)
locationHref = re.findall(
r"window\.top\.location\.href=\'Home\.aspx\?id\=(.*?)\'\;",
login.text,
)[0]
homePath = (
f"https://vulms.vu.edu.pk/Home.aspx?id={locationHref}"
)
homeRequest = session.get(
homePath,
# proxies = {
# 'http': '127.0.0.1:8080',
# 'https': '127.0.0.1:8080',
# },
verify=False,
)
return session
else:
returnRequestDetailsOnFailure(
url=loginURL,
customString="There was an error trying to fetch params",
requestObj=getLoginParameters,
)
else:
returnRequestDetailsOnFailure(
url=loginURL,
customString="There was an error trying to login",
requestObj=login,
)
def fetchCalendarAndDetails(session):
"""Fetches the Calendar for all the tasks assigned
Args:
session ([object]): Requests session to do further requests
Returns:
[str]: Returns the Discord post
"""
calendarURL = (
"https://vulms.vu.edu.pk/ActivityCalendar/ActivityCalendar.aspx"
)
try:
request = session.get(
calendarURL,
# proxies = {
# 'http': '127.0.0.1:8080',
# 'https': '127.0.0.1:8080',
# },
verify=False,
)
if request.status_code == 200:
post = ""
source = request.text
jsonData = re.findall(r"var\sJsonData\s\=\s(.*?)\;", source)[0]
calendarJSON = json.loads(jsonData)
print(json.dumps(calendarJSON, indent=4))
print()
for subjects in calendarJSON:
title = subjects.get("title")
if not title:
title = subjects.get("Title")
start = subjects.get("Start")
end = subjects.get("end")
startDate, endDate, dateToday = fixAndReturnDates(start, end)
# print(title, startDate, endDate, dateToday, subtDate)
subtDate = datetime.datetime.strptime(
endDate, "%d-%m-%Y"
) - datetime.datetime.strptime(dateToday, "%d-%m-%Y")
subtDate = str(subtDate).split(" ")[0]
if "0:00:00" in subtDate:
subtDate = 1
subtDate = int(subtDate)
if startDate == dateToday:
startDate += " **(today)**"
if endDate == dateToday:
endDate += " __**(today)**__"
if subtDate == -1:
if startDate != endDate:
if "Result" in title:
post += f"[#] **{title}**\n"
post += f"Start date: {startDate}\n\n"
if startDate < endDate:
post += f"End date: {endDate}\n\n"
else:
post += f"[#] **{title}**\n"
post += f"Start date: {startDate}\n"
if startDate < endDate:
post += f"End date: {endDate}\n\n"
if subtDate > 0:
if subtDate == 1:
post += (
f"[#] **{title}** (_**{subtDate}** day left_)\n"
)
else:
post += (
f"[#] **{title}** (_**{subtDate}** days left_)\n"
)
post += f"Start date: {startDate}\n"
post += f"End date: {endDate}\n\n"
print(post)
return post
else:
returnRequestDetailsOnFailure(
url=calendarURL,
customString="There was an error trying to fetch calendar",
requestObj=request,
)
# except KeyboardInterrupt:
# print("\n[!] Did you forget to add credentials in config.json?")
except AttributeError:
print("\n[!] Did you forget to add credentials in config.json?")
def postIntoDiscord(post, webHookURL):
"""Posts the given string into discord channel through webhook
Args:
post ([str]): Summary/Data to post
webHookURL ([str]): Webhook URL of discord channel
"""
discordPost = requests.post(webHookURL, {"content": post})
if discordPost.status_code == 204:
print("[#] Posted in discord!")
else:
returnRequestDetailsOnFailure(
url=webHookURL[:31],
customString="There was an error trying to post on webhook",
requestObj=discordPost,
)
def main():
if (
os.getenv("VUSERNAME")
and os.getenv("VPASSWORD")
and os.getenv("WEBHOOK_URL")
):
studentId = os.getenv("VUSERNAME")
password = os.getenv("VPASSWORD")
webHookURL = os.getenv("WEBHOOK_URL")
print("[&] Logging into the Web application...\n")
session = loginIntoWebApplication(studentId, password)
post = fetchCalendarAndDetails(session)
postIntoDiscord(post, webHookURL)
if __name__ == "__main__":
main()