2021-06-10 22:50:10 +00:00
|
|
|
import Alamofire
|
2021-06-14 18:05:02 +00:00
|
|
|
import AVKit
|
2021-06-10 22:50:10 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
2021-07-31 22:10:56 +00:00
|
|
|
struct Video: Identifiable, Equatable {
|
2021-06-10 22:50:10 +00:00
|
|
|
let id: String
|
|
|
|
var title: String
|
2021-07-07 22:39:18 +00:00
|
|
|
var thumbnails: [Thumbnail]
|
2021-06-10 22:50:10 +00:00
|
|
|
var author: String
|
2021-06-11 00:05:59 +00:00
|
|
|
var length: TimeInterval
|
|
|
|
var published: String
|
|
|
|
var views: Int
|
2021-06-17 22:43:29 +00:00
|
|
|
var description: String
|
|
|
|
var genre: String
|
2021-06-11 21:11:59 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
// index used when in the Playlist
|
2021-07-09 14:53:53 +00:00
|
|
|
let indexID: String?
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var live: Bool
|
|
|
|
var upcoming: Bool
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
var streams = [Stream]()
|
2021-07-22 12:43:13 +00:00
|
|
|
var hlsUrl: URL?
|
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
var publishedAt: Date?
|
|
|
|
var likes: Int?
|
|
|
|
var dislikes: Int?
|
|
|
|
var keywords = [String]()
|
|
|
|
|
2021-08-25 22:12:59 +00:00
|
|
|
var channel: Channel
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
init(
|
|
|
|
id: String,
|
|
|
|
title: String,
|
|
|
|
author: String,
|
|
|
|
length: TimeInterval,
|
|
|
|
published: String,
|
|
|
|
views: Int,
|
|
|
|
description: String,
|
|
|
|
genre: String,
|
2021-08-25 22:12:59 +00:00
|
|
|
channel: Channel,
|
2021-07-22 12:43:13 +00:00
|
|
|
thumbnails: [Thumbnail] = [],
|
|
|
|
indexID: String? = nil,
|
|
|
|
live: Bool = false,
|
2021-08-22 19:13:33 +00:00
|
|
|
upcoming: Bool = false,
|
|
|
|
publishedAt: Date? = nil,
|
|
|
|
likes: Int? = nil,
|
|
|
|
dislikes: Int? = nil,
|
|
|
|
keywords: [String] = []
|
2021-07-22 12:43:13 +00:00
|
|
|
) {
|
|
|
|
self.id = id
|
|
|
|
self.title = title
|
|
|
|
self.author = author
|
|
|
|
self.length = length
|
|
|
|
self.published = published
|
|
|
|
self.views = views
|
|
|
|
self.description = description
|
|
|
|
self.genre = genre
|
2021-08-25 22:12:59 +00:00
|
|
|
self.channel = channel
|
2021-07-22 12:43:13 +00:00
|
|
|
self.thumbnails = thumbnails
|
|
|
|
self.indexID = indexID
|
|
|
|
self.live = live
|
|
|
|
self.upcoming = upcoming
|
2021-08-22 19:13:33 +00:00
|
|
|
self.publishedAt = publishedAt
|
|
|
|
self.likes = likes
|
|
|
|
self.dislikes = dislikes
|
|
|
|
self.keywords = keywords
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
2021-06-10 22:50:10 +00:00
|
|
|
|
|
|
|
init(_ json: JSON) {
|
2021-07-09 14:53:53 +00:00
|
|
|
let videoID = json["videoId"].stringValue
|
|
|
|
|
|
|
|
if let id = json["indexId"].string {
|
|
|
|
indexID = id
|
|
|
|
self.id = videoID + id
|
|
|
|
} else {
|
|
|
|
indexID = nil
|
|
|
|
id = videoID
|
|
|
|
}
|
|
|
|
|
2021-06-10 22:50:10 +00:00
|
|
|
title = json["title"].stringValue
|
|
|
|
author = json["author"].stringValue
|
2021-06-11 00:05:59 +00:00
|
|
|
length = json["lengthSeconds"].doubleValue
|
|
|
|
published = json["publishedText"].stringValue
|
|
|
|
views = json["viewCount"].intValue
|
2021-06-17 22:43:29 +00:00
|
|
|
description = json["description"].stringValue
|
|
|
|
genre = json["genre"].stringValue
|
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
thumbnails = Video.extractThumbnails(from: json)
|
2021-06-11 00:05:59 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
live = json["liveNow"].boolValue
|
|
|
|
upcoming = json["isUpcoming"].boolValue
|
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
likes = json["likeCount"].int
|
|
|
|
dislikes = json["dislikeCount"].int
|
|
|
|
|
|
|
|
keywords = json["keywords"].arrayValue.map { $0.stringValue }
|
|
|
|
|
|
|
|
if let publishedInterval = json["published"].double {
|
|
|
|
publishedAt = Date(timeIntervalSince1970: publishedInterval)
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
streams = Video.extractFormatStreams(from: json["formatStreams"].arrayValue)
|
|
|
|
streams.append(contentsOf: Video.extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue))
|
2021-07-22 12:43:13 +00:00
|
|
|
|
|
|
|
hlsUrl = json["hlsUrl"].url
|
2021-08-25 22:12:59 +00:00
|
|
|
channel = Channel(json: json)
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|
2021-06-11 00:05:59 +00:00
|
|
|
|
|
|
|
var playTime: String? {
|
2021-06-16 19:30:50 +00:00
|
|
|
guard !length.isZero else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 00:05:59 +00:00
|
|
|
let formatter = DateComponentsFormatter()
|
|
|
|
|
|
|
|
formatter.unitsStyle = .positional
|
|
|
|
formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
|
|
|
|
formatter.zeroFormattingBehavior = [.pad]
|
|
|
|
|
|
|
|
return formatter.string(from: length)
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var publishedDate: String? {
|
|
|
|
(published.isEmpty || published == "0 seconds ago") ? nil : published
|
|
|
|
}
|
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
var viewsCount: String? {
|
2021-08-31 21:17:50 +00:00
|
|
|
views != 0 ? views.formattedAsAbbreviation() : nil
|
2021-08-22 19:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var likesCount: String? {
|
2021-08-31 21:17:50 +00:00
|
|
|
likes?.formattedAsAbbreviation()
|
2021-08-22 19:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dislikesCount: String? {
|
2021-08-31 21:17:50 +00:00
|
|
|
dislikes?.formattedAsAbbreviation()
|
2021-06-11 00:05:59 +00:00
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
|
|
|
var selectableStreams: [Stream] {
|
|
|
|
let streams = streams.sorted { $0.resolution > $1.resolution }
|
|
|
|
var selectable = [Stream]()
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
Stream.Resolution.allCases.forEach { resolution in
|
|
|
|
if let stream = streams.filter({ $0.resolution == resolution }).min(by: { $0.kind < $1.kind }) {
|
2021-06-14 18:05:02 +00:00
|
|
|
selectable.append(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return selectable
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultStream: Stream? {
|
2021-07-22 12:43:13 +00:00
|
|
|
selectableStreams.first { $0.kind == .stream }
|
2021-06-14 18:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 16:35:21 +00:00
|
|
|
var bestStream: Stream? {
|
|
|
|
selectableStreams.min { $0.resolution > $1.resolution }
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
func streamWithResolution(_ resolution: Stream.Resolution) -> Stream? {
|
2021-06-19 20:10:14 +00:00
|
|
|
selectableStreams.first { $0.resolution == resolution }
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultStreamForProfile(_ profile: Profile) -> Stream? {
|
2021-07-07 22:39:18 +00:00
|
|
|
streamWithResolution(profile.defaultStreamResolution.value) ?? streams.first
|
2021-06-19 20:10:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
|
2021-07-07 22:39:18 +00:00
|
|
|
thumbnails.first { $0.quality == quality }?.url
|
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
private static func extractThumbnails(from details: JSON) -> [Thumbnail] {
|
|
|
|
details["videoThumbnails"].arrayValue.map { json in
|
|
|
|
Thumbnail(json)
|
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
private static func extractFormatStreams(from streams: [JSON]) -> [Stream] {
|
2021-06-14 18:05:02 +00:00
|
|
|
streams.map {
|
2021-07-22 12:43:13 +00:00
|
|
|
SingleAssetStream(
|
2021-07-31 22:10:56 +00:00
|
|
|
avAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset($0["url"].stringValue)!),
|
2021-07-22 12:43:13 +00:00
|
|
|
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue)!,
|
|
|
|
kind: .stream,
|
2021-06-14 18:05:02 +00:00
|
|
|
encoding: $0["encoding"].stringValue
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:39:18 +00:00
|
|
|
private static func extractAdaptiveFormats(from streams: [JSON]) -> [Stream] {
|
2021-06-14 18:05:02 +00:00
|
|
|
let audioAssetURL = streams.first { $0["type"].stringValue.starts(with: "audio/mp4") }
|
|
|
|
guard audioAssetURL != nil else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
let videoAssetsURLs = streams.filter { $0["type"].stringValue.starts(with: "video/mp4") && $0["encoding"].stringValue == "h264" }
|
|
|
|
|
|
|
|
return videoAssetsURLs.map {
|
|
|
|
Stream(
|
2021-07-31 22:10:56 +00:00
|
|
|
audioAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset(audioAssetURL!["url"].stringValue)!),
|
|
|
|
videoAsset: AVURLAsset(url: InvidiousAPI.proxyURLForAsset($0["url"].stringValue)!),
|
2021-07-22 12:43:13 +00:00
|
|
|
resolution: Stream.Resolution.from(resolution: $0["resolution"].stringValue)!,
|
|
|
|
kind: .adaptive,
|
2021-06-14 18:05:02 +00:00
|
|
|
encoding: $0["encoding"].stringValue
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
|
|
|
|
static func == (lhs: Video, rhs: Video) -> Bool {
|
|
|
|
lhs.id == rhs.id
|
|
|
|
}
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|