~shreyasminocha/rice-dining-api

4a75efd882ede31c93142dc2aaa7b0a708e3b1fa — Shreyas Minocha 2 years ago 9e364a1
Move utils to modules
3 files changed, 27 insertions(+), 19 deletions(-)

A src/meal-to-data.js
A src/pageify.js
M src/scrape.js
A src/meal-to-data.js => src/meal-to-data.js +10 -0
@@ 0,0 1,10 @@
function mealToData(meal) {
	return {
		meal: meal.querySelector('.title').textContent,
		menu: Array
			.from(meal.querySelectorAll('.menu-items .mitem'))
			.map((mitem) => mitem.textContent),
	}
}

export default mealToData;

A src/pageify.js => src/pageify.js +12 -0
@@ 0,0 1,12 @@
function pageify(body) {
	return `
		<!DOCTYPE html>
		<html>
		<body>
			${body}
		</body>
		</html>
	`;
}

export default pageify;

M src/scrape.js => src/scrape.js +5 -19
@@ 1,31 1,17 @@
import got from 'got';
import jsdom from 'jsdom';
import camelcase from 'camelcase';
import mealToData from './meal-to-data.js';
import pageify from './pageify.js';

async function getDailyMenu(servery) {
	const res = await got(`https://web-api3.rice.edu/static/${servery}-menu-new.js`);
	const menuHtml = eval(res.body + `${camelcase(servery)}MenuTemplate()`); // eval bad
	const dom = new jsdom.JSDOM(`
		<!DOCTYPE html>
		<html>
		<body>
			${menuHtml}
		</body>
		</html>
	`);
	const dom = new jsdom.JSDOM(pageify(menuHtml));
	const doc = dom.window.document;
	const mealsDom = doc.querySelectorAll('.inside-daily-menu .item:not(.legend)');

	const mealToData = (meal) => ({
		meal: meal.querySelector('.servery-title').textContent,
		menu: Array
			.from(meal.querySelectorAll('.menu-items .mitem'))
			.map((mitem) => mitem.textContent),
	});
	const meals = Array
		.from(doc.querySelectorAll('.inside-daily-menu .item:not(.legend)'))
		.map(mealToData);

	return meals;
	return Array.from(mealsDom).map(mealToData);
}

console.log(await getDailyMenu('seibel-servery'));