-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaketinyjson.html
More file actions
156 lines (145 loc) · 4.83 KB
/
maketinyjson.html
File metadata and controls
156 lines (145 loc) · 4.83 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Menu Data Extractor (All Dining Halls)</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 40px;
background: #f4f6f9;
color: #2c3e50;
}
h1 { color: #2c3e50; }
select, button {
padding: 10px 14px;
font-size: 16px;
margin-right: 10px;
border-radius: 6px;
border: 1px solid #ccc;
}
button {
background: #27ae60;
color: white;
border: none;
cursor: pointer;
}
button:hover { background: #219653; }
#output {
margin-top: 20px;
background: white;
padding: 16px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
max-height: 700px;
overflow-y: auto;
font-family: monospace;
font-size: 0.9em;
}
.download-btn, .copy-btn {
background: #3498db;
margin-top: 10px;
margin-right: 10px;
display: inline-block;
padding: 8px 12px;
border-radius: 6px;
text-decoration: none;
color: white;
font-size: 0.9em;
cursor: pointer;
}
.download-btn:hover, .copy-btn:hover { background: #2980b9; }
.copy-btn.copied { background: #27ae60; }
.error {
color: #e74c3c;
background: #fdf2f2;
padding: 12px;
border-radius: 6px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Menu Data Extractor (All Dining Halls)</h1>
<p>Extracts only: <code>name, calories, protein, allergens, isVegan</code>.<br>
<strong>Filtered out:</strong> <code>Bakery, Dessert, Beverages, Bliss</code></p>
<button id="fetchAllBtn">Fetch All Dining Halls</button>
<div id="output">Result will appear here...</div>
<script>
const API_KEY = "68717828-b754-420d-9488-4c37cb7d7ef7";
const diningData = [
{ name: "BARH", location_id: "76929003", menu_id: "153626" },
{ name: "Commons", location_id: "76929001", menu_id: "153148" },
{ name: "Russell Sage", location_id: "76929002", menu_id: "153157" },
{ name: "Blitman", location_id: "76929015", menu_id: "153702" } // make sure this menu_id is correct
];
const EXCLUDED_GROUPS = ["Bakery","Dessert","Beverages","Bliss"];
function extractItem(item) {
return {
name: item.formalName,
calories: item.calories || null,
protein: item.protein ? `${item.protein}g` : null,
allergens: item.allergens ? item.allergens.map(a => a.name) : [],
isVegan: !!item.isVegan
};
}
function buildFilteredJson(data) {
const result = {};
const meals = Array.isArray(data) ? data : (data.meals || []);
meals.forEach(meal => {
const mealName = meal.name;
result[mealName] = {};
meal.groups.forEach(group => {
const groupName = group.name;
if (EXCLUDED_GROUPS.some(ex => ex.toLowerCase() === groupName.toLowerCase())) return;
result[mealName][groupName] = [];
group.items.forEach(item => result[mealName][groupName].push(extractItem(item)));
});
});
return result;
}
function downloadJson(data, filename) {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
async function fetchDiningHallMenu(dhall) {
const url = `https://api-prd.sodexomyway.net/v0.2/data/menu/${dhall.location_id}/${dhall.menu_id}?date=2025-11-21`;
const resp = await fetch(url, {
headers: {
"accept": "application/json",
"content-type": "application/json",
"api-key": API_KEY
}
});
if (!resp.ok) throw new Error(`${dhall.name} HTTP ${resp.status}`);
const fullData = await resp.json();
return buildFilteredJson(fullData);
}
document.getElementById("fetchAllBtn").addEventListener("click", async () => {
const output = document.getElementById("output");
output.textContent = "Fetching all dining halls...";
const allMenus = {};
for (const dhall of diningData) {
try {
const menu = await fetchDiningHallMenu(dhall);
allMenus[dhall.name] = menu;
} catch (err) {
allMenus[dhall.name] = { error: err.message };
}
}
output.innerHTML = `<pre>${JSON.stringify(allMenus, null, 2)}</pre>`;
// Download button
const downloadBtn = document.createElement('div');
downloadBtn.innerHTML = 'Download All Menus JSON';
downloadBtn.className = 'download-btn';
downloadBtn.onclick = () => downloadJson(allMenus, 'all_dining_halls_menu_2025-11-21.json');
output.appendChild(downloadBtn);
});
</script>
</body>
</html>