Der zweite Server. Eigenes Target neben PlexKit, nicht darin — die beiden teilen die neutrale Schicht und sonst nichts. Die Anmeldung geht zuerst über **Quick Connect**: die App zeigt einen sechsstelligen Code, den man in einem bereits angemeldeten Client bestätigt. Damit gilt hier dieselbe Zusage wie bei Plex — das Passwort erreicht Flixy nie. Benutzername und Passwort bleiben als Rückfall, falls der Server Quick Connect abgeschaltet hat; dann hält die App das Passwort für die Dauer einer Anfrage und speichert es nirgends. Zwei Stellen, an denen sich eine Übersetzung verrechnet, und die deshalb Tests haben: Jellyfin zählt Zeiten in Ticks zu hundert Nanosekunden. Eine Null zu viel, und aus 44 Minuten werden siebeneinhalb Stunden. Und es meldet die **ungesehenen** Folgen, nicht die gesehenen. Ungeprüft übernommen stünde bei einer fast fertigen Serie „9 von 24" statt „15 von 24". Dass die Adressen den Token tragen, steht hier von Anfang an — bei Plex hat genau das tagelang gekostet, weil AVPlayer keine Kopfzeilen mitschickt. Zwei Fähigkeiten sind ausdrücklich nicht da: Sterne-Bewertungen kennt Jellyfin nicht, nur Daumen hoch und runter. Und die Adresse einer verkleinerten Fassung gibt es erst nach einer Anfrage — sie zu erfinden hieße, eine Adresse anzubieten, die nicht antwortet. Die Startseite ist nachsichtig gebaut: fällt „Als Nächstes" aus, bleibt deshalb nicht die ganze Seite leer. Die App bietet Jellyfin noch nicht an — das ist der nächste Schritt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
361 lines
14 KiB
Swift
361 lines
14 KiB
Swift
import Foundation
|
|
import MediaKit
|
|
|
|
/// Jellyfin als `MediaBackend`.
|
|
public struct JellyfinBackend: MediaBackend {
|
|
private let client: JellyfinClient
|
|
private let account: JellyfinAccount
|
|
private let images = JellyfinImagePaths()
|
|
public let serverName: String
|
|
|
|
public init(client: JellyfinClient, account: JellyfinAccount, serverName: String) {
|
|
self.client = client
|
|
self.account = account
|
|
self.serverName = serverName
|
|
}
|
|
|
|
public var isAdministrator: Bool { account.isAdministrator }
|
|
|
|
public nonisolated var capabilities: Set<BackendCapability> {
|
|
var capabilities: Set<BackendCapability> = [.favourites, .liveTV, .libraryScan, .transcoding]
|
|
// Verwaltung nur, wenn das Konto sie auch darf. Den Bereich zu zeigen
|
|
// und bei jedem Tipp abzulehnen, wäre die schlechtere Antwort.
|
|
if account.isAdministrator { capabilities.insert(.administration) }
|
|
return capabilities
|
|
}
|
|
|
|
// MARK: - Blättern
|
|
|
|
public func libraries() async throws(MediaBackendError) -> [MediaLibrary] {
|
|
let page = try await client.send(
|
|
JellyfinRequest(path: "/Users/\(account.userID)/Views"),
|
|
as: JellyfinItemPage.self
|
|
)
|
|
return page.items.map { view in
|
|
let kind: MediaLibrary.Kind = switch view.collectionType {
|
|
case "movies": .movie
|
|
case "tvshows": .show
|
|
case "music": .music
|
|
case "homevideos", "photos": .photo
|
|
case "livetv": .liveTV
|
|
default: .other
|
|
}
|
|
return MediaLibrary(
|
|
id: view.id,
|
|
title: view.name ?? "—",
|
|
kind: kind,
|
|
thumb: images.primary(itemID: view.id, tag: nil)
|
|
)
|
|
}
|
|
}
|
|
|
|
public func homeRows() async throws(MediaBackendError) -> [MediaRowGroup] {
|
|
// Jellyfin hat keine fertigen Reihen wie Plex' Hubs — die Startseite
|
|
// wird aus einzelnen Abfragen zusammengesetzt. Das ist kein Mangel:
|
|
// so steht hier, was die Reihen bedeuten, statt dass es der Server
|
|
// entscheidet.
|
|
// „Zuletzt hinzugefügt" wird streng geholt: scheitert es, ist der
|
|
// Server nicht erreichbar, und dann gehört eine Fehlermeldung hin.
|
|
let latest = try await rowItems(path: "/Users/\(account.userID)/Items", query: [
|
|
URLQueryItem(name: "SortBy", value: "DateCreated"),
|
|
URLQueryItem(name: "SortOrder", value: "Descending"),
|
|
URLQueryItem(name: "IncludeItemTypes", value: "Movie,Series"),
|
|
URLQueryItem(name: "Recursive", value: "true"),
|
|
URLQueryItem(name: "Limit", value: "20"),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
])
|
|
|
|
// Die beiden anderen nachsichtig: fällt „Als Nächstes" aus, soll
|
|
// deshalb nicht die ganze Startseite leer bleiben.
|
|
let resuming = try? await rowItems(path: "/Users/\(account.userID)/Items/Resume", query: [
|
|
URLQueryItem(name: "Limit", value: "20"),
|
|
URLQueryItem(name: "MediaTypes", value: "Video"),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
])
|
|
let nextUp = try? await rowItems(path: "/Shows/NextUp", query: [
|
|
URLQueryItem(name: "userId", value: account.userID),
|
|
URLQueryItem(name: "Limit", value: "20"),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
])
|
|
|
|
let rows = [
|
|
MediaRowGroup(
|
|
id: "resume",
|
|
title: String(localized: "row.continueWatching", bundle: .module),
|
|
items: resuming ?? []
|
|
),
|
|
MediaRowGroup(
|
|
id: "nextup",
|
|
title: String(localized: "row.nextUp", bundle: .module),
|
|
items: nextUp ?? []
|
|
),
|
|
MediaRowGroup(
|
|
id: "latest",
|
|
title: String(localized: "row.recentlyAdded", bundle: .module),
|
|
items: latest
|
|
),
|
|
]
|
|
// Leere Reihen wären auf der Startseite nur Überschriften ohne Inhalt.
|
|
return rows.filter { !$0.items.isEmpty }
|
|
}
|
|
|
|
public func items(
|
|
inLibrary library: MediaLibrary,
|
|
query: MediaQuery,
|
|
offset: Int,
|
|
limit: Int
|
|
) async throws(MediaBackendError) -> MediaPage {
|
|
var items: [URLQueryItem] = [
|
|
URLQueryItem(name: "ParentId", value: library.id),
|
|
URLQueryItem(name: "Recursive", value: "true"),
|
|
URLQueryItem(name: "StartIndex", value: String(offset)),
|
|
URLQueryItem(name: "Limit", value: String(limit)),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
URLQueryItem(name: "SortBy", value: query.sort.jellyfinField),
|
|
URLQueryItem(name: "SortOrder", value: query.sort.isDescending ? "Descending" : "Ascending"),
|
|
]
|
|
|
|
// Ohne diese Angabe liefert eine Serienmediathek alle Folgen einzeln
|
|
// statt der Serien — dieselbe Falle wie bei Plex.
|
|
switch library.kind {
|
|
case .movie: items.append(URLQueryItem(name: "IncludeItemTypes", value: "Movie"))
|
|
case .show: items.append(URLQueryItem(name: "IncludeItemTypes", value: "Series"))
|
|
default: break
|
|
}
|
|
|
|
if query.unwatchedOnly { items.append(URLQueryItem(name: "Filters", value: "IsUnplayed")) }
|
|
if query.favouritesOnly { items.append(URLQueryItem(name: "Filters", value: "IsFavorite")) }
|
|
if let genre = query.genre { items.append(URLQueryItem(name: "Genres", value: genre)) }
|
|
if let year = query.year { items.append(URLQueryItem(name: "Years", value: String(year))) }
|
|
if let rating = query.contentRating {
|
|
items.append(URLQueryItem(name: "OfficialRatings", value: rating))
|
|
}
|
|
if let text = query.searchText { items.append(URLQueryItem(name: "searchTerm", value: text)) }
|
|
// Auflösung filtert Jellyfin nicht selbst; „4K" wird unten aussortiert.
|
|
|
|
let page = try await client.send(
|
|
JellyfinRequest(path: "/Users/\(account.userID)/Items", query: items),
|
|
as: JellyfinItemPage.self
|
|
)
|
|
|
|
var mapped = page.items.map { $0.asMediaItem(imageBase: images) }
|
|
var total = page.totalRecordCount ?? mapped.count
|
|
|
|
if query.resolution?.lowercased() == "4k" {
|
|
mapped = mapped.filter { ($0.preferredVersion?.pixelHeight ?? 0) >= 1_600 }
|
|
// Die Gesamtzahl stimmt dann nicht mehr — lieber die gefilterte
|
|
// Menge als eine Zahl, die zum Nachladen auffordert, obwohl nichts
|
|
// mehr kommt.
|
|
total = mapped.count + offset
|
|
}
|
|
|
|
return MediaPage(items: mapped, offset: offset, totalCount: total)
|
|
}
|
|
|
|
public func item(id: String) async throws(MediaBackendError) -> MediaItem {
|
|
let item = try await client.send(
|
|
JellyfinRequest(
|
|
path: "/Users/\(account.userID)/Items/\(id)",
|
|
query: [URLQueryItem(name: "Fields", value: Self.fields)]
|
|
),
|
|
as: JellyfinItem.self
|
|
)
|
|
return item.asMediaItem(imageBase: images)
|
|
}
|
|
|
|
public func children(of id: String) async throws(MediaBackendError) -> [MediaItem] {
|
|
try await rowItems(path: "/Users/\(account.userID)/Items", query: [
|
|
URLQueryItem(name: "ParentId", value: id),
|
|
URLQueryItem(name: "SortBy", value: "SortName"),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
])
|
|
}
|
|
|
|
public func search(_ text: String, limit: Int) async throws(MediaBackendError) -> [MediaRowGroup] {
|
|
let page = try await client.send(
|
|
JellyfinRequest(path: "/Users/\(account.userID)/Items", query: [
|
|
URLQueryItem(name: "searchTerm", value: text),
|
|
URLQueryItem(name: "Recursive", value: "true"),
|
|
URLQueryItem(name: "IncludeItemTypes", value: "Movie,Series,Episode"),
|
|
URLQueryItem(name: "Limit", value: String(limit)),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
]),
|
|
as: JellyfinItemPage.self
|
|
)
|
|
|
|
// Nach Art gruppiert, weil Plex' Suche gruppiert zurückkommt und die
|
|
// Ergebnisansicht darauf ausgelegt ist.
|
|
let items = page.items.map { $0.asMediaItem(imageBase: images) }
|
|
let groups: [(MediaItem.Kind, String)] = [
|
|
(.movie, "search.movies"), (.show, "search.shows"), (.episode, "search.episodes"),
|
|
]
|
|
return groups.compactMap { kind, key in
|
|
let matching = items.filter { $0.kind == kind }
|
|
guard !matching.isEmpty else { return nil }
|
|
return MediaRowGroup(
|
|
id: kind.rawValue,
|
|
title: String(localized: String.LocalizationValue(key), bundle: .module),
|
|
items: matching
|
|
)
|
|
}
|
|
}
|
|
|
|
public func filterOptions(
|
|
inLibrary library: MediaLibrary,
|
|
field: MediaQuery.Field
|
|
) async throws(MediaBackendError) -> [MediaFilterOption] {
|
|
guard field == .genre else { return [] }
|
|
let page = try await client.send(
|
|
JellyfinRequest(path: "/Genres", query: [
|
|
URLQueryItem(name: "ParentId", value: library.id),
|
|
URLQueryItem(name: "userId", value: account.userID),
|
|
]),
|
|
as: JellyfinItemPage.self
|
|
)
|
|
return page.items.map { MediaFilterOption(id: $0.name ?? $0.id, title: $0.name ?? "—") }
|
|
}
|
|
|
|
public func nextEpisode(after item: MediaItem) async throws(MediaBackendError) -> MediaItem? {
|
|
guard let seriesID = item.seriesID else { return nil }
|
|
let next = try await rowItems(path: "/Shows/NextUp", query: [
|
|
URLQueryItem(name: "userId", value: account.userID),
|
|
URLQueryItem(name: "SeriesId", value: seriesID),
|
|
URLQueryItem(name: "Limit", value: "1"),
|
|
URLQueryItem(name: "Fields", value: Self.fields),
|
|
])
|
|
// Die laufende Folge selbst ist nicht die nächste.
|
|
return next.first { $0.id != item.id }
|
|
}
|
|
|
|
// MARK: - Bilder
|
|
|
|
public nonisolated func artworkURL(path: String?, width: Int, height: Int) -> URL? {
|
|
guard let path else { return nil }
|
|
return imageURL(path: path, extra: [
|
|
URLQueryItem(name: "fillWidth", value: String(width)),
|
|
URLQueryItem(name: "fillHeight", value: String(height)),
|
|
URLQueryItem(name: "quality", value: "90"),
|
|
])
|
|
}
|
|
|
|
public nonisolated func rawImageURL(path: String?) -> URL? {
|
|
guard let path else { return nil }
|
|
return imageURL(path: path, extra: [])
|
|
}
|
|
|
|
/// Setzt Pfad, mitgelieferte Marke und Größenangaben zusammen.
|
|
private nonisolated func imageURL(path: String, extra: [URLQueryItem]) -> URL? {
|
|
let pieces = path.split(separator: "?", maxSplits: 1)
|
|
var query = extra
|
|
if pieces.count > 1 {
|
|
query += URLComponents(string: "?\(pieces[1])")?.queryItems ?? []
|
|
}
|
|
// Bilder holt kein angemeldeter Client, sondern eine Bildansicht — sie
|
|
// schickt keine Kopfzeilen mit, also muss der Schlüssel in die Adresse.
|
|
return client.url(path: String(pieces[0]), query: query, includingToken: account.token)
|
|
}
|
|
|
|
// MARK: - Herunterladen
|
|
|
|
public nonisolated func downloadURL(for file: MediaFile) -> URL? {
|
|
guard let sourceID = file.key else { return nil }
|
|
return client.url(
|
|
path: "/Videos/\(sourceID)/stream",
|
|
query: [
|
|
URLQueryItem(name: "static", value: "true"),
|
|
URLQueryItem(name: "mediaSourceId", value: sourceID),
|
|
],
|
|
includingToken: account.token
|
|
)
|
|
}
|
|
|
|
public nonisolated func compressedDownloadURL(
|
|
itemID: String,
|
|
versionIndex: Int,
|
|
maximumBitrate: Int,
|
|
resolution: String?
|
|
) -> URL? {
|
|
// Jellyfin liefert die Adresse für eine umgerechnete Fassung erst nach
|
|
// einer Anfrage an `/PlaybackInfo` — das geht hier nicht, weil diese
|
|
// Methode keine Anfrage stellen darf. `nil` heißt: nur Original
|
|
// anbieten, statt eine Adresse zu erfinden, die nicht antwortet.
|
|
nil
|
|
}
|
|
|
|
// MARK: - Zustand melden
|
|
|
|
public func markWatched(id: String) async throws(MediaBackendError) {
|
|
try await client.send(JellyfinRequest(
|
|
method: .post,
|
|
path: "/Users/\(account.userID)/PlayedItems/\(id)"
|
|
))
|
|
}
|
|
|
|
public func markUnwatched(id: String) async throws(MediaBackendError) {
|
|
try await client.send(JellyfinRequest(
|
|
method: .delete,
|
|
path: "/Users/\(account.userID)/PlayedItems/\(id)"
|
|
))
|
|
}
|
|
|
|
public func setFavourite(_ isFavourite: Bool, id: String) async throws(MediaBackendError) {
|
|
try await client.send(JellyfinRequest(
|
|
method: isFavourite ? .post : .delete,
|
|
path: "/Users/\(account.userID)/FavoriteItems/\(id)"
|
|
))
|
|
}
|
|
|
|
public func rate(id: String, rating: Int) async throws(MediaBackendError) {
|
|
// Jellyfin kennt nur Daumen hoch und runter, keine Sterne. Eine Zahl
|
|
// von eins bis zehn hier hineinzupressen hieße, etwas zu behaupten,
|
|
// was der Server nicht speichert.
|
|
throw .notFound
|
|
}
|
|
|
|
public func refreshLibrary(_ library: MediaLibrary) async throws(MediaBackendError) {
|
|
try await client.send(JellyfinRequest(
|
|
method: .post,
|
|
path: "/Items/\(library.id)/Refresh",
|
|
query: [URLQueryItem(name: "Recursive", value: "true")]
|
|
))
|
|
}
|
|
|
|
// MARK: - Hilfen
|
|
|
|
private func rowItems(
|
|
path: String,
|
|
query: [URLQueryItem]
|
|
) async throws(MediaBackendError) -> [MediaItem] {
|
|
let page = try await client.send(
|
|
JellyfinRequest(path: path, query: query),
|
|
as: JellyfinItemPage.self
|
|
)
|
|
return page.items.map { $0.asMediaItem(imageBase: images) }
|
|
}
|
|
|
|
/// Was Jellyfin zusätzlich mitschicken soll.
|
|
///
|
|
/// Ohne diese Liste fehlen Beschreibung, Besetzung und Fassungen — und
|
|
/// jede Detailseite bräuchte eine zweite Anfrage.
|
|
private static let fields = "Overview,Genres,People,MediaSources,DateCreated,ParentId"
|
|
}
|
|
|
|
extension MediaQuery.Sort {
|
|
var jellyfinField: String {
|
|
switch self {
|
|
case .titleAscending, .titleDescending: "SortName"
|
|
case .recentlyAdded: "DateCreated"
|
|
case .releaseDate: "PremiereDate"
|
|
case .rating: "CommunityRating"
|
|
case .lastPlayed: "DatePlayed"
|
|
}
|
|
}
|
|
|
|
var isDescending: Bool {
|
|
switch self {
|
|
case .titleAscending: false
|
|
case .titleDescending, .recentlyAdded, .releaseDate, .rating, .lastPlayed: true
|
|
}
|
|
}
|
|
}
|