2021-11-02 19:40:49 +00:00
|
|
|
import AVFoundation
|
2021-10-20 22:21:50 +00:00
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
|
|
|
|
protocol VideosAPI {
|
2021-10-26 22:59:59 +00:00
|
|
|
var account: Account! { get }
|
2021-10-20 22:21:50 +00:00
|
|
|
var signedIn: Bool { get }
|
|
|
|
|
|
|
|
func channel(_ id: String) -> Resource
|
2021-11-01 21:56:18 +00:00
|
|
|
func channelVideos(_ id: String) -> Resource
|
2021-10-20 22:21:50 +00:00
|
|
|
func trending(country: Country, category: TrendingCategory?) -> Resource
|
2022-01-04 23:18:01 +00:00
|
|
|
func search(_ query: SearchQuery, page: String?) -> Resource
|
2021-10-20 22:21:50 +00:00
|
|
|
func searchSuggestions(query: String) -> Resource
|
|
|
|
|
|
|
|
func video(_ id: Video.ID) -> Resource
|
|
|
|
|
|
|
|
var subscriptions: Resource? { get }
|
|
|
|
var feed: Resource? { get }
|
|
|
|
var home: Resource? { get }
|
|
|
|
var popular: Resource? { get }
|
|
|
|
var playlists: Resource? { get }
|
|
|
|
|
2021-11-14 23:06:01 +00:00
|
|
|
func subscribe(_ channelID: String, onCompletion: @escaping () -> Void)
|
|
|
|
func unsubscribe(_ channelID: String, onCompletion: @escaping () -> Void)
|
2021-10-20 22:21:50 +00:00
|
|
|
|
2021-11-01 21:56:18 +00:00
|
|
|
func playlist(_ id: String) -> Resource?
|
2021-10-20 22:21:50 +00:00
|
|
|
func playlistVideo(_ playlistID: String, _ videoID: String) -> Resource?
|
|
|
|
func playlistVideos(_ id: String) -> Resource?
|
2021-10-22 23:04:03 +00:00
|
|
|
|
|
|
|
func channelPlaylist(_ id: String) -> Resource?
|
2021-10-24 18:01:08 +00:00
|
|
|
|
|
|
|
func loadDetails(_ item: PlayerQueueItem, completionHandler: @escaping (PlayerQueueItem) -> Void)
|
2021-11-02 19:40:49 +00:00
|
|
|
func shareURL(_ item: ContentItem, frontendHost: String?, time: CMTime?) -> URL?
|
2021-12-04 19:35:41 +00:00
|
|
|
|
|
|
|
func comments(_ id: Video.ID, page: String?) -> Resource?
|
2021-10-24 18:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension VideosAPI {
|
|
|
|
func loadDetails(_ item: PlayerQueueItem, completionHandler: @escaping (PlayerQueueItem) -> Void = { _ in }) {
|
|
|
|
guard (item.video?.streams ?? []).isEmpty else {
|
|
|
|
completionHandler(item)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
video(item.videoID).load().onSuccess { response in
|
|
|
|
guard let video: Video = response.typedContent() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var newItem = item
|
|
|
|
newItem.video = video
|
|
|
|
|
|
|
|
completionHandler(newItem)
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:59:59 +00:00
|
|
|
|
2021-11-02 19:40:49 +00:00
|
|
|
func shareURL(_ item: ContentItem, frontendHost: String? = nil, time: CMTime? = nil) -> URL? {
|
2021-12-17 20:01:05 +00:00
|
|
|
guard let frontendHost = frontendHost ?? account?.instance?.frontendHost,
|
|
|
|
var urlComponents = account?.instance?.urlComponents
|
|
|
|
else {
|
2021-10-28 17:14:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
urlComponents.host = frontendHost
|
2021-10-27 21:11:38 +00:00
|
|
|
|
2021-11-02 19:40:49 +00:00
|
|
|
var queryItems = [URLQueryItem]()
|
|
|
|
|
2021-10-26 22:59:59 +00:00
|
|
|
switch item.contentType {
|
|
|
|
case .video:
|
|
|
|
urlComponents.path = "/watch"
|
2021-11-02 19:40:49 +00:00
|
|
|
queryItems.append(.init(name: "v", value: item.video.videoID))
|
2021-10-26 22:59:59 +00:00
|
|
|
case .channel:
|
|
|
|
urlComponents.path = "/channel/\(item.channel.id)"
|
|
|
|
case .playlist:
|
|
|
|
urlComponents.path = "/playlist"
|
2021-11-02 19:40:49 +00:00
|
|
|
queryItems.append(.init(name: "list", value: item.playlist.id))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !time.isNil, time!.seconds.isFinite {
|
|
|
|
queryItems.append(.init(name: "t", value: "\(Int(time!.seconds))s"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !queryItems.isEmpty {
|
|
|
|
urlComponents.queryItems = queryItems
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 15:45:47 +00:00
|
|
|
return urlComponents.url
|
2021-10-26 22:59:59 +00:00
|
|
|
}
|
2021-10-20 22:21:50 +00:00
|
|
|
}
|