-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsjrmenu.html
184 lines (165 loc) · 7.34 KB
/
sjrmenu.html
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
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Menu</title>
<meta charset = "utf-8">
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js"></script>
<script src = "https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.min.js"></script>
<script src = "https://cdn.jsdelivr.net/npm/pdfjs-dist/build/pdf.min.js"></script>
<style>
html, body {
margin: 0px;
}
h2, p {
color: #000000;
font-family: Arial, sans-serif;
}
p {
font-size: 24px;
margin: 20px 20px 20px 60px;
line-height: 1;
}
#start {
margin: 20px;
line-height: 1;
font-size: 20px;
}
h1 {
font-family: Arial, sans-serif;
color: #008000;
font-size: 36px;
font-weight: bold;
margin: 20px;
}
a {
text-decoration: none;
color: #008000;
}
a:hover {
text-decoration: underline;
}
h2 {
font-size: 32px;
margin: 20px;
font-weight: normal;
}
mark {
color: #008000;
background-color: #ffffff;
font-weight: bold;
}
label {
margin: 20px 10px 20px 0px;
}
div {
margin: 0px;
}
</style>
</head>
<body onload = "brython (0)">
<noscript>
<p id = "start">JavaScript is required to use this website.</p>
</noscript>
<script type = "text/python">
from browser import document, ajax, html, window
from browser.local_storage import storage
from bisect import bisect
from datetime import datetime as d
import json
# (Brunch, Soup, Salad, Deli, Hot Entrée, Vegetarian Entrée, Hydration Station) for each day
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
items = ("Brunch", "Soup", "Salad", "Deli", "Hot Entrée", "Vegetarian Entrée", "Dinner")
VERSION = "3" # Affected by changes in the menu format
def rfind (text, values, end = ")>>"):
pointer = 0
for j in values:
pointer += text [pointer : ].find (j)
if pointer == -1:
return "N/A"
return " • ".join (i for i in text [pointer + len (values [-1]) : pointer + text [pointer : ].find (end)].split ("\\r\\n") if i)
def complete (p):
global keys, days, items, values, weekday
document.clear ()
text = [i ["str"] for i in p ["items"] if i ["str"].replace ("\uf0b5", "").strip ()]
transform = [i ["transform"] for i in p ["items"] if i ["str"].replace ("\uf0b5", "").strip ()]
columns = {}
for i, j in zip (text, transform):
columns.setdefault (str (j [4]), []).append ([i, j [5], j [4]])
columns = [sorted (v, key = lambda x: -x [1]) for k, v in sorted (columns.items (), key = lambda x: float (x [0])) if float (k) > 100] # TEMPORARY FIX - Magic number
titles, column = [], 0
while column < len (columns):
i = columns [column]
if any (k [1] > 520 for k in i): # TEMPORARY FIX - Magic number
title = columns.pop (column)
column -= 1
if any (k [1] > 540 for k in title):
titles.extend ([k [0] for k in title])
column += 1
document <= html.A (html.H1 (f'{" • ".join (titles)} (Click to Refresh)'), id = "title", href = "javascript:void (0)")
document ["title"].bind ("click", fetch_menu)
row_headers = ["water, and milk", "Daily Soup", "Salad Bar", "Deli Station", "Hot Entrée", "Vegetarian Entrée", "Dinner"]
row_heights = []
for i, j in zip (row_headers, row_headers [1 : ]):
a = transform [text.index (i)] [5]
b = transform [text.index (j)] [5]
row_heights.append ((float (a) + float (b)) / 2) # Average of the two heights
row_heights.reverse () # PDF coordinates start from bottom
col_headers = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
col_heights = [transform [text.index (i)] [4] for i in col_headers]
values = [[] for i in range (35)]
for i in columns:
for j in i:
values [(6 - bisect (row_heights, j [1])) * 5 + bisect (col_heights, j [2])].append (j [0])
values = [" • ".join (i) for i in values]
window.console.log ("Values: ", values)
select = html.P ("<mark>Display: </mark>", style = "margin: 20px;")
chkbox_list = json.loads (storage ["display_sel"]) if "display_sel" in storage and storage ["version"] == VERSION else None
check = lambda b: chkbox_list [b] if chkbox_list is not None else "checked"
for i in items:
chkbox = html.INPUT (Type = "checkbox", id = f"{i}_sel", checked = check (i))
chkbox.bind ("change", display)
select <= chkbox
select <= html.LABEL (i, For = f"{i}_sel")
document <= select
display (None)
def display (ev):
global keys, days, items, values, weekday
if "version" not in storage or storage ["version"] != VERSION:
storage.clear ()
storage ["version"] = VERSION
if "menu" in document:
del document ["menu"]
document <= html.DIV (id = "menu")
for j, i in enumerate (days):
if weekday == j:
document ["menu"] <= html.H2 (f"Today ({i})", style = "font-weight: bold; color: #008000;")
else:
document ["menu"] <= html.H2 (f"{i}")
chkbox_list = {}
for l, k in enumerate (items):
if document [f"{k}_sel"].checked:
document ["menu"] <= html.P (f"<mark>{k}</mark>: {values [l * len (days) + j]}")
chkbox_list [k] = document [f"{k}_sel"].checked
storage ["display_sel"] = json.dumps (chkbox_list)
document ["menu"] <= html.A (html.H2 ("View the menu as a PDF"), href = "https://sjr.myschoolapp.com/ftpimages/1091/download/download_5418715.pdf", target = "_blank")
def fetch_menu (ev):
global weekday
weekday = document.query.getvalue ("weekday")
if weekday:
weekday = int (weekday) - 1
else:
weekday = d.now ().weekday ()
document.clear ()
document <= html.P ("Fetching Menu...", id = "start")
pdf = window.pdfjsLib.getDocument ("https://api.codetabs.com/v1/proxy?quest=https://sjr.myschoolapp.com/ftpimages/1091/download/download_5418715.pdf").promise
def get_page (p):
return p.getPage (1)
pdf = pdf.then (lambda p: p.getPage (1))
pdf = pdf.then (lambda p: p.getTextContent ())
pdf = pdf.then (complete)
fetch_menu (None)
</script>
<script>
document.body.insertAdjacentHTML ("beforeend", `<p id = "start">Starting Brython...</p>`)
</script>
</body>