-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (58 loc) · 2.84 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
import requests
import json
from os.path import exists, join, dirname
from os import mkdir
from bs4 import BeautifulSoup as BS
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Preparation with the list of notifications to do
informationFile = open(join(dirname(__file__), "information.json"), "r", encoding="UTF-8")
taskList = json.load(informationFile)
informationFile.close()
# Notify news for each person's wanted exams
for person in taskList:
if not exists(join(dirname(__file__), "users", person["name"])):
mkdir(join(dirname(__file__), "users", person["name"]))
for exam in person["exams"]:
# Preparation with needed list
saved_tests = [] # saved in file
read_tests = [] # newly read
new_tests = [] # not in file
# Get the saved dates
if exists(join(dirname(__file__), "users", person["name"], exam["id"] + ".txt")):
readFile = open(join(dirname(__file__), "users", person["name"], exam["id"] + ".txt"), "r", encoding="UTF-8")
saved_tests = readFile.readlines()
readFile.close()
# Get the new read dates and compare to saved dates to find the lately update dates
url = "https://iigvietnam.com/lich-thi/?test_type=" + exam["id"] + "&location=" + exam["location id"] + "&start_date=" + exam["start date"] + "&end_date=" + exam["end date"]
reader = BS(requests.get(url).content, "lxml").find_all("td")
index = 1
for idx_tag in range(len(reader)):
if (reader[idx_tag].string == str(index)):
test = str(reader[idx_tag+1].string) + ' - ' + str(reader[idx_tag+2].string) + '\n'
if test not in saved_tests:
new_tests.append(test)
saved_tests.append(test)
index += 1
# Update the list of saved exam
writeFile = open(join(dirname(__file__), "users", person["name"], exam["id"] + ".txt"), "w", encoding="UTF-8")
for test in saved_tests:
writeFile.write(test)
writeFile.close()
# Send email
'''
msg = MIMEMultipart('alternative')
msg['Subject'] = "Cập nhật mới nhất cho lịch thi có mã số " + exam["id"]
msg['From'] = "<sender_email>"
msg['To'] = person["gmail"]
content = "Xin chào " + person["name"] + ", đây là những cập nhật mới nhất đối với lịch thi mà bạn đang quan tâm:\n"
for exam in new_exams:
content = content + exam + "\n"
body = MIMEText(content, 'plain', "UTF-8")
msg.attach(body)
s = smtplib.SMTP("localhost")
#s.login(SMTP_user, SMTP_password)
s.sendmail("sender_email>", person["gmail"], msg.as_string())
s.quit()
'''