-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
273 lines (246 loc) · 12.5 KB
/
test.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
from bs4 import BeautifulSoup
import requests
import ssl
import re
from ssl_type import SSLAdapter
import config
import pymongo
import os
from telegram import chat
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import datetime
import calendar
import messages
TELENAME = ""
TOKEN = ""
PORT = int(os.environ.get('', ''))
NAME = ''
updater = Updater(TOKEN)
updater.start_webhook(listen="0.0.0.0",
port=PORT,
url_path=TOKEN)
updater.start_webhook(listen="0.0.0.0",port=int(PORT),url_path=TOKEN)
updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(NAME, TOKEN))
dispatcher = updater.dispatcher
client = pymongo.MongoClient("")
user_db = client[""]
user_list = user_db[""]
def start(bot, update):
welcome_string = "".join(messages.welcome_text)
bot.send_message(chat_id=update.message.chat_id, text=welcome_string)
if (user_list.count_documents({"userid": str(update.message.chat_id)})==1):
for x in user_list.find({"userid": str(update.message.chat_id)},{ "_id": 0,"userkey": 1 }):
name = x[""]
db = client[""]
global timetable
timetable = db[name]
bot.send_message(chat_id=update.message.chat_id, text=("Welcome back, " + name.title()))
else:
bot.send_message(chat_id=update.message.chat_id, text=("You seem to be new, please type /create (your single-worded name) to register. The bot will not function until registration is done."))
dispatcher.add_handler(CommandHandler('start', start))
job = updater.job_queue
def create_db(bot, update, args):
if (len(args)==0):
bot.send_message(chat_id=update.message.chat_id, text="🧒 Please enter a single-worded name")
return
if (len(args)>1):
bot.send_message(chat_id=update.message.chat_id, text="🧒 Please enter a single-worded name")
return
global db
name = args[0]
db = client["timetableapp"]
global timetable
timetable = db[name]
user_entry = { "userid": str(update.message.chat_id), "userkey": name }
user_list.insert_one(user_entry)
bot.send_message(chat_id=update.message.chat_id, text="Name added to database! Please call /addmodules to add your modules! See /help for an example of how to add them!")
dispatcher.add_handler(CommandHandler('create', create_db, pass_args=True))
def fetch_schedule(courseCode):
message = "OK"
url = \
"https://wish.wis.ntu.edu.sg/webexe/owa/AUS_SCHEDULE" + \
".main_display1?acadsem=" + config.ACADYEAR + ";" + config.ACADSEM + \
"&r_search_type=F&r_subj_code=" + courseCode.upper() + \
"&boption=Search&staff_access=false&acadsem=" + config.ACADYEAR + \
";" + config.ACADSEM + "&r_course_yr="
try:
s = requests.Session()
s.mount("https://", SSLAdapter(ssl.PROTOCOL_TLSv1))
r = s.post(url)
except requests.exceptions.ConnectionError:
message = "Connection error. Cannot connect to NTU server."
exit(-1)
soup = BeautifulSoup(r.text, features="html.parser")
try:
name_table = schedule = soup.find_all("table")[0].find_all("td")
schedule = soup.find_all("table")[1].find_all("td")
except IndexError:
message = "The course code does not exist in NTU database for this semester."
exit(-1)
return schedule, name_table, message
def course_entry_into_database(course_code, index):
if re.search(r"^\w{2}\d{4}$", course_code) == None:
return("invalid course code format (not xxYYYY)")
schedule, name_table, message = fetch_schedule(course_code)
if message != "OK":
return(message)
course_name = str(name_table[1])[41:-16]
_dict = {}
for i in range(len(schedule)):
result = re.search(r"\d{5}", str(schedule[i]))
if result != None:
_dict[result.group(0)]=i
if index not in _dict:
return("index wrong")
else:
if len(_dict) != 1:
number_per_index = list(_dict.values())[1]-list(_dict.values())[0]
index_full = schedule[_dict[index]:_dict[index]+number_per_index]
elif len(_dict) == 1:
index_full = schedule[:]
number_of_classes = (len(index_full)//7)
print(number_of_classes)
for j in range(number_of_classes):
if (timetable.count_documents({ "course_name": course_name, "course_code": course_code, "type": str(index_full[j*7+1])[7:-9], "day": str(index_full[j*7+3])[7:-9], "time": str(index_full[j*7+4])[7:-9], "location": str(index_full[j*7+5])[7:-9], "index": index, "acadyear": config.ACADYEAR, "acadsem": config.ACADSEM}) == 0):
class_entry = {"course_name": course_name, "course_code": course_code, "type": str(index_full[j*7+1])[7:-9], "day": str(index_full[j*7+3])[7:-9], "time": str(index_full[j*7+4])[7:-9], "location": str(index_full[j*7+5])[7:-9], "index": index, "acadyear": config.ACADYEAR, "acadsem": config.ACADSEM}
timetable.insert_one(class_entry)
#return("info added")
else:
pass
#return("info already inside")
return(course_code + " added!")
def get_course_list(bot,update,args):
course_dict = {}
if (len(args)%2!=0):
bot.send_message(chat_id=update.message.chat_id, text="Invalid entry format. Please enter\n/add \n(course code) (index)\n(course code) (index)\n for the modules in your timetable")
return
else:
for x in range(len(args)//2):
reply = course_entry_into_database(args[2*x],args[2*x+1])
bot.send_message(chat_id=update.message.chat_id, text=reply)
dispatcher.add_handler(CommandHandler('addmodules', get_course_list, pass_args=True))
def tomorrow(bot,update):
tomorrow_day = (datetime.datetime.today()+ datetime.timedelta(days=1)).strftime('%a')
tomorrow_longday = (datetime.datetime.today()+ datetime.timedelta(days=1)).strftime('%A')
daily = ""
daily += "📝 Here's your timetable for tomorrow!\n"
daily += "====================\n"
daily += (tomorrow_longday + "'s timetable!\n")
# if (tomorrow_day == "Saturday" or tomorrow_day == "Sunday"):
# bot.send_message(chat_id=update.message.chat_id, text="It's a weekend tomorrow!")
# return
query ={"day": tomorrow_day.upper()}
for x in timetable.find(query,{ "_id": 0,"course_name": 1, "course_code": 1, "type":1, "time":1, "location":1, "index":1}).sort("time"):
name = x["course_name"].title()
print(name)
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
name = re.sub('Amp;', '', name)
daily += ("====================\n")
daily += ("📌 " + x["course_code"].upper() + " " + name.title() + "\n")
daily += ("Type: " + x["type"].title() + "\n")
daily += ("⌛ " + x["time"] + "\n")
#daily += ("Index: " + x["index"] + "\n")
daily += ("📍 " + x["location"].upper() + "\n")
if (daily[-6:-1]=="able!"):
daily += "You have no lessons tomorrow!"
bot.send_message(chat_id=update.message.chat_id, text=daily)
dispatcher.add_handler(CommandHandler('tomorrow', tomorrow))
def today(bot,update):
today_day = (datetime.datetime.today().strftime('%a'))
today_longday = (datetime.datetime.today().strftime('%A'))
daily = ""
daily += "📝 Here's your timetable for today!\n"
daily += "====================\n"
daily += (today_day + "'s timetable!\n")
query ={"day": today_day.upper()}
for x in timetable.find(query,{ "_id": 0,"course_name": 1, "course_code": 1, "type":1, "time":1, "location":1}).sort("time"):
name = x["course_name"].title()
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
name = re.sub('Amp;', '', name)
daily += ("====================\n")
daily += ("📌 " + x["course_code"].upper() + " " + name.title() + "\n")
daily += ("Type: " + x["type"].title() + "\n")
daily += ("⌛ " + x["time"] + "\n")
daily += ("📍 " + x["location"].upper() + "\n")
if (daily[-6:-1]=="able!"):
daily += "You have no lessons today!"
bot.send_message(chat_id=update.message.chat_id, text=daily)
dispatcher.add_handler(CommandHandler('today', today))
def timetable1(bot,update):
days_of_week=['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
days_short_week=["MON","TUE","WED","THU","FRI","SAT","SUN"]
weekly = ""
weekly += "📝 Here's your weekly timetable!\n"
weekly += ("====================\n")
for x in range(len(days_of_week)):
day_classes = timetable.find({"day": days_short_week[x]},{ "_id": 0,"course_name": 1, "course_code":1, "type":1, "time":1, "location":1, "day":1, "day":1}).sort([("day", 1), ("time", 1)])
weekly += ("🗓 " + days_of_week[x].title() + "\n")
for y in day_classes:
name = y["course_name"].title()
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
name = re.sub('Amp;', '', name)
weekly += ("---------------------\n")
weekly += ("📌 " + y["course_code"].upper() + " " + name.title() + "\n")
weekly += ("Type: " + y["type"].title() + "\n")
weekly += ("⌛ " + y["time"] + "\n")
weekly += ("📍 " + y["location"].upper() + "\n")
if (weekly[-4:-1]=="day"):
weekly += ("You have no lessons on this day!\n")
weekly += ("====================\n")
bot.send_message(chat_id=update.message.chat_id, text=weekly)
dispatcher.add_handler(CommandHandler('timetable', timetable1))
def tomorrow1(bot,job):
print("1")
tomorrow_day = (datetime.datetime.today()+ datetime.timedelta(days=1)).strftime('%a')
tomorrow_longday = (datetime.datetime.today()+ datetime.timedelta(days=1)).strftime('%A')
daily = ""
daily += "📝 Here's your timetable for tomorrow!\n"
daily += "====================\n"
daily += (tomorrow_longday + "'s timetable!\n")
query ={"day": tomorrow_day.upper()}
for x in timetable.find(query,{ "_id": 0,"course_name": 1, "course_code": 1, "type":1, "time":1, "location":1, "index":1}).sort("time"):
name = x["course_name"].title()
print(name)
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
if (name[-1] == "*" or name[-1] == "#" or name[-1] == "^"):
name = name[:-1]
name = re.sub('Amp;', '', name)
daily += ("====================\n")
daily += ("📌 " + x["course_code"].upper() + " " + name.title() + "\n")
daily += ("Type: " + x["type"].title() + "\n")
daily += ("⌛ " + x["time"] + "\n")
daily += ("📍 " + x["location"].upper() + "\n")
if (daily[-6:-1]=="able!"):
daily += "You have no lessons tomorrow!"
bot.send_message(chat_id=job.context, text=daily)
def start_reminders(bot,update,job_queue):
bot.send_message(chat_id=update.message.chat_id, text = 'Starting daily reminders!')
job.run_daily(tomorrow1,datetime.time(hour=22, minute=30, second=0),days=(0,1,2,3,4,5,6), context = update.message.chat_id)
dispatcher.add_handler(CommandHandler('reminders', start_reminders, pass_job_queue=True))
def help1(bot,update):
help_string = "".join(messages.help_text)
bot.send_message(chat_id=update.message.chat_id, text=help_string)
bot.send_message(chat_id=update.message.chat_id, text=daily)
dispatcher.add_handler(CommandHandler('help', help1))
def unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="😞 Sorry, I didn't understand that command.")
dispatcher.add_handler(MessageHandler(Filters.command, unknown))
updater.idle()