From 1ee58fa90dec344e232f7bd431990bf2c4fd8d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Barfu=C3=9F?= Date: Sat, 23 Dec 2023 01:38:42 +0100 Subject: [PATCH] +Mensa Scraper +Bundesland Scraper +bl route --- src/index.ts | 46 ++++++++++++++++++++++++------- src/stripper.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9eebe08..97b0ae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,32 +24,60 @@ app.get("/", (req: Request, res: Response) => { res.send("Mensa API"); }); -app.get("/api/:Ort/:Mensa?", (req: Request, res: Response) => { - if (req.params.Ort === null) { + +app.get("/api/bl/:Bundesland", (req: Request, res: Response) => { + if (req.params.Bundesland === undefined) { return res.send("Invalid request"); } - let cachedData = cache.get(); + let cachedData = cache.get("BL: "+req.params.Bundesland); if (cachedData !== null) { return res.send(cachedData); }else { - let url = baseUrl + req.params.Ort.toLowerCase(); + let url = baseUrl + req.params.Bundesland.toLowerCase()+".html"; fetch(url).then((data) => { let stripedData = null; - if (req.params.Mensa !== null) { - let stripedData = stripMensa(data); + if (data === null) { + return res.send("Invalid request"); + } + stripedData = stripCampus(data); + cache.set("BL: "+req.params.Bundesland, stripedData); + res.send(stripedData); + }); + } +}); +app.get("/api/:Location/:Mensa?", (req: Request, res: Response) => { + if (req.params.Location === undefined) { + return res.send("Invalid request"); + } + let cachedData = cache.get(req.params.Mensa ?? req.params.Location); + if (cachedData !== null) { + return res.send(cachedData); + }else { + let url = baseUrl + req.params.Location.toLowerCase(); + if (req.params.Mensa !== undefined) { + url += "/" + req.params.Mensa.toLowerCase(); + } + fetch(url).then((data) => { + let stripedData = null; + if (data === null) { + return res.send("Invalid request"); + } + if (req.params.Mensa !== undefined) { + stripedData = stripMensa(data); }else { - let stripedData = stripCampus(data); + stripedData = stripCampus(data); } - cache.set(req.params.Ort, stripedData); + cache.set(req.params.Mensa ?? req.params.Location, stripedData); res.send(stripedData); }); } }); + app.get("/api", (req: Request, res: Response) => { - res.send("/api/:Ort/:Mensa?"); + res.send("/api/:Location/:Mensa?"); }); app.listen(port, () => { diff --git a/src/stripper.ts b/src/stripper.ts index a9149e6..8e1163e 100644 --- a/src/stripper.ts +++ b/src/stripper.ts @@ -11,6 +11,13 @@ class Campus { addMensa(mensa: Mensa) { this.mensen.push(mensa); } + toString() { + let str = this.name + "\n"; + this.mensen.forEach((elem) => { + str += " -> " + elem.name + " | " + elem.url + "\n"; + }); + return str; + } } class Mensa { @@ -23,6 +30,39 @@ class Mensa { } } +class Menu { + name: string; + meals: Meal[]; + + constructor(name: string) { + this.name = name; + this.meals = []; + } + addMeal(meal: Meal) { + this.meals.push(meal); + } + + toString() { + let str = this.name + "\n"; + this.meals.forEach((elem) => { + str += " -> " + elem.name + " | " + elem.price + " | " + elem.atributs + "\n"; + }); + return str; + } +} + +class Meal { + name: string; + price: string; + atributs: string[]; + + constructor(name: string, price: string, atributs: string[] ) { + this.name = name; + this.price = price; + this.atributs = atributs; + } +} + export function stripCampus(html: string): JSON { const $ = cheerio.load(html); @@ -45,6 +85,36 @@ export function stripCampus(html: string): JSON { export function stripMensa(html: string): string { const $ = cheerio.load(html); - return "Mensa"; + + let menus :Menu[] = []; + + const $menu = $('.aw-meal-category'); + $menu.each((i, elem) => { + const menu = new Menu($(elem).find('h3').text()); + + //meal + $(elem).find('.aw-meal').each((i, elem) => { + const name = $(elem).find('.aw-meal-description').text(); + const price = $(elem).find('.aw-meal-price').text(); + + //delete last time served + $(elem).find('.aw-meal-attributes').find('.hidden-md').remove(); + + const atributs :string[] = $(elem).find('.aw-meal-attributes').text().split(" "); + + atributs.forEach((elem, i) => { + elem = elem.trim(); + atributs[i] = elem; + }); + + + menu.addMeal(new Meal(name, price, atributs)); + }); + menus.push(menu); + }); + if (menus.length === 0) { + return JSON.parse("No usable Data found"); + } + return JSON.parse(JSON.stringify(menus)); }