add chinese subtitles
This commit is contained in:
@@ -46,6 +46,26 @@ struct SentenceExplanationResponse: Decodable {
|
||||
}
|
||||
}
|
||||
|
||||
struct SubtitleTranslationItem: Decodable {
|
||||
let index: Int
|
||||
let zhText: String
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case index
|
||||
case zhText = "zh_text"
|
||||
}
|
||||
}
|
||||
|
||||
struct SubtitleTranslationResponse: Decodable {
|
||||
let videoHash: String
|
||||
let translations: [SubtitleTranslationItem]
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case translations
|
||||
case videoHash = "video_hash"
|
||||
}
|
||||
}
|
||||
|
||||
extension SentenceBoundaryAPI {
|
||||
static func explain(
|
||||
_ sentence: SentenceBoundary,
|
||||
@@ -79,4 +99,32 @@ extension SentenceBoundaryAPI {
|
||||
.decode(SentenceExplanationResponse.self, from: result.body)
|
||||
.explanation
|
||||
}
|
||||
|
||||
static func translateSubtitles(videoHash: String) async throws -> [Int: String] {
|
||||
let hash = videoHash.lowercased()
|
||||
guard hash.range(of: AppConfig.sha256Pattern, options: .regularExpression) != nil else {
|
||||
throw SentenceBoundaryAPIError.invalidHash
|
||||
}
|
||||
var headers: [String: String] = ["Accept": "application/json"]
|
||||
if !AppConfig.authToken.isEmpty {
|
||||
headers["Authorization"] = "Bearer \(AppConfig.authToken)"
|
||||
}
|
||||
let result = try await HTTPClient.execute(
|
||||
baseURL: AppConfig.serverBaseURL,
|
||||
path: "api/v1/videos/\(hash)/subtitle-translation",
|
||||
method: "POST",
|
||||
timeout: AppConfig.translationTimeout,
|
||||
headers: headers
|
||||
)
|
||||
guard (200...299).contains(result.statusCode) else {
|
||||
let detail = String(data: result.body, encoding: .utf8) ?? ""
|
||||
throw HTTPClientError.badStatus(result.statusCode, detail)
|
||||
}
|
||||
let payload = try JSONDecoder().decode(SubtitleTranslationResponse.self, from: result.body)
|
||||
var mapped: [Int: String] = [:]
|
||||
for item in payload.translations where !item.zhText.isEmpty {
|
||||
mapped[item.index] = item.zhText
|
||||
}
|
||||
return mapped
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user