Skip to content

Commit

Permalink
Merge pull request #8 from Draculente/7-split-price
Browse files Browse the repository at this point in the history
✨ Add priceByGroup
  • Loading branch information
Draculente authored Nov 1, 2023
2 parents 83bf3ab + f92115c commit aed9526
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ export interface Allergen {
export interface Meal {
name: string;
price: string;
priceByGroup: PriceByGroup;
allergens: Allergen[];
}

export interface PriceByGroup {
students: number;
employees: number;
guests: number;
}

enum HasError {
HAS_ERROR = "has_error",
NO_ERROR = "no_error",
Expand Down Expand Up @@ -131,6 +138,25 @@ function getMealsByDate(document: Document, date: Date): Opt<Element> {
return opt(document.querySelector(`[data-day="${isoDate}"]:not(.mb_day)`));
}

// Funktion zum Extrahieren der Preise für die verschiedenen Gruppen
function getPriceByGroup(price: string): PriceByGroup {
const priceByGroup = {
students: 0,
employees: 0,
guests: 0,
};

const priceArray = price.replaceAll("€", "").replaceAll(",", ".").split("/");

if (priceArray.length === 3) {
priceByGroup.students = parseFloat(priceArray[0].trim());
priceByGroup.employees = parseFloat(priceArray[1].trim());
priceByGroup.guests = parseFloat(priceArray[2].trim());
}

return priceByGroup;
}

// Funktion zum Extrahieren der Informationen für jede Mahlzeit
function extractMealInformation(meals: Opt<Element>, allergens: Allergen[]): Partial<Day> {
if (meals.isNone()) return { open: true, meals: [], hasError: HasError.HAS_ERROR }; // Wenn keine Mahlzeiten gefunden wurden, wird ein leeres Array zurückgegeben
Expand Down Expand Up @@ -163,6 +189,7 @@ function extractMealInformation(meals: Opt<Element>, allergens: Allergen[]): Par
.orElse("Error getting name"); // Den Namen der Mahlzeit extrahieren und HTML-Tags entfernen

const price = opt(mealInfo.querySelector(".menu_preis")?.textContent).map((price) => price.trim()).orElse("Error getting price"); // Den Preis der Mahlzeit extrahieren
const priceByGroup = getPriceByGroup(price);
const vegan = mealInfo.getAttribute("data-arten")?.includes("vn") ?? false; // Überprüfen, ob die Mahlzeit vegan ist
const vegetarian = (mealInfo.getAttribute("data-arten")?.includes("ve") ?? false) || vegan; // Überprüfen, ob die Mahlzeit vegetarisch ist
const location = mealInfo.querySelector(".menu_art")?.textContent ?? "Error getting location"; // Den Standort der Mahlzeit extrahieren
Expand All @@ -177,6 +204,7 @@ function extractMealInformation(meals: Opt<Element>, allergens: Allergen[]): Par
name,
price,
vegan,
priceByGroup,
vegetarian,
location,
allergens: mealAllergens,
Expand Down

0 comments on commit aed9526

Please sign in to comment.