1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-12 21:30:32 +05:30
yattee/Model/Video.swift

338 lines
9.3 KiB
Swift
Raw Normal View History

2021-06-11 04:20:10 +05:30
import Alamofire
2021-06-14 23:35:02 +05:30
import AVKit
2021-06-11 04:20:10 +05:30
import Foundation
import SwiftUI
2021-06-11 04:20:10 +05:30
import SwiftyJSON
struct Video: Identifiable, Equatable, Hashable {
2023-02-25 21:12:18 +05:30
static let shortLength = 61.0
2022-11-10 22:41:28 +05:30
enum VideoID {
static func isValid(_ id: Video.ID) -> Bool {
2022-12-09 05:45:19 +05:30
isYouTube(id) || isPeerTube(id)
}
static func isYouTube(_ id: Video.ID) -> Bool {
2022-11-10 22:41:28 +05:30
id.count == 11
}
2022-12-09 05:45:19 +05:30
static func isPeerTube(_ id: Video.ID) -> Bool {
id.count == 36
}
2022-11-10 22:41:28 +05:30
}
2022-12-09 05:45:19 +05:30
var instanceID: Instance.ID?
var app: VideosApp
var instanceURL: URL?
var id: String
var videoID: String
var videoURL: URL?
2021-06-11 04:20:10 +05:30
var title: String
2021-07-08 04:09:18 +05:30
var thumbnails: [Thumbnail]
2021-06-11 04:20:10 +05:30
var author: String
2021-06-11 05:35:59 +05:30
var length: TimeInterval
var published: String
var views: Int
2021-10-21 03:51:50 +05:30
var description: String?
var genre: String?
2021-06-12 02:41:59 +05:30
2021-07-22 18:13:13 +05:30
// index used when in the Playlist
var indexID: String?
2021-07-22 18:13:13 +05:30
var live: Bool
var upcoming: Bool
2023-02-25 21:12:18 +05:30
var short: Bool
2021-07-22 18:13:13 +05:30
2021-06-14 23:35:02 +05:30
var streams = [Stream]()
2021-07-22 18:13:13 +05:30
2021-08-23 00:43:33 +05:30
var publishedAt: Date?
var likes: Int?
var dislikes: Int?
var keywords = [String]()
2021-08-26 03:42:59 +05:30
var channel: Channel
2023-09-23 18:45:21 +05:30
var related = [Self]()
var chapters = [Chapter]()
2021-11-03 04:32:02 +05:30
2022-07-05 22:50:25 +05:30
var captions = [Captions]()
2021-07-22 18:13:13 +05:30
init(
2022-12-09 05:45:19 +05:30
instanceID: Instance.ID? = nil,
app: VideosApp,
instanceURL: URL? = nil,
id: String? = nil,
videoID: String,
2022-12-09 05:45:19 +05:30
videoURL: URL? = nil,
title: String = "",
author: String = "",
length: TimeInterval = .zero,
published: String = "",
views: Int = 0,
2021-10-21 03:51:50 +05:30
description: String? = nil,
genre: String? = nil,
2022-12-14 04:37:32 +05:30
channel: Channel? = nil,
2021-07-22 18:13:13 +05:30
thumbnails: [Thumbnail] = [],
indexID: String? = nil,
live: Bool = false,
2021-08-23 00:43:33 +05:30
upcoming: Bool = false,
2023-02-25 21:12:18 +05:30
short: Bool = false,
2021-08-23 00:43:33 +05:30
publishedAt: Date? = nil,
likes: Int? = nil,
dislikes: Int? = nil,
2021-10-21 03:51:50 +05:30
keywords: [String] = [],
2021-11-03 04:32:02 +05:30
streams: [Stream] = [],
2023-06-17 17:39:51 +05:30
related: [Self] = [],
2022-07-05 22:50:25 +05:30
chapters: [Chapter] = [],
captions: [Captions] = []
2021-07-22 18:13:13 +05:30
) {
2022-12-09 05:45:19 +05:30
self.instanceID = instanceID
self.app = app
self.instanceURL = instanceURL
self.id = id ?? UUID().uuidString
self.videoID = videoID
2022-12-09 05:45:19 +05:30
self.videoURL = videoURL
2021-07-22 18:13:13 +05:30
self.title = title
self.author = author
self.length = length
self.published = published
self.views = views
self.description = description
self.genre = genre
2022-12-14 04:37:32 +05:30
self.channel = channel ?? .init(app: app, id: "", name: "")
2021-07-22 18:13:13 +05:30
self.thumbnails = thumbnails
self.indexID = indexID
self.live = live
self.upcoming = upcoming
2023-02-25 21:12:18 +05:30
self.short = short
2021-08-23 00:43:33 +05:30
self.publishedAt = publishedAt
self.likes = likes
self.dislikes = dislikes
self.keywords = keywords
2021-10-21 03:51:50 +05:30
self.streams = streams
2021-11-03 04:32:02 +05:30
self.related = related
self.chapters = chapters
2022-07-05 22:50:25 +05:30
self.captions = captions
2021-07-22 18:13:13 +05:30
}
2021-06-11 04:20:10 +05:30
2023-06-17 17:39:51 +05:30
static func local(_ url: URL) -> Self {
2023-04-22 18:38:33 +05:30
Self(
2022-12-09 05:45:19 +05:30
app: .local,
2022-11-10 22:41:28 +05:30
videoID: url.absoluteString,
streams: [.init(localURL: url)]
)
}
2022-12-10 05:53:13 +05:30
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 21:12:18 +05:30
"short": short,
2022-12-10 05:53:13 +05:30
"publishedAt": publishedAt
]
}
static func from(_ json: JSON) -> Self {
let dateFormatter = ISO8601DateFormatter()
2023-04-22 18:38:33 +05:30
return Self(
2022-12-10 05:53:13 +05:30
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 21:12:18 +05:30
short: json["short"].boolValue,
2022-12-10 05:53:13 +05:30
publishedAt: dateFormatter.date(from: json["publishedAt"].stringValue)
)
}
2022-12-09 05:45:19 +05:30
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 22:41:28 +05:30
var isLocal: Bool {
!VideoID.isValid(videoID) && videoID != Self.fixtureID
2022-11-10 22:41:28 +05:30
}
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 18:13:13 +05:30
var publishedDate: String? {
2022-12-19 16:43:27 +05:30
(published.isEmpty || published == "0 seconds ago") ? publishedAt?.timeIntervalSince1970.formattedAsRelativeTime() : published
2021-07-22 18:13:13 +05:30
}
2021-08-23 00:43:33 +05:30
var viewsCount: String? {
views != 0 ? views.formattedAsAbbreviation() : nil
2021-08-23 00:43:33 +05:30
}
var likesCount: String? {
2022-11-15 03:43:56 +05:30
guard let likes else {
2021-11-12 02:37:13 +05:30
return nil
}
2022-11-15 03:43:56 +05:30
return likes.formattedAsAbbreviation()
2021-08-23 00:43:33 +05:30
}
var dislikesCount: String? {
2022-11-19 03:13:16 +05:30
guard let dislikes else { return nil }
2021-11-12 02:37:13 +05:30
2022-11-19 03:13:16 +05:30
return dislikes.formattedAsAbbreviation()
2021-06-11 05:35:59 +05:30
}
2021-06-14 23:35:02 +05:30
2021-07-22 18:13:13 +05:30
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
2021-10-25 03:06:24 +05:30
thumbnails.first { $0.quality == quality }?.url
2021-07-08 04:09:18 +05:30
}
2021-06-14 23:35:02 +05:30
2023-06-17 17:39:51 +05:30
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 22:41:28 +05:30
var localStream: Stream? {
guard isLocal else { return nil }
return streams.first
}
2022-11-13 04:31:04 +05:30
var localStreamImageSystemName: String {
guard localStream != nil else { return "" }
if localStreamIsDirectory {
return "folder"
}
if localStreamIsFile {
return "doc"
}
return "globe"
}
2022-11-10 22:41:28 +05:30
var localStreamIsFile: Bool {
2023-10-15 03:59:09 +05:30
guard let url = localStream?.localURL else { return false }
return url.isFileURL
2022-11-10 22:41:28 +05:30
}
var localStreamIsRemoteURL: Bool {
2023-04-23 16:41:30 +05:30
guard let url = localStream?.localURL else { return false }
return url.isFileURL
2022-11-10 22:41:28 +05:30
}
2022-11-13 04:31:04 +05:30
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 22:41:28 +05:30
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-19 00:09:03 +05:30
var isShareable: Bool {
!isLocal || localStreamIsRemoteURL
}
2022-11-10 22:41:28 +05:30
private var localStreamURLComponents: URLComponents? {
guard let localStream else { return nil }
return URLComponents(url: localStream.localURL, resolvingAgainstBaseURL: false)
}
2021-06-11 04:20:10 +05:30
}