-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert.py
79 lines (68 loc) · 2.42 KB
/
alert.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
#!/usr/bin/env python
# encoding: utf-8
import send_mail
import query
import re
class PsuRegisAlert(object):
def __init__(self):
self.regis_query = query.PsuRegisQuery()
def send_notice_mail(self, toaddr, subject, message):
send_mail.SendMail.send(toaddr, subject, message)
def _display_result(self, subject_id, subject_name, results):
return (
subject_id +
"\n" +
subject_name +
"\n" +
"sec " +
"\nsec ".join(results))
def list_sec_rooms(self, sec_rooms):
list_sec = []
for k, v in sec_rooms.items():
if v:
list_sec.append(k)
return list_sec
def get_static_url(self, url):
selected_node = 3
template = re.sub(r"(^\D+)(\d+)(.psu.*$)", r"\1{0}\3", url)
url = template.format(selected_node)
return url
def alert(self, url):
url = self.get_static_url(url)
result, sec_rooms = self.regis_query.query(url)
subject_id, _, has_room = result
if has_room:
self.subject_id = subject_id
self.message = self._display_result(*result)
self.list_rooms = self.list_sec_rooms(sec_rooms)
return True
else:
self.subject_id = ""
self.message = "No room"
self.list_rooms = None
return False
if __name__ == '__main__':
email = None
# hatyai normal url
url = \
"https://sis-hatyai6.psu.ac.th/WebRegist2005/" \
"SubjectInfo.aspx?subject=2558100048520119"
# hatyai normal url with login require
# that should avoid by remove /Student
# url = "https://sis-hatyai46.psu.ac.th/WebRegist2005/" \
# "Student/SubjectInfo.aspx?subject=2558100064080120"
# phuket normal url
# that should enable cookie before open request
# url = "https://sis-phuket4.psu.ac.th/WebRegist2005/" \
# "SubjectInfo.aspx?subject=2558100024830001"
# phuket normal url with login require
# that should avoid by remove /Search
# url = # "https://sis-phuket2.psu.ac.th/WebRegist2005/Search/SubjectInfo.aspx?subject=2558100023120009"
# url = "https://sis-hatyai50.psu.ac.th/WebRegist2005/SubjectInfo.aspx?subject=2558100048680119"
alert = PsuRegisAlert()
try:
if alert.alert(url):
print alert.list_rooms
print alert.message
except AttributeError as e:
print(e)