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
// Use user magnets to check for IA instead
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
try await getUserMagnets()
for cloudMagnet in cloudMagnets {
IAValues.append(
DebridIA(
magnet: Magnet(hash: cloudMagnet.hash, link: nil),
expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: []
if sendMagnets.contains(where: { $0.hash == cloudMagnet.hash }) {
IAValues.append(
DebridIA(
magnet: Magnet(hash: cloudMagnet.hash, link: nil),
expiryTimeStamp: Date().timeIntervalSince1970 + 300,
files: []
)
)
)
}
}
}
@ -381,22 +398,6 @@ class RealDebrid: PollingDebridSource, ObservableObject {
default:
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

View file

@ -174,7 +174,7 @@ class DebridManager: ObservableObject {
return true
} 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
}
}

View file

@ -56,20 +56,27 @@ struct BookmarksView: View {
.frame(height: 15)
}
.task {
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)
}
await matchAgainstIA()
}
.refreshable {
await matchAgainstIA()
}
}
func fetchPredicate() {
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 {
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(
magnet: result.magnet,
expiryTimeStamp: Date().timeIntervalSince1970 + 200,
expiryTimeStamp: Date().timeIntervalSince1970,
files: []
)
}
@ -113,8 +116,10 @@ struct SearchResultButtonView: View {
debridManager.currentDebridTask = Task {
await downloadToDebrid()
// Re-populate the IA cache
await debridManager.populateDebridIA([result.magnet])
// Re-populate the IA cache if a result wasn't initially found
if !foundIAResult {
await debridManager.populateDebridIA([result.magnet])
}
}
}
} label: {