-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheScrape2.py
392 lines (344 loc) · 14.9 KB
/
TheScrape2.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#TheScrape2 IS NOW A RELIC OF THE PAST (NO LONGER IN USE)
#She is very slow but is accurate i think
#Kept around as a last resort if TheScrape3 has mistakes
from datetime import *
import time
from pytz import timezone
TIMEZONE = timezone('Australia/Sydney')
from bs4 import BeautifulSoup # Importing BeautifulSoup class from the bs4 module
from killswitch import read_custom_message
#define the dino times here used throughout
from bot_constants import (bassertimes, dinotimes)
def checkForDino(message, con, value):
week = getmenuweek()
response = ""
day, current_day, column, week = getDay(message, week) #checks for days and creates current_day
#current_day: day of week 0-6 inclusive
#day_value: day of week 1-7 inclusive
#day: name of the day e.g. monday, wednesday, tomorrow, today
#week: week of cycle (1-4)
time = datetime.now(TIMEZONE).time().hour
if value == "dino": #handling if meal is non-specified
if "time" in message:
response = response + dinotimes
elif day == "Tomorrow":
response = response + (f"Dino Breakfast Tomorrow: \n")
day_value = current_day + 1
response = response + breakfastmenu(day_value, column, week)
elif time < 10:
response = response + (f"Breakfast {day}: \n")
day_value = current_day + 1
response = response + breakfastmenu(day_value, column, week)
elif time < 14:
response = response + (f"Lunch {day}: \n")
day_value = current_day + 1
response = response + lunchmenu(day_value, column, week)
elif time < 19:
response = response + (f"Dinner {day}: \n")
day_value = int(datetime.now(TIMEZONE).weekday()) + 1
response = response + dinnermenu(day_value, column, week)
else:
response = response + (f"Breakfast Tomorrow: \n")
day_value = int(datetime.now(TIMEZONE).weekday()) + 2 #this is 2 since early +1 was done cause "tomorrow" was in message
response = response + breakfastmenu(day_value, column, week)
elif value == "breakfast":
if "time" in message:
response = response + "Basser breakfast is at " + bassertimes["breakfast"]
elif time > 14 and day == "Today": #after 2pm will give the breakfast for the next day
day = "Tomorrow"
response = response + (f"Breakfast {day}: \n")
day_value = current_day + 2
current_day += 1
time = 0
if current_day==7:
if week==4:
week = 1
print(str(week) + " week")
column = 1
else:
week = week + 1
print(str(week) + "week")
column = 1
response = response + breakfastmenu(day_value, column, week)
else:
response = response + (f"Breakfast {day}: \n")
day_value = current_day + 1
response = response + breakfastmenu(day_value, column, week)
elif value == "lunch":
if "time" in message:
response = response + "Basser lunch is at " + bassertimes["lunch"]
elif time > 17 and day == "Today": #after 5pm will give the lunch for the next day
day = "Tomorrow"
response = response + (f"Lunch {day}: \n")
day_value = current_day + 2
current_day += 1
time = 0
if current_day==7:
if week==4:
week = 1
print(str(week) + " week")
column = 1
else:
week = week + 1
print(str(week) + "week")
column = 1
response = response + lunchmenu(day_value, column, week)
else:
response = response + (f"Lunch {day}: \n")
day_value = current_day + 1
response = response + lunchmenu(day_value, column, week)
elif value == "dinner":
if "time" in message:
response = response + "Basser dinner is at " + bassertimes["dinner"]
elif time > 20 and day == "Today": #after 8pm will give the value for the next day
day = "Tomorrow"
response = response + (f"Dinner {day}: \n")
day_value = current_day + 2
current_day += 1
time = 0
if current_day==7:
if week==4:
week = 1
print(str(week) + " week")
column = 1
else:
week = week + 1
print(str(week) + "week")
column = 1
response = response + dinnermenu(day_value, column, week)
else:
response = response + (f"Dinner {day}: \n")
day_value = current_day + 1
response = response + dinnermenu(day_value, column, week)
if "time" not in message:
note = addnote(con, value, day)
else:
note = None
if note is not None:
response = response + str(note)
return response
def getDay(message, week): #here is where we get the day and current_day and sometimes week
column = ""
current_day = datetime.now(TIMEZONE).weekday()
day = "Today"
#See if user is asking about tomorrow
if "tomorrow" in message or "tmrw" in message or "tomoz" in message or "tmoz" in message:
day = "Tomorrow"
current_day+=1
time = 0
if current_day==7:
if week==4:
week = 1
print(str(week) + " week")
column = 1
else:
week = week + 1
print(str(week) + "week")
column = 1
#check if user has asked about a day of the week
elif checkForDay(message):
print("day found")
week_days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
if current_day > int(checkForDay(message)):
print(str(week) + " week")
if str(week)==str("4"):
week = 1
print(str(week) + " week")
else:
week = week + 1
print(str(week) + " week")
current_day = int(checkForDay(message))
day = str(week_days[int(checkForDay(message))])
else:
current_day = int(checkForDay(message))
day = str(week_days[int(checkForDay(message))])
#otherwise must be today: and day and current_day are not updated from todays value
return day, current_day, column, week
# def wholedaymenu(message):
# global day_value
# global column
# #global Range
# global page
# global week
# day, current_day, week, column = getDay(message, week)
# if day == "Today":
# response = response + (f"Breakfast {day}: \n")
# day_value = current_day + 1
# response = response + breakfastmenu() + "\n\n"
# response = response + (f"Lunch {day}: \n")
# response = response + lunchmenu() + "\n\n"
# response = response + (f"Dinner {day}: \n")
# response = response + dinnermenu() + "\n\n"
# elif day == "Tomorrow":
# elif checkForDay(message):
def breakfastmenu(day_value, column, week):
page = str((2*(week-1)+1))
Range = int("2")
response = ""
for i in range(0,Range):
try:
header = ""
column = 0
header = header + columnlist(page, column, Range)[i]
header = addemojis(header)
content = ""
if day_value == 8:
column = 1
else:
column = day_value
content = content + columnlist(page, column, Range)[i]
if content != "":
content = addemojiscontent(content)
response = "".join([response,str(header).title(),": \n",str(content).capitalize(),"\n\n"])
except IndexError:
print('NOK')
return response
def lunchmenu(day_value, column, week):
page = str((2*(week-1)+1.5))
Range = int("3")
response = ""
for i in range(0,Range):
try:
header = ""
column = 0
header = header + columnlist(page, column, Range)[i]
header = addemojis(header)
content = ""
if day_value == 8:
column = 1
else:
column = day_value
content = content + columnlist(page, column, Range)[i]
if content != "":
content = addemojiscontent(content)
response = "".join([response,str(header).title(),": \n",str(content).capitalize(),"\n\n"])
except IndexError:
print('NOK')
return response
def dinnermenu(day_value, column, week):
#print("dinnermenu")
page = str((2*(week-1)+2))
Range = int("8")
response = ""
for i in range(1,Range):
try:
header = ""
column = 0 #set to first column to get header
header = "".join([header,columnlist(page, column, Range)[i]])
header = addemojis(header)
content = ""
if day_value == 8:
column = 1
else:
column = day_value
content = content + columnlist(page, column, Range)[i]
if header == "vegetables":
content = ""
if content != "":
content = addemojiscontent(content)
response = "".join([response,str(header).title(),": \n",str(content).capitalize(),"\n\n"])
#response = response + str(header).title() + ": \n" + str(content).capitalize() + "\n\n"
except IndexError:
print('NOK')
return response
def addemojis(header):
header = header.replace("salad", u"salad \U0001F957")
if "vegetarian option" in header:
header = header.replace("vegetarian option", u"vegetarian option \U0001F331")
else:
header = header.replace("vegetarian", u"vegetarian \U0001F331")
header = header.replace("main course", u"main course \U0001F37D").replace("hot option", u"hot option \U0001F37D")
header = header.replace("residential breakfast", u"residential breakfast \U0001f95e")
header = header.replace("soup", u"soup \U0001f372")
header = header.replace("the dessert station", u"the dessert station \U0001f370")
header = header.replace("additional vegetables", u"additional vegetables \U0001F966")
return header
def addemojiscontent(content):
#content = content.replace("egg", u"egg \U0001F95A")
content = content.replace("pancakes", u"pancakes \U0001f95e")
content = content.replace("pizza", u"pizza \U0001f355")
content = content.replace("sushi", u"sushi \U0001f363")
content = content.replace("chicken", u"chicken \U0001F357")
content = content.replace("honey", u"honey \U0001F36F")
return content
# def addemojisresponse(response):
# if "vegetarian option" in response:
# response = response.replace("vegetarian option", u"vegetarian option \U0001F331")
# else:
# response = response.replace("vegetarian", u"vegetarian \U0001F331")
# response = response.replace("main course", u"main course \U0001F37D").replace("hot option", u"hot option \U0001F37D")
# response = response.replace("residential breakfast", u"residential breakfast \U0001f95e")
# response = response.replace("soup", u"soup \U0001f372")
# response = response.replace("the dessert station", u"the dessert station \U0001f370")
# response = response.replace("additional vegetables", u"additional vegetables \U0001F966")
# response = response.replace("pancakes", u"pancakes \U0001f95e")
# response = response.replace("pizza", u"pizza \U0001f355")
# response = response.replace("sushi", u"sushi \U0001f363")
# response = response.replace("chicken", u"chicken \U0001F357")
# response = response.replace("honey", u"honey \U0001F36F")
# return response
def columnlist(page, column, Range): #gets the info from each column as a list
rowcontents = []
for i in range(0,Range):
row = i
content = getinfo(page, row, column)
rowcontents.append(content)
return rowcontents
def addnote(con, value, day):
meal = value
if day == "Today": #makes sure we are talking about the actual day e.g. not tommorrow or the coming wednesday
try:
note = "".join(["Note:\n",read_custom_message(meal, con).capitalize()])
except AttributeError:
note = None
else: #otherwise there is no note
note = None
return note
def getinfo(page, row, column):
#-----------------------Opening the HTML file--------------------------#
HTMLFile = open(str("menu/" + page + ".html"), "r") #try putting in func.
#print(str(HTMLFile))
# Reading the file
index = HTMLFile.read()
# Creating a BeautifulSoup object and specifying the parser
soup = BeautifulSoup(index, 'lxml')
# Using the prettify method to modify the code
#print(soup.body.prettify())
#print(soup.title) #prints the table title if it has one
menu_table = soup.find("table", attrs={"class": "dataframe"})
menu_table_data = menu_table.tbody.find_all("tr") # contains 2 rows
#---------------------------------------------------------------------#
info = []
for td in menu_table_data[row].find_all("td"):
if td is not None:
#plain_text = str(td).replace(r"– \n \n","- ").replace(r" \n \n", ", ").replace(r"– \n", "- ").replace(r"\n–","-").replace(", \n",", ").replace(r" \n ","").replace(r" \n",", ").replace(r"\n",", ")
stuff = str(td).replace("<td>","").replace("</td>","").replace("amp;","").replace(r"\n","")
#plain_text = stuff.strip(""",.;:-¢"'�_!?I•,L4J£<~""") #removes all weird artifacts
info.append(stuff)
else:
print("none!")
return info[column]
def getmenuweek():
TIMEZONE = timezone('Australia/Sydney')
x = datetime.datetime.now(TIMEZONE)
week = (int(x.strftime("%W"))+1) #plus one changes the cycle to match the dino cycle
menuweek = (week)%4+1 #this cheeky +1 changes range from (0-3 to 1-4)
print(menuweek)
return menuweek
def checkForDay(message): #check of day of week specified
day = ""
if "monday" in message or " mon" in message or "mon " in message:
day = str('0')
elif "tuesday" in message or " tues" in message or "tues " in message:
day = 1
elif "wednesday" in message or " wed" in message or "wed " in message:
day = 2
elif "thursday" in message or " thur" in message or "thur " in message or " thurs" in message or "thurs " in message:
day = 3
elif "friday" in message or " fri" in message or "fri " in message:
day = 4
elif "saturday" in message or " sat" in message or "sat " in message:
day = 5
elif "sunday" in message or " sun" in message or "sun " in message:
day = 6
return day