-
Notifications
You must be signed in to change notification settings - Fork 0
/
websoc_app.py
164 lines (128 loc) · 5.3 KB
/
websoc_app.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
import scrape_websoc
from datetime import datetime
import time
import pushbullet
import pprint
import traceback
from logger import *
from sqlite_manager import sqlite_manager
from config import *
from Course import Course
class UserData:
def __init__(self, email: str, user: str, courses: [int]):
self._user = user
self._courses = courses
self._email = email
def get_email(self) -> str:
return self._email
def get_name(self) -> str:
return self._user
def get_courses(self) -> str:
return self._courses
def set_courses(self, course_list):
self._courses = course_list
def set_email(self, email):
self._email = email
def __eq__(self, other):
if type(other) == UserData: return self.get_name() == other.get_name()
return other == self.get_name()
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self) -> str:
return 'UserData(email={}, user={}, courses={})'.format(self._email, self._user, self._courses)
def __hash__(self):
return hash(repr(self))
class App:
def __init__(self):
self._users = []
self._last_course_data = []
initialize_logger(LOG_DIRECTORY)
with sqlite_manager(WEBSOC_TERM, row_factory=True) as s_manager:
for course in s_manager.get_all_latest():
d = {k:course[k] for k in course.keys() if k != 'last_updated'}
c = Course()
c.__dict__ = d
self._last_course_data.append(c)
def add_user(self, data: UserData) -> None:
self._users.append(data)
def get_new_course_data(self) -> [scrape_websoc.Course]:
courses = set()
for user in self._users:
courses.update(user.get_courses())
return scrape_websoc.query_websoc(list(courses))
@staticmethod
def parse_change(changes, old, new):
old_dict = old.__dict__
new_dict = new.__dict__
s = '\n '
for change in changes:
s += '{}: {} -> {}\n '.format(change, old_dict[change], new_dict[change])
info = '{code} (max: {c_max}) ({num} {c_type} {section} -- {instructor}): {string}'.format(
c_max = new.max_slots, code = new.code, num = new.num, c_type = new.c_type,
section = new.section, instructor = new.instructor, string = s.rstrip()
)
return info
@staticmethod
def notify(affects, info):
affect_names = [user.get_name() for user in affects]
logging.info('-- Affects: {}'.format(affect_names))
for user in affects:
name = user.get_name()
email = user.get_email()
if email:
try:
pushbullet.send_push(email, '{}: websoc-notify'.format(name), info)
except Exception as e:
logging.error('Could not push!')
logging.error('\t'+str(traceback.format_exc()))
@staticmethod
def notify_set(n_set):
for x in n_set:
App.notify(x[0], x[1])
@staticmethod
def write_sqlite(courses):
with sqlite_manager(WEBSOC_TERM) as s_manager:
s_manager.add_many([dict(course.__dict__) for course in courses])
def refresh(self) -> None:
now = datetime.now()
logging.info('=============== {} ==============='.format(str(now)))
new_data = self.get_new_course_data()
notification_set = set()
queued_sql_entries = []
for new in new_data:
if new not in self._last_course_data:
logging.info('Added {code} (max: {c_max}) ({num} {c_type} {section} -- {instructor})'.format(
c_max = new.max_slots, code = new.code, num = new.num, c_type = new.c_type,
section = new.section, instructor = new.instructor
))
#pprint.pprint(new.to_dict())
if new not in queued_sql_entries:
queued_sql_entries.append(new)
self._last_course_data.append(new)
else:
i = self._last_course_data.index(new)
old = self._last_course_data[i]
self._last_course_data[i] = new
changes = scrape_websoc.dict_changes(old.__dict__, new.__dict__)
if changes:
info = self.parse_change(changes, old, new)
logging.info(info)
affects = [user for user in self._users if new.code in user.get_courses()]
notification_set.add((frozenset(affects), info))
if new not in queued_sql_entries:
queued_sql_entries.append(new)
if notification_set:
self.notify_set(notification_set)
if queued_sql_entries:
self.write_sqlite(queued_sql_entries)
def loop(self) -> None:
try:
self.refresh()
except Exception as e:
logging.error('\t'+str(traceback.format_exc()))
def main(self) -> None:
while True:
self.loop()
time.sleep(15)
def __del__(self):
print('del!')