2021-11-03 01:10:49 +05:30
|
|
|
import AVFoundation
|
2021-10-21 03:51:50 +05:30
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
|
|
|
|
protocol VideosAPI {
|
2021-10-27 04:29:59 +05:30
|
|
|
var account: Account! { get }
|
2021-10-21 03:51:50 +05:30
|
|
|
var signedIn: Bool { get }
|
|
|
|
|
|
|
|
func channel(_ id: String) -> Resource
|
2021-11-02 03:26:18 +05:30
|
|
|
func channelVideos(_ id: String) -> Resource
|
2021-10-21 03:51:50 +05:30
|
|
|
func trending(country: Country, category: TrendingCategory?) -> Resource
|
2022-01-05 04:48:01 +05:30
|
|
|
func search(_ query: SearchQuery, page: String?) -> Resource
|
2021-10-21 03:51:50 +05:30
|
|
|
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-15 04:36:01 +05:30
|
|
|
func subscribe(_ channelID: String, onCompletion: @escaping () -> Void)
|
|
|
|
func unsubscribe(_ channelID: String, onCompletion: @escaping () -> Void)
|
2021-10-21 03:51:50 +05:30
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
func playlist(_ id: String) -> Resource?
|
2021-10-21 03:51:50 +05:30
|
|
|
func playlistVideo(_ playlistID: String, _ videoID: String) -> Resource?
|
|
|
|
func playlistVideos(_ id: String) -> Resource?
|
2021-10-23 04:34:03 +05:30
|
|
|
|
|
|
|
func channelPlaylist(_ id: String) -> Resource?
|
2021-10-24 23:31:08 +05:30
|
|
|
|
|
|
|
func loadDetails(_ item: PlayerQueueItem, completionHandler: @escaping (PlayerQueueItem) -> Void)
|
2021-11-03 01:10:49 +05:30
|
|
|
func shareURL(_ item: ContentItem, frontendHost: String?, time: CMTime?) -> URL?
|
2021-12-05 01:05:41 +05:30
|
|
|
|
|
|
|
func comments(_ id: Video.ID, page: String?) -> Resource?
|
2021-10-24 23:31:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-27 04:29:59 +05:30
|
|
|
|
2021-11-03 01:10:49 +05:30
|
|
|
func shareURL(_ item: ContentItem, frontendHost: String? = nil, time: CMTime? = nil) -> URL? {
|
2021-12-18 01:31:05 +05:30
|
|
|
guard let frontendHost = frontendHost ?? account?.instance?.frontendHost,
|
|
|
|
var urlComponents = account?.instance?.urlComponents
|
|
|
|
else {
|
2021-10-28 22:44:55 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
urlComponents.host = frontendHost
|
2021-10-28 02:41:38 +05:30
|
|
|
|
2021-11-03 01:10:49 +05:30
|
|
|
var queryItems = [URLQueryItem]()
|
|
|
|
|
2021-10-27 04:29:59 +05:30
|
|
|
switch item.contentType {
|
|
|
|
case .video:
|
|
|
|
urlComponents.path = "/watch"
|
2021-11-03 01:10:49 +05:30
|
|
|
queryItems.append(.init(name: "v", value: item.video.videoID))
|
2021-10-27 04:29:59 +05:30
|
|
|
case .channel:
|
|
|
|
urlComponents.path = "/channel/\(item.channel.id)"
|
|
|
|
case .playlist:
|
|
|
|
urlComponents.path = "/playlist"
|
2021-11-03 01:10:49 +05:30
|
|
|
queryItems.append(.init(name: "list", value: item.playlist.id))
|
2022-03-27 16:19:57 +05:30
|
|
|
default:
|
|
|
|
return nil
|
2021-11-03 01:10:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if !time.isNil, time!.seconds.isFinite {
|
|
|
|
queryItems.append(.init(name: "t", value: "\(Int(time!.seconds))s"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !queryItems.isEmpty {
|
|
|
|
urlComponents.queryItems = queryItems
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
|
|
|
|
2021-11-13 21:15:47 +05:30
|
|
|
return urlComponents.url
|
2021-10-27 04:29:59 +05:30
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
}
|