Add related videos

This commit is contained in:
Arkadiusz Fal
2021-11-03 00:02:02 +01:00
parent f49453e871
commit f8e6560698
13 changed files with 185 additions and 46 deletions

View File

@@ -314,8 +314,8 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
likes: json["likeCount"].int,
dislikes: json["dislikeCount"].int,
keywords: json["keywords"].arrayValue.map { $0.stringValue },
streams: extractFormatStreams(from: json["formatStreams"].arrayValue) +
extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue)
streams: extractStreams(from: json),
related: extractRelated(from: json)
)
}
@@ -349,6 +349,11 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
}
}
private static func extractStreams(from json: JSON) -> [Stream] {
extractFormatStreams(from: json["formatStreams"].arrayValue) +
extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue)
}
private static func extractFormatStreams(from streams: [JSON]) -> [Stream] {
streams.map {
SingleAssetStream(
@@ -378,4 +383,11 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
)
}
}
private static func extractRelated(from content: JSON) -> [Video] {
content
.dictionaryValue["recommendedVideos"]?
.arrayValue
.compactMap(extractVideo(from:)) ?? []
}
}

View File

@@ -224,7 +224,8 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
thumbnails: thumbnails,
likes: details["likes"]?.int,
dislikes: details["dislikes"]?.int,
streams: extractStreams(from: content)
streams: extractStreams(from: content),
related: extractRelated(from: content)
)
}
@@ -310,6 +311,13 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
return streams
}
private static func extractRelated(from content: JSON) -> [Video] {
content
.dictionaryValue["relatedStreams"]?
.arrayValue
.compactMap(extractVideo(from:)) ?? []
}
private static func compatibleAudioStreams(from content: JSON) -> [JSON] {
content
.dictionaryValue["audioStreams"]?

View File

@@ -30,6 +30,8 @@ struct Video: Identifiable, Equatable, Hashable {
var channel: Channel
var related = [Video]()
init(
id: String? = nil,
videoID: String,
@@ -49,7 +51,8 @@ struct Video: Identifiable, Equatable, Hashable {
likes: Int? = nil,
dislikes: Int? = nil,
keywords: [String] = [],
streams: [Stream] = []
streams: [Stream] = [],
related: [Video] = []
) {
self.id = id ?? UUID().uuidString
self.videoID = videoID
@@ -70,6 +73,7 @@ struct Video: Identifiable, Equatable, Hashable {
self.dislikes = dislikes
self.keywords = keywords
self.streams = streams
self.related = related
}
var publishedDate: String? {