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

208 lines
5.2 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 {
2022-11-10 22:41:28 +05:30
enum VideoID {
static func isValid(_ id: Video.ID) -> Bool {
id.count == 11
}
}
2021-06-11 04:20:10 +05:30
let id: String
let videoID: String
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
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
2021-11-03 04:32:02 +05:30
var related = [Video]()
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(
id: String? = nil,
videoID: String,
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,
channel: Channel = .init(id: "", name: ""),
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,
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] = [],
related: [Video] = [],
2022-07-05 22:50:25 +05:30
chapters: [Chapter] = [],
captions: [Captions] = []
2021-07-22 18:13:13 +05:30
) {
self.id = id ?? UUID().uuidString
self.videoID = videoID
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
2021-08-26 03:42:59 +05:30
self.channel = channel
2021-07-22 18:13:13 +05:30
self.thumbnails = thumbnails
self.indexID = indexID
self.live = live
self.upcoming = upcoming
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
2022-11-10 22:41:28 +05:30
static func local(_ url: URL) -> Video {
Video(
videoID: url.absoluteString,
streams: [.init(localURL: url)]
)
}
var isLocal: Bool {
!VideoID.isValid(videoID)
}
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? {
(published.isEmpty || published == "0 seconds ago") ? nil : published
}
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? {
2021-12-19 21:45:27 +05:30
guard (likes ?? 0) > 0 else {
2021-11-12 02:37:13 +05:30
return nil
}
return likes?.formattedAsAbbreviation()
2021-08-23 00:43:33 +05:30
}
var dislikesCount: String? {
2021-12-19 21:45:27 +05:30
guard (dislikes ?? 0) > 0 else {
2021-11-12 02:37:13 +05:30
return nil
}
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
static func == (lhs: Video, rhs: Video) -> 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
}
var localStreamIsFile: Bool {
guard let localStream else { return false }
return localStream.localURL.isFileURL
}
var localStreamIsRemoteURL: Bool {
guard let localStream else { return false }
return !localStream.localURL.isFileURL
}
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
}
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
}