Channel view

This commit is contained in:
Arkadiusz Fal
2021-06-11 23:11:59 +02:00
parent 4bc70f351d
commit 314c3b4968
13 changed files with 205 additions and 45 deletions

View File

@@ -5,34 +5,38 @@ import SwiftyJSON
final class Video: Identifiable, ObservableObject {
let id: String
var title: String
var thumbnailURL: URL
var thumbnailURL: URL?
var author: String
var length: TimeInterval
var published: String
var views: Int
var channelID: String
@Published var url: URL?
@Published var error: Bool = false
init(id: String, title: String, thumbnailURL: URL, author: String, length: TimeInterval, published: String, views: Int = 0) {
init(id: String, title: String, thumbnailURL: URL?, author: String, length: TimeInterval, published: String, channelID: String, views: Int = 0) {
self.id = id
self.title = title
self.thumbnailURL = thumbnailURL
self.author = author
self.length = length
self.published = published
self.channelID = channelID
self.views = views
}
init(_ json: JSON) {
func extractThumbnailURL(from details: JSON) -> URL {
func extractThumbnailURL(from details: JSON) -> URL? {
if details["videoThumbnails"].arrayValue.isEmpty {
return URL(string: "https://invidious.home.arekf.net/vi/LuKwJyBNBsE/maxres.jpg")!
return nil
}
return details["videoThumbnails"][0]["url"].url!
let thumbnail = details["videoThumbnails"].arrayValue.first(where: { $0["quality"].stringValue == "medium" })!
return thumbnail["url"].url!
}
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
if streams.isEmpty {
error = true
@@ -43,7 +47,7 @@ final class Video: Identifiable, ObservableObject {
return stream["url"].url
}
id = json["videoId"].stringValue
title = json["title"].stringValue
thumbnailURL = extractThumbnailURL(from: json)
@@ -51,6 +55,7 @@ final class Video: Identifiable, ObservableObject {
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
channelID = json["authorId"].stringValue
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
}