yattee/Model/Video.swift

338 lines
9.4 KiB
Swift
Raw Normal View History

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 SwiftUI
2021-06-10 22:50:10 +00:00
import SwiftyJSON
struct Video: Identifiable, Equatable, Hashable {
2023-02-25 15:42:18 +00:00
static let shortLength = 61.0
2022-11-10 17:11:28 +00:00
enum VideoID {
static func isValid(_ id: Video.ID) -> Bool {
2022-12-09 00:15:19 +00:00
isYouTube(id) || isPeerTube(id)
}
static func isYouTube(_ id: Video.ID) -> Bool {
2022-11-10 17:11:28 +00:00
id.count == 11
}
2022-12-09 00:15:19 +00:00
static func isPeerTube(_ id: Video.ID) -> Bool {
id.count == 36
}
2022-11-10 17:11:28 +00:00
}
2022-12-09 00:15:19 +00:00
var instanceID: Instance.ID?
var app: VideosApp
var instanceURL: URL?
var id: String
var videoID: String
var videoURL: URL?
2021-06-10 22:50:10 +00:00
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-10-20 22:21:50 +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
var indexID: String?
2021-07-22 12:43:13 +00:00
var live: Bool
var upcoming: Bool
2023-02-25 15:42:18 +00:00
var short: Bool
2021-07-22 12:43:13 +00:00
2021-06-14 18:05:02 +00:00
var streams = [Stream]()
2021-07-22 12:43:13 +00:00
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-11-02 23:02:02 +00:00
var related = [Video]()
var chapters = [Chapter]()
2021-11-02 23:02:02 +00:00
2022-07-05 17:20:25 +00:00
var captions = [Captions]()
2021-07-22 12:43:13 +00:00
init(
2022-12-09 00:15:19 +00:00
instanceID: Instance.ID? = nil,
app: VideosApp,
instanceURL: URL? = nil,
id: String? = nil,
videoID: String,
2022-12-09 00:15:19 +00:00
videoURL: URL? = nil,
title: String = "",
author: String = "",
length: TimeInterval = .zero,
published: String = "",
views: Int = 0,
2021-10-20 22:21:50 +00:00
description: String? = nil,
genre: String? = nil,
2022-12-13 23:07:32 +00:00
channel: Channel? = nil,
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,
2023-02-25 15:42:18 +00:00
short: Bool = false,
2021-08-22 19:13:33 +00:00
publishedAt: Date? = nil,
likes: Int? = nil,
dislikes: Int? = nil,
2021-10-20 22:21:50 +00:00
keywords: [String] = [],
2021-11-02 23:02:02 +00:00
streams: [Stream] = [],
2023-06-17 12:09:51 +00:00
related: [Self] = [],
2022-07-05 17:20:25 +00:00
chapters: [Chapter] = [],
captions: [Captions] = []
2021-07-22 12:43:13 +00:00
) {
2022-12-09 00:15:19 +00:00
self.instanceID = instanceID
self.app = app
self.instanceURL = instanceURL
self.id = id ?? UUID().uuidString
self.videoID = videoID
2022-12-09 00:15:19 +00:00
self.videoURL = videoURL
2021-07-22 12:43:13 +00:00
self.title = title
self.author = author
self.length = length
self.published = published
self.views = views
self.description = description
self.genre = genre
2022-12-13 23:07:32 +00:00
self.channel = channel ?? .init(app: app, id: "", name: "")
2021-07-22 12:43:13 +00:00
self.thumbnails = thumbnails
self.indexID = indexID
self.live = live
self.upcoming = upcoming
2023-02-25 15:42:18 +00:00
self.short = short
2021-08-22 19:13:33 +00:00
self.publishedAt = publishedAt
self.likes = likes
self.dislikes = dislikes
self.keywords = keywords
2021-10-20 22:21:50 +00:00
self.streams = streams
2021-11-02 23:02:02 +00:00
self.related = related
self.chapters = chapters
2022-07-05 17:20:25 +00:00
self.captions = captions
2021-07-22 12:43:13 +00:00
}
2021-06-10 22:50:10 +00:00
2023-06-17 12:09:51 +00:00
static func local(_ url: URL) -> Self {
2023-04-22 13:08:33 +00:00
Self(
2022-12-09 00:15:19 +00:00
app: .local,
2022-11-10 17:11:28 +00:00
videoID: url.absoluteString,
streams: [.init(localURL: url)]
)
}
2022-12-10 00:23:13 +00:00
var cacheKey: String {
switch app {
case .local:
return videoID
case .invidious:
return "youtube-\(videoID)"
case .piped:
return "youtube-\(videoID)"
case .peerTube:
return "peertube-\(instanceURL?.absoluteString ?? "unknown-instance")-\(videoID)"
}
}
var json: JSON {
let dateFormatter = ISO8601DateFormatter()
let publishedAt = self.publishedAt == nil ? "" : dateFormatter.string(from: self.publishedAt!)
return [
"instanceID": instanceID ?? "",
"app": app.rawValue,
"instanceURL": instanceURL?.absoluteString ?? "",
"id": id,
"videoID": videoID,
"videoURL": videoURL?.absoluteString ?? "",
"title": title,
"author": author,
"length": length,
"published": published,
"views": views,
"description": description ?? "",
"genre": genre ?? "",
"channel": channel.json.object,
"thumbnails": thumbnails.compactMap { $0.json.object },
"indexID": indexID ?? "",
"live": live,
"upcoming": upcoming,
2023-02-25 15:42:18 +00:00
"short": short,
2022-12-10 00:23:13 +00:00
"publishedAt": publishedAt
]
}
static func from(_ json: JSON) -> Self {
let dateFormatter = ISO8601DateFormatter()
2023-04-22 13:08:33 +00:00
return Self(
2022-12-10 00:23:13 +00:00
instanceID: json["instanceID"].stringValue,
app: .init(rawValue: json["app"].stringValue) ?? AccountsModel.shared.current.app ?? .local,
instanceURL: URL(string: json["instanceURL"].stringValue) ?? AccountsModel.shared.current.instance.apiURL,
id: json["id"].stringValue,
videoID: json["videoID"].stringValue,
videoURL: json["videoURL"].url,
title: json["title"].stringValue,
author: json["author"].stringValue,
length: json["length"].doubleValue,
published: json["published"].stringValue,
views: json["views"].intValue,
description: json["description"].string,
genre: json["genre"].string,
channel: Channel.from(json["channel"]),
thumbnails: json["thumbnails"].arrayValue.compactMap { Thumbnail.from($0) },
indexID: json["indexID"].stringValue,
live: json["live"].boolValue,
upcoming: json["upcoming"].boolValue,
2023-02-25 15:42:18 +00:00
short: json["short"].boolValue,
2022-12-10 00:23:13 +00:00
publishedAt: dateFormatter.date(from: json["publishedAt"].stringValue)
)
}
2022-12-09 00:15:19 +00:00
var instance: Instance! {
if let instance = InstancesModel.shared.find(instanceID) {
return instance
}
if let url = instanceURL?.absoluteString {
return Instance(app: app, id: instanceID, apiURLString: url, proxiesVideos: false)
}
return nil
}
2022-11-10 17:11:28 +00:00
var isLocal: Bool {
!VideoID.isValid(videoID) && videoID != Self.fixtureID
2022-11-10 17:11:28 +00:00
}
var displayTitle: String {
if isLocal {
return localStreamFileName ?? localStream?.description ?? title
}
return title
}
var displayAuthor: String {
if isLocal, localStreamIsRemoteURL {
return remoteUrlHost ?? "Unknown"
}
return author
}
2021-07-22 12:43:13 +00:00
var publishedDate: String? {
2022-12-19 11:13:27 +00:00
(published.isEmpty || published == "0 seconds ago") ? publishedAt?.timeIntervalSince1970.formattedAsRelativeTime() : published
2021-07-22 12:43:13 +00:00
}
2021-08-22 19:13:33 +00:00
var viewsCount: String? {
views != 0 ? views.formattedAsAbbreviation() : nil
2021-08-22 19:13:33 +00:00
}
var likesCount: String? {
2022-11-14 22:13:56 +00:00
guard let likes else {
2021-11-11 21:07:13 +00:00
return nil
}
2022-11-14 22:13:56 +00:00
return likes.formattedAsAbbreviation()
2021-08-22 19:13:33 +00:00
}
var dislikesCount: String? {
2022-11-18 21:43:16 +00:00
guard let dislikes else { return nil }
2021-11-11 21:07:13 +00:00
2022-11-18 21:43:16 +00:00
return dislikes.formattedAsAbbreviation()
2021-06-11 00:05:59 +00:00
}
2021-06-14 18:05:02 +00:00
2021-07-22 12:43:13 +00:00
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
2021-10-24 21:36:24 +00:00
thumbnails.first { $0.quality == quality }?.url
2021-07-07 22:39:18 +00:00
}
2021-06-14 18:05:02 +00:00
2023-06-17 12:09:51 +00:00
static func == (lhs: Self, rhs: Self) -> Bool {
let videoIDIsEqual = lhs.videoID == rhs.videoID
if !lhs.indexID.isNil, !rhs.indexID.isNil {
return videoIDIsEqual && lhs.indexID == rhs.indexID
}
return videoIDIsEqual
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
var watchFetchRequest: FetchRequest<Watch> {
FetchRequest<Watch>(
entity: Watch.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "videoID = %@", videoID)
)
}
2022-11-10 17:11:28 +00:00
var localStream: Stream? {
guard isLocal else { return nil }
return streams.first
}
2022-11-12 23:01:04 +00:00
var localStreamImageSystemName: String {
guard localStream != nil else { return "" }
if localStreamIsDirectory {
return "folder"
}
if localStreamIsFile {
return "doc"
}
return "globe"
}
2022-11-10 17:11:28 +00:00
var localStreamIsFile: Bool {
guard let localStream else { return false }
return localStream.localURL.isFileURL
}
var localStreamIsRemoteURL: Bool {
2023-04-23 11:11:30 +00:00
guard let url = localStream?.localURL else { return false }
return url.isFileURL
2022-11-10 17:11:28 +00:00
}
2022-11-12 23:01:04 +00:00
var localStreamIsDirectory: Bool {
guard let localStream else { return false }
#if os(iOS)
return DocumentsModel.shared.isDirectory(localStream.localURL)
#else
return false
#endif
}
2022-11-10 17:11:28 +00:00
var remoteUrlHost: String? {
localStreamURLComponents?.host
}
var localStreamFileName: String? {
guard let path = localStream?.localURL?.lastPathComponent else { return nil }
if let localStreamFileExtension {
return String(path.dropLast(localStreamFileExtension.count + 1))
}
return String(path)
}
var localStreamFileExtension: String? {
guard let path = localStreamURLComponents?.path else { return nil }
return path.contains(".") ? path.components(separatedBy: ".").last?.uppercased() : nil
}
2022-12-18 18:39:03 +00:00
var isShareable: Bool {
!isLocal || localStreamIsRemoteURL
}
2022-11-10 17:11:28 +00:00
private var localStreamURLComponents: URLComponents? {
guard let localStream else { return nil }
return URLComponents(url: localStream.localURL, resolvingAgainstBaseURL: false)
}
2021-06-10 22:50:10 +00:00
}