-
Notifications
You must be signed in to change notification settings - Fork 1
/
first_crawl.py
109 lines (88 loc) · 3.5 KB
/
first_crawl.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
from selenium import webdriver
import os
from time import sleep
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
@sched.scheduled_job('cron', day_of_week='mon-sun', hour=0, minute=30)
def scheduled_job():
options = webdriver.ChromeOptions()
options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
browser = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=options)
#-----Crawling part starts here------------
url = 'https://lmsone.iiitkottayam.ac.in/#/'
browser.get(url)
m_name = os.environ.get("MNAME")
search = browser.find_element_by_name('username')
m_pass = os.environ.get("MPASS")
search.send_keys(m_name)
search = browser.find_element_by_name('password')
search.send_keys(m_pass)
submit = browser.find_element_by_id('loginbtn')
sleep(2)
submit.click()
sleep(5)
url = 'https://lmsone.iiitkottayam.ac.in/calendar/view.php'
browser.get(url)
tasks = browser.find_elements_by_xpath('//h3[@class="name d-inline-block"]')
t = []
for i in tasks:
t.append(i.text)
def check(x):
if x[0] not in ['Today,', 'Tomorrow,']: return True
if x[0]=='Tomorrow,' and x[-1]=='PM': return True
if x[0]=='Today,': return False
y = x[-2].split(':')
if y[0] == '12': y[0] = '00'
mins = int(y[0])*60 + int(y[1])
if mins > 360: return True
return False
pending_assignments = browser.find_elements_by_xpath('//div[@class="description card-body"]//div[@class="row"]//div[@class="col-11"]')
pending_assignments_name = browser.find_elements_by_xpath('//div[@class="description card-body"]//div[@class="row mt-1"]//div[@class="col-11"]//a')
assignments = []
for i in range(len(pending_assignments)):
y = str(pending_assignments[i].text)
y1 = str(pending_assignments_name[i].text)
z = y.split()
if check(z): break
z.append(t[i])
z.append(y1)
assignments.append(z)
j = 1
mail = ''
for i in assignments:
mail += (i[-1]+' : '+i[0]+' '+i[1]+' '+i[2]+' : '+i[-2]+"\n\n")
j+=1
#------Mailing Part start here!!--------
if mail:
sleep(5)
recievers = []
sheet_json = os.environ.get("JSON")
spreadsheet_data = requests.get(sheet_json)
spreadsheet_data = spreadsheet_data.json()
for i in spreadsheet_data:
if i['Select Yes/No.'] == 'Yes':
recievers.append(i['Email Address'])
msg = MIMEMultipart()
maill = os.environ.get("EMAIL")
passs = os.environ.get("PASSWORD")
msg['From'] = "Selenium Bot"
msg['To'] = ','.join(recievers)
msg['Subject'] ="Assignemnts To Be Submitted Today!"
body = 'Hello,\nThe assignments that require your attention today are listed below\n\n'+mail+'\n\nRegards.'
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
smtp_server = os.environ.get("SMTP_SERVER")
sm= smtplib.SMTP(smtp_server, 587)
sm.starttls()
sm.login(maill, passs)
sm.sendmail(maill, recievers, text)
sm.quit()
sched.start()