-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from kiliankoe/sold-out-meals
Fetch sold out state for meals from RSS data
- Loading branch information
Showing
13 changed files
with
920 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Foundation | ||
import FeedKit | ||
import os.log | ||
|
||
extension Meal { | ||
public struct RSSMeal { | ||
public let title: String | ||
public let description: String | ||
public let guid: String | ||
public let link: String | ||
public let author: String | ||
|
||
func matches(meal: Meal) -> Bool { | ||
return link.contains(String(meal.id)) | ||
} | ||
|
||
var isSoldOut: Bool { | ||
title.lowercased().contains("ausverkauft") | ||
} | ||
} | ||
|
||
public static func rssData() async throws -> [RSSMeal] { | ||
let feedURL = URL(string: "https://www.studentenwerk-dresden.de/feeds/speiseplan.rss")! | ||
let parser = FeedParser(URL: feedURL) | ||
return try await withCheckedThrowingContinuation { continuation in | ||
parser.parseAsync { result in | ||
switch result { | ||
case .success(let feed): | ||
guard (feed.rssFeed?.title?.contains("von heute") ?? false) else { | ||
Logger.emealKit.error("Wrong feed?") | ||
continuation.resume(returning: []) | ||
return | ||
} | ||
guard let items = feed.rssFeed?.items else { | ||
Logger.emealKit.error("No feed items found") | ||
continuation.resume(returning: []) | ||
return | ||
} | ||
let meals = items.compactMap { item -> RSSMeal? in | ||
guard let title = item.title, | ||
let description = item.description, | ||
let guid = item.guid?.value, | ||
let link = item.link, | ||
let author = item.author | ||
else { | ||
return nil | ||
} | ||
return RSSMeal( | ||
title: title, | ||
description: description, | ||
guid: guid, | ||
link: link, | ||
author: author | ||
) | ||
} | ||
continuation.resume(returning: meals) | ||
case .failure(let error): | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
|
||
public protocol URLSessionProtocol { | ||
func data(from url: URL) async throws -> (Data, URLResponse) | ||
} | ||
|
||
extension URLSession: URLSessionProtocol { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.