RealDebrid: Demystify instantAvailability endpoint

The instantAvailability endpoint is now decoded using Codable
and does not have any ambiguity.

Batches are now checked, but still not accounted for due to how
RD handles batches from this endpoint. All batch links are not
supported for instant streaming from Ferrite until support is added.

Thanks to Skitty for helping out with decoding the endpoint.

Signed-off-by: kingbri <bdashore3@gmail.com>
This commit is contained in:
kingbri 2022-07-22 12:02:08 -04:00
parent 1ddf50cd15
commit 47f713744b
2 changed files with 29 additions and 15 deletions

View file

@ -48,19 +48,29 @@ public struct TokenResponse: Codable {
}
}
// MARK: - instantAvailability endpoint: Currently not used due to JSON complexity
/*
public struct InstantAvailabilityValue: Decodable {
let rd: [[String: InstantAvailabilityFile]]
// MARK: - instantAvailability endpoint
// Thanks Skitty!
struct InstantAvailabilityResponse: Codable {
var data: InstantAvailabilityData?
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let data = try? container.decode(InstantAvailabilityData.self) {
self.data = data
}
}
}
struct InstantAvailabilityFile: Codable {
let filename: String
let filesize: Int
struct InstantAvailabilityData: Codable {
var rd: [[String: InstantAvailabilityInfo]]
}
public typealias InstantAvailabilityResponse = [String: InstantAvailabilityValue]
*/
struct InstantAvailabilityInfo: Codable {
var filename: String
var filesize: Int
}
// MARK: - addMagnet endpoint
public struct AddMagnetResponse: Codable {

View file

@ -6,7 +6,6 @@
//
import Foundation
import SwiftUI
public enum RealDebridError: Error {
case InvalidUrl
@ -226,11 +225,16 @@ public class RealDebrid: ObservableObject {
let data = try await performRequest(request: &request, requestName: #function)
// Does not account for torrent packs at the moment
if let rawResponse = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
for (key, value) in rawResponse {
if value as? [String: Any] != nil {
availableHashes.append(key)
}
let rawResponseDict = try jsonDecoder.decode([String: InstantAvailabilityResponse].self, from: data)
for (hash, response) in rawResponseDict {
guard let data = response.data else {
continue
}
// Do not include if a hash is a batch
if !(data.rd.count > 1), !(data.rd[safe: 0]?.keys.count ?? 0 > 1) {
availableHashes.append(hash)
}
}