+Mensa Scraper +Bundesland Scraper +bl route

This commit is contained in:
Jan Barfuß 2023-12-23 01:38:42 +01:00
parent d4520dfbd0
commit 1ee58fa90d
2 changed files with 108 additions and 10 deletions

View File

@ -24,32 +24,60 @@ app.get("/", (req: Request, res: Response) => {
res.send("Mensa API"); 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"); return res.send("Invalid request");
} }
let cachedData = cache.get(); let cachedData = cache.get("BL: "+req.params.Bundesland);
if (cachedData !== null) { if (cachedData !== null) {
return res.send(cachedData); return res.send(cachedData);
}else { }else {
let url = baseUrl + req.params.Ort.toLowerCase(); let url = baseUrl + req.params.Bundesland.toLowerCase()+".html";
fetch(url).then((data) => { fetch(url).then((data) => {
let stripedData = null; let stripedData = null;
if (req.params.Mensa !== null) { if (data === null) {
let stripedData = stripMensa(data); 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 { }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); res.send(stripedData);
}); });
} }
}); });
app.get("/api", (req: Request, res: Response) => { app.get("/api", (req: Request, res: Response) => {
res.send("/api/:Ort/:Mensa?"); res.send("/api/:Location/:Mensa?");
}); });
app.listen(port, () => { app.listen(port, () => {

View File

@ -11,6 +11,13 @@ class Campus {
addMensa(mensa: Mensa) { addMensa(mensa: Mensa) {
this.mensen.push(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 { 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 { export function stripCampus(html: string): JSON {
const $ = cheerio.load(html); const $ = cheerio.load(html);
@ -45,6 +85,36 @@ export function stripCampus(html: string): JSON {
export function stripMensa(html: string): string { export function stripMensa(html: string): string {
const $ = cheerio.load(html); 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));
} }