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

92 lines
2.6 KiB
Swift
Raw Normal View History

2021-06-11 04:20:10 +05:30
import Alamofire
import Foundation
import SwiftyJSON
2021-06-11 05:35:59 +05:30
final class Video: Identifiable, ObservableObject {
2021-06-11 04:20:10 +05:30
let id: String
var title: String
2021-06-12 02:41:59 +05:30
var thumbnailURL: URL?
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-06-11 04:20:10 +05:30
2021-06-12 02:41:59 +05:30
var channelID: String
2021-06-11 04:20:10 +05:30
@Published var url: URL?
@Published var error: Bool = false
2021-06-12 02:41:59 +05:30
init(id: String, title: String, thumbnailURL: URL?, author: String, length: TimeInterval, published: String, channelID: String, views: Int = 0) {
2021-06-11 04:20:10 +05:30
self.id = id
self.title = title
self.thumbnailURL = thumbnailURL
self.author = author
2021-06-11 05:35:59 +05:30
self.length = length
self.published = published
2021-06-12 02:41:59 +05:30
self.channelID = channelID
2021-06-11 05:35:59 +05:30
self.views = views
2021-06-11 04:20:10 +05:30
}
init(_ json: JSON) {
2021-06-12 02:41:59 +05:30
func extractThumbnailURL(from details: JSON) -> URL? {
2021-06-11 18:06:26 +05:30
if details["videoThumbnails"].arrayValue.isEmpty {
2021-06-12 02:41:59 +05:30
return nil
2021-06-11 18:06:26 +05:30
}
2021-06-12 02:41:59 +05:30
let thumbnail = details["videoThumbnails"].arrayValue.first(where: { $0["quality"].stringValue == "medium" })!
return thumbnail["url"].url!
2021-06-11 18:06:26 +05:30
}
2021-06-12 02:41:59 +05:30
2021-06-11 18:06:26 +05:30
func extractFormatStreamURL(from streams: [JSON]) -> URL? {
if streams.isEmpty {
error = true
return nil
}
let stream = streams.last!
return stream["url"].url
}
2021-06-12 02:41:59 +05:30
2021-06-11 04:20:10 +05:30
id = json["videoId"].stringValue
title = json["title"].stringValue
2021-06-11 18:06:26 +05:30
thumbnailURL = extractThumbnailURL(from: json)
2021-06-11 04:20:10 +05:30
author = json["author"].stringValue
2021-06-11 05:35:59 +05:30
length = json["lengthSeconds"].doubleValue
published = json["publishedText"].stringValue
views = json["viewCount"].intValue
2021-06-12 02:41:59 +05:30
channelID = json["authorId"].stringValue
2021-06-11 05:35:59 +05:30
2021-06-11 18:06:26 +05:30
url = extractFormatStreamURL(from: json["formatStreams"].arrayValue)
2021-06-11 04:20:10 +05:30
}
2021-06-11 05:35:59 +05:30
var playTime: String? {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = length >= (60 * 60) ? [.hour, .minute, .second] : [.minute, .second]
formatter.zeroFormattingBehavior = [.pad]
return formatter.string(from: length)
}
var viewsCount: String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 1
var number: NSNumber
var unit: String
if views < 1_000_000 {
number = NSNumber(value: Double(views) / 1000.0)
unit = "K"
} else {
number = NSNumber(value: Double(views) / 1_000_000.0)
unit = "M"
}
return "\(formatter.string(from: number)!)\(unit)"
}
2021-06-11 04:20:10 +05:30
}