From 5eb1158456242bccaf2f46cb417e61279dca062e Mon Sep 17 00:00:00 2001 From: kingbri Date: Mon, 3 Jun 2024 16:15:17 -0400 Subject: [PATCH] Debrid: Add Premiumize to InstantAvailability Also add the requirement to the protocol. Signed-off-by: kingbri --- Ferrite/API/PremiumizeWrapper.swift | 23 +++++++++++++++++++++++ Ferrite/Protocols/Debrid.swift | 2 ++ Ferrite/ViewModels/DebridManager.swift | 19 ++----------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Ferrite/API/PremiumizeWrapper.swift b/Ferrite/API/PremiumizeWrapper.swift index 3216ad6..43884b1 100644 --- a/Ferrite/API/PremiumizeWrapper.swift +++ b/Ferrite/API/PremiumizeWrapper.swift @@ -118,6 +118,29 @@ public class Premiumize: OAuthDebridSource { // MARK: - Instant availability + public func instantAvailability(magnets: [Magnet]) async throws -> [DebridIA] { + var collectedIA: [DebridIA] = [] + + // Only strip magnets that don't have an associated link for PM + let strippedMagnets: [Magnet] = magnets.compactMap { + if let magnetLink = $0.link { + return Magnet(hash: $0.hash, link: magnetLink) + } else { + return nil + } + } + + let availableMagnets = try await divideCacheRequests(magnets: strippedMagnets) + + // Split DDL requests into chunks of 10 + for chunk in availableMagnets.chunked(into: 10) { + let tempIA = try await divideDDLRequests(magnetChunk: chunk) + collectedIA += tempIA + } + + return collectedIA + } + // Function to divide and execute DDL endpoint requests in parallel // Calls this for 10 requests at a time to not overwhelm API servers public func divideDDLRequests(magnetChunk: [Magnet]) async throws -> [DebridIA] { diff --git a/Ferrite/Protocols/Debrid.swift b/Ferrite/Protocols/Debrid.swift index b4cd275..8853145 100644 --- a/Ferrite/Protocols/Debrid.swift +++ b/Ferrite/Protocols/Debrid.swift @@ -17,6 +17,8 @@ public protocol DebridSource { func setApiKey(_ key: String) -> Bool func logout() async + func instantAvailability(magnets: [Magnet]) async throws -> [DebridIA] + // Fetches a download link from a source // Include the instant availability information with the args func getDownloadLink(magnet: Magnet, ia: DebridIA?, iaFile: DebridIAFile?) async throws -> String diff --git a/Ferrite/ViewModels/DebridManager.swift b/Ferrite/ViewModels/DebridManager.swift index 87139b3..57b1feb 100644 --- a/Ferrite/ViewModels/DebridManager.swift +++ b/Ferrite/ViewModels/DebridManager.swift @@ -246,23 +246,8 @@ public class DebridManager: ObservableObject { if enabledDebrids.contains(.premiumize) { do { - // Only strip magnets that don't have an associated link for PM - let strippedResultMagnets: [Magnet] = resultMagnets.compactMap { - if let magnetLink = $0.link { - return Magnet(hash: $0.hash, link: magnetLink) - } else { - return nil - } - } - - let availableMagnets = try await premiumize.divideCacheRequests(magnets: strippedResultMagnets) - - // Split DDL requests into chunks of 10 - for chunk in availableMagnets.chunked(into: 10) { - let tempIA = try await premiumize.divideDDLRequests(magnetChunk: chunk) - - premiumizeIAValues += tempIA - } + let fetchedPremiumizeIA = try await premiumize.instantAvailability(magnets: sendMagnets) + premiumizeIAValues += fetchedPremiumizeIA } catch { await sendDebridError(error, prefix: "Premiumize IA fetch error") }