Debrid: Update IA fetching for RealDebrid

To avoid inflating the IA value cache, restore the TTL logic and only
append IAs that are part of the sent magnets.

In addition, if an IA result isn't found for a model download, re-fetch
the IA cache.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2024-11-26 23:33:50 -05:00
parent 4fb5f77718
commit 84357ea2c5
4 changed files with 50 additions and 37 deletions

View file

@ -255,17 +255,34 @@ class RealDebrid: PollingDebridSource, ObservableObject {
// Post-API changes // Post-API changes
// Use user magnets to check for IA instead // Use user magnets to check for IA instead
func instantAvailability(magnets: [Magnet]) async throws { func instantAvailability(magnets: [Magnet]) async throws {
let now = Date().timeIntervalSince1970
let sendMagnets = magnets.filter { magnet in
if let IAIndex = IAValues.firstIndex(where: { $0.magnet.hash == magnet.hash }) {
if now > IAValues[IAIndex].expiryTimeStamp {
IAValues.remove(at: IAIndex)
return true
} else {
return false
}
} else {
return true
}
}
// Fetch the user magnets to the latest version // Fetch the user magnets to the latest version
try await getUserMagnets() try await getUserMagnets()
for cloudMagnet in cloudMagnets { for cloudMagnet in cloudMagnets {
IAValues.append( if sendMagnets.contains(where: { $0.hash == cloudMagnet.hash }) {
DebridIA( IAValues.append(
magnet: Magnet(hash: cloudMagnet.hash, link: nil), DebridIA(
expiryTimeStamp: Date().timeIntervalSince1970 + 300, magnet: Magnet(hash: cloudMagnet.hash, link: nil),
files: [] expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: []
)
) )
) }
} }
} }
@ -381,22 +398,6 @@ class RealDebrid: PollingDebridSource, ObservableObject {
default: default:
throw DebridError.EmptyUserMagnets throw DebridError.EmptyUserMagnets
} }
/*
if rawResponse.status == "downloaded" {
//let cloudMagnetLink = rawResponse.links[safe: linkIndex ?? -1],
/*
return DebridIAFile(
id: 0,
name: rawResponse.filename,
streamUrlString: cloudMagnetLink
)
*/
} else if rawResponse.status == "downloading" || rawResponse.status == "queued" {
throw DebridError.IsCaching
} else {
throw DebridError.EmptyUserMagnets
}
*/
} }
// Downloads link from selectFiles for playback // Downloads link from selectFiles for playback

View file

@ -174,7 +174,7 @@ class DebridManager: ObservableObject {
return true return true
} else { } else {
logManager?.error("DebridManager: Could not find the associated \(selectedSource.id) entry for magnet hash \(magnetHash)") logManager?.warn("DebridManager: Could not find the associated \(selectedSource.id) entry for magnet hash \(magnetHash)")
return false return false
} }
} }

View file

@ -56,20 +56,27 @@ struct BookmarksView: View {
.frame(height: 15) .frame(height: 15)
} }
.task { .task {
if !debridManager.enabledDebrids.isEmpty { await matchAgainstIA()
let magnets = bookmarks.compactMap { }
if let magnetHash = $0.magnetHash { .refreshable {
return Magnet(hash: magnetHash, link: $0.magnetLink) await matchAgainstIA()
} else {
return nil
}
}
await debridManager.populateDebridIA(magnets)
}
} }
} }
func fetchPredicate() { func fetchPredicate() {
bookmarks.nsPredicate = searchText.isEmpty ? nil : NSPredicate(format: "title CONTAINS[cd] %@", searchText) bookmarks.nsPredicate = searchText.isEmpty ? nil : NSPredicate(format: "title CONTAINS[cd] %@", searchText)
} }
func matchAgainstIA() async {
if !debridManager.enabledDebrids.isEmpty {
let magnets = bookmarks.compactMap {
if let magnetHash = $0.magnetHash {
return Magnet(hash: magnetHash, link: $0.magnetLink)
} else {
return nil
}
}
await debridManager.populateDebridIA(magnets)
}
}
} }

View file

@ -102,10 +102,13 @@ struct SearchResultButtonView: View {
Button { Button {
if debridManager.currentDebridTask == nil { if debridManager.currentDebridTask == nil {
if !debridManager.selectDebridResult(magnet: result.magnet) { let foundIAResult = debridManager.selectDebridResult(magnet: result.magnet)
// Add a fake IA because we don't know if the magnet is cached at this point
if !foundIAResult {
debridManager.selectedDebridItem = DebridIA( debridManager.selectedDebridItem = DebridIA(
magnet: result.magnet, magnet: result.magnet,
expiryTimeStamp: Date().timeIntervalSince1970 + 200, expiryTimeStamp: Date().timeIntervalSince1970,
files: [] files: []
) )
} }
@ -113,8 +116,10 @@ struct SearchResultButtonView: View {
debridManager.currentDebridTask = Task { debridManager.currentDebridTask = Task {
await downloadToDebrid() await downloadToDebrid()
// Re-populate the IA cache // Re-populate the IA cache if a result wasn't initially found
await debridManager.populateDebridIA([result.magnet]) if !foundIAResult {
await debridManager.populateDebridIA([result.magnet])
}
} }
} }
} label: { } label: {