Skip to content

Commit 0fe7f8f

Browse files
committed
fetch meal rss feed
1 parent 3228f1a commit 0fe7f8f

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

Package.resolved

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{
22
"object": {
33
"pins": [
4+
{
5+
"package": "FeedKit",
6+
"repositoryURL": "https://github.com/nmdias/FeedKit.git",
7+
"state": {
8+
"branch": null,
9+
"revision": "68493a33d862c33c9a9f67ec729b3b7df1b20ade",
10+
"version": "9.1.2"
11+
}
12+
},
413
{
514
"package": "HTMLString",
615
"repositoryURL": "https://github.com/alexaubry/HTMLString.git",

Package.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ let package = Package(
1818
dependencies: [
1919
.package(url: "https://github.com/alexaubry/HTMLString.git", from: "5.0.0"),
2020
.package(url: "https://github.com/sharplet/Regex.git", from: "2.1.0"),
21+
.package(url: "https://github.com/nmdias/FeedKit.git", from: "9.1.2"),
2122
],
2223
targets: [
2324
.target(
2425
name: "EmealKit",
25-
dependencies: ["HTMLString", "Regex"]),
26+
dependencies: ["HTMLString", "Regex", "FeedKit"]),
2627
.testTarget(
2728
name: "EmealKitTests",
2829
dependencies: ["EmealKit"]),

Sources/EmealKit/Mensa/Meal.swift

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public struct Meal: Identifiable, Equatable, Decodable {
1111
public var image: URL
1212
public var url: URL
1313

14+
public var isSoldOut: Bool?
15+
1416
private enum CodingKeys: String, CodingKey {
1517
case id
1618
case name

Sources/EmealKit/Mensa/MealFeed.swift

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import Foundation
2+
import FeedKit
3+
import os.log
4+
5+
extension Meal {
6+
public struct RSSMeal {
7+
public let title: String
8+
public let description: String
9+
public let guid: String
10+
public let link: String
11+
public let author: String
12+
13+
func matches(meal: Meal) -> Bool {
14+
return link.contains(String(meal.id))
15+
}
16+
}
17+
18+
public static func rssData(session: URLSession = .shared) async throws -> [RSSMeal] {
19+
let feedURL = URL(string: "https://www.studentenwerk-dresden.de/feeds/speiseplan.rss")!
20+
let parser = FeedParser(URL: feedURL)
21+
return try await withCheckedThrowingContinuation { continuation in
22+
parser.parseAsync { result in
23+
switch result {
24+
case .success(let feed):
25+
guard (feed.rssFeed?.title?.contains("von heute") ?? false) else {
26+
Logger.emealKit.error("Wrong feed?")
27+
continuation.resume(returning: [])
28+
return
29+
}
30+
guard let items = feed.rssFeed?.items else {
31+
Logger.emealKit.error("No feed items found")
32+
continuation.resume(returning: [])
33+
return
34+
}
35+
let meals = items.compactMap { item -> RSSMeal? in
36+
guard let title = item.title,
37+
let description = item.description,
38+
let guid = item.guid?.value,
39+
let link = item.link,
40+
let author = item.author
41+
else {
42+
return nil
43+
}
44+
return RSSMeal(
45+
title: title,
46+
description: description,
47+
guid: guid,
48+
link: link,
49+
author: author
50+
)
51+
}
52+
continuation.resume(returning: meals)
53+
case .failure(let error):
54+
continuation.resume(throwing: error)
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)