mirror of
https://github.com/dirtydishes/dreamio.git
synced 2026-06-06 13:37:24 +00:00
fix stremio subtitle handoff to vlc
This commit is contained in:
parent
c59b318d9b
commit
d3c5507763
9 changed files with 951 additions and 42 deletions
|
|
@ -6,6 +6,10 @@ struct ResolvedNativeStream {
|
|||
let source: String
|
||||
}
|
||||
|
||||
protocol SubtitleResolving {
|
||||
func resolve(_ candidate: SubtitleCandidate) async -> SubtitleCandidate?
|
||||
}
|
||||
|
||||
enum StreamResolverError: LocalizedError {
|
||||
case noResolverURL
|
||||
case httpStatus(Int)
|
||||
|
|
@ -47,6 +51,9 @@ final class SubtitleResolver: SubtitleResolving {
|
|||
|
||||
var request = URLRequest(url: candidate.url)
|
||||
request.setValue("application/json, text/plain, text/vtt, application/x-subrip, */*", forHTTPHeaderField: "Accept")
|
||||
StreamClassifier.defaultHeaders(userAgent: nil).forEach { key, value in
|
||||
request.setValue(value, forHTTPHeaderField: key)
|
||||
}
|
||||
|
||||
do {
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
|
@ -66,7 +73,12 @@ final class SubtitleResolver: SubtitleResolving {
|
|||
from: data,
|
||||
responseURL: response.url,
|
||||
original: candidate
|
||||
)
|
||||
).map { resolved in
|
||||
#if DEBUG
|
||||
print("[DreamioSubtitles] resolved candidate from=\(URLRedactor.redactedURLString(candidate.url.absoluteString)) to=\(URLRedactor.redactedURLString(resolved.url.absoluteString))")
|
||||
#endif
|
||||
return resolved
|
||||
} ?? Self.logRejected(candidate, responseURL: response.url, data: data)
|
||||
} catch {
|
||||
#if DEBUG
|
||||
print("[DreamioSubtitles] resolve failure=\(URLRedactor.redactedURLString(error.localizedDescription)) url=\(URLRedactor.redactedURLString(candidate.url.absoluteString))")
|
||||
|
|
@ -127,6 +139,24 @@ final class SubtitleResolver: SubtitleResolving {
|
|||
|| lowercased.contains("/subtitle")
|
||||
|| lowercased.contains("subtitle")
|
||||
}
|
||||
|
||||
private static func logRejected(_ candidate: SubtitleCandidate, responseURL: URL?, data: Data) -> SubtitleCandidate? {
|
||||
#if DEBUG
|
||||
let responseDescription = responseURL.map { URLRedactor.redactedURLString($0.absoluteString) } ?? "none"
|
||||
let bodyKind: String
|
||||
if data.isEmpty {
|
||||
bodyKind = "empty"
|
||||
} else if (try? JSONSerialization.jsonObject(with: data)) != nil {
|
||||
bodyKind = "json-without-direct-subtitle"
|
||||
} else if String(data: data, encoding: .utf8) != nil {
|
||||
bodyKind = "text-without-direct-subtitle"
|
||||
} else {
|
||||
bodyKind = "unreadable"
|
||||
}
|
||||
print("[DreamioSubtitles] rejected candidate reason=\(bodyKind) url=\(URLRedactor.redactedURLString(candidate.url.absoluteString)) responseURL=\(responseDescription)")
|
||||
#endif
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
protocol StreamResolving {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue