From 4754ce25fc1803d78a582beaae329b311fddd8c2 Mon Sep 17 00:00:00 2001 From: Kilian Koeltzsch Date: Wed, 13 Nov 2024 23:33:00 +0100 Subject: [PATCH] fetch rss data to augment meal data --- Sources/EmealKit/Mensa/MealFeed.swift | 4 ++++ Sources/EmealKit/Mensa/MensaAPI.swift | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Sources/EmealKit/Mensa/MealFeed.swift b/Sources/EmealKit/Mensa/MealFeed.swift index 776a644..7707f99 100644 --- a/Sources/EmealKit/Mensa/MealFeed.swift +++ b/Sources/EmealKit/Mensa/MealFeed.swift @@ -13,6 +13,10 @@ extension Meal { func matches(meal: Meal) -> Bool { return link.contains(String(meal.id)) } + + var isSoldOut: Bool { + title.lowercased().contains("ausverkauft") + } } public static func rssData(session: URLSession = .shared) async throws -> [RSSMeal] { diff --git a/Sources/EmealKit/Mensa/MensaAPI.swift b/Sources/EmealKit/Mensa/MensaAPI.swift index f6fe503..a2c2e9a 100644 --- a/Sources/EmealKit/Mensa/MensaAPI.swift +++ b/Sources/EmealKit/Mensa/MensaAPI.swift @@ -37,7 +37,17 @@ extension Meal { let (data, _) = try await session.data(from: URL.Mensa.meals(canteen: canteen, date: date)) let meals = try JSONDecoder().decode([Meal].self, from: data) Logger.emealKit.debug("Successfully fetched \(meals.count) meals") - return meals + + let feedItems = try await Self.rssData() + return meals.map { meal in + var meal = meal + let matchingItem = feedItems.first { $0.matches(meal: meal) } + if let matchingItem { + Logger.emealKit.debug("Found matching feeditem for \(meal.id)") + meal.isSoldOut = matchingItem.isSoldOut + } + return meal + } } catch (let error) { Logger.emealKit.error("Failed to fetch meal data: \(String(describing: error))") throw .other(error)