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 }
|
|
|
|
|
2022-12-09 05:45:19 +05:30
|
|
|
static func withAnonymousAccountForInstanceURL(_ url: URL) -> Self
|
|
|
|
|
2023-03-01 01:33:02 +05:30
|
|
|
func channel(_ id: String, contentType: Channel.ContentType, data: String?, page: String?) -> Resource
|
2022-06-25 04:18:57 +05:30
|
|
|
func channelByName(_ name: String) -> Resource?
|
2022-06-30 05:01:51 +05:30
|
|
|
func channelByUsername(_ username: 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
|
|
|
|
|
2022-12-10 07:31:59 +05:30
|
|
|
func feed(_ page: Int?) -> Resource?
|
2021-10-21 03:51:50 +05:30
|
|
|
var subscriptions: 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
|
|
|
|
2022-05-22 03:59:51 +05:30
|
|
|
func addVideoToPlaylist(
|
|
|
|
_ videoID: String,
|
|
|
|
_ playlistID: String,
|
|
|
|
onFailure: @escaping (RequestError) -> Void,
|
|
|
|
onSuccess: @escaping () -> Void
|
|
|
|
)
|
|
|
|
|
|
|
|
func removeVideoFromPlaylist(
|
|
|
|
_ index: String,
|
|
|
|
_ playlistID: String,
|
|
|
|
onFailure: @escaping (RequestError) -> Void,
|
|
|
|
onSuccess: @escaping () -> Void
|
|
|
|
)
|
|
|
|
|
|
|
|
func playlistForm(
|
|
|
|
_ name: String,
|
|
|
|
_ visibility: String,
|
|
|
|
playlist: Playlist?,
|
|
|
|
onFailure: @escaping (RequestError) -> Void,
|
|
|
|
onSuccess: @escaping (Playlist?) -> Void
|
|
|
|
)
|
|
|
|
|
|
|
|
func deletePlaylist(
|
|
|
|
_ playlist: Playlist,
|
|
|
|
onFailure: @escaping (RequestError) -> Void,
|
|
|
|
onSuccess: @escaping () -> Void
|
|
|
|
)
|
|
|
|
|
2021-10-23 04:34:03 +05:30
|
|
|
func channelPlaylist(_ id: String) -> Resource?
|
2021-10-24 23:31:08 +05:30
|
|
|
|
2022-06-30 04:14:32 +05:30
|
|
|
func loadDetails(
|
|
|
|
_ item: PlayerQueueItem,
|
|
|
|
failureHandler: ((RequestError) -> Void)?,
|
|
|
|
completionHandler: @escaping (PlayerQueueItem) -> Void
|
|
|
|
)
|
2024-05-16 22:23:47 +05:30
|
|
|
func shareURL(_ item: ContentItem, frontendURLString: 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 {
|
2023-03-01 01:33:02 +05:30
|
|
|
func channel(_ id: String, contentType: Channel.ContentType, data: String? = nil, page: String? = nil) -> Resource {
|
|
|
|
channel(id, contentType: contentType, data: data, page: page)
|
2022-11-27 16:12:16 +05:30
|
|
|
}
|
|
|
|
|
2022-06-30 04:14:32 +05:30
|
|
|
func loadDetails(
|
|
|
|
_ item: PlayerQueueItem,
|
|
|
|
failureHandler: ((RequestError) -> Void)? = nil,
|
|
|
|
completionHandler: @escaping (PlayerQueueItem) -> Void = { _ in }
|
|
|
|
) {
|
2021-10-24 23:31:08 +05:30
|
|
|
guard (item.video?.streams ?? []).isEmpty else {
|
|
|
|
completionHandler(item)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
if let video = item.video, video.isLocal {
|
|
|
|
completionHandler(item)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 22:56:03 +05:30
|
|
|
video(item.videoID).load()
|
|
|
|
.onSuccess { response in
|
|
|
|
guard let video: Video = response.typedContent() else {
|
|
|
|
return
|
|
|
|
}
|
2021-10-24 23:31:08 +05:30
|
|
|
|
2022-12-13 05:08:26 +05:30
|
|
|
VideosCacheModel.shared.storeVideo(video)
|
|
|
|
|
2022-07-05 22:56:03 +05:30
|
|
|
var newItem = item
|
2022-11-10 22:41:28 +05:30
|
|
|
newItem.id = UUID()
|
2022-07-05 22:56:03 +05:30
|
|
|
newItem.video = video
|
2021-10-24 23:31:08 +05:30
|
|
|
|
2022-07-05 22:56:03 +05:30
|
|
|
completionHandler(newItem)
|
|
|
|
}
|
|
|
|
.onFailure { failureHandler?($0) }
|
2021-10-24 23:31:08 +05:30
|
|
|
}
|
2021-10-27 04:29:59 +05:30
|
|
|
|
2024-05-16 21:55:08 +05:30
|
|
|
func shareURL(_ item: ContentItem, frontendURLString: String? = nil, time: CMTime? = nil) -> URL? {
|
2024-03-21 08:51:26 +05:30
|
|
|
var urlComponents: URLComponents?
|
2024-05-16 21:55:08 +05:30
|
|
|
if let frontendURLString,
|
2024-05-16 21:58:32 +05:30
|
|
|
let frontendURL = URL(string: frontendURLString)
|
|
|
|
{
|
2024-05-16 22:23:47 +05:30
|
|
|
urlComponents = URLComponents(url: frontendURL, resolvingAgainstBaseURL: false)
|
2024-03-21 08:51:26 +05:30
|
|
|
} else if let instanceComponents = account?.instance?.urlComponents {
|
|
|
|
urlComponents = instanceComponents
|
2021-10-28 22:44:55 +05:30
|
|
|
}
|
2024-05-16 21:58:32 +05:30
|
|
|
|
2024-05-16 21:55:15 +05:30
|
|
|
guard var urlComponents else {
|
2024-03-21 08:51:26 +05:30
|
|
|
return nil
|
2024-03-19 10:28:16 +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
|
|
|
}
|
2022-06-18 18:09:49 +05:30
|
|
|
|
|
|
|
func extractChapters(from description: String) -> [Chapter] {
|
2023-12-28 03:06:52 +05:30
|
|
|
/*
|
|
|
|
The following chapter patterns are covered:
|
|
|
|
|
|
|
|
start - end - title / start - end: Title / start - end title
|
|
|
|
start - title / start: title / start title / [start] - title / [start]: title / [start] title
|
|
|
|
index. title - start / index. title start
|
|
|
|
title: (start)
|
|
|
|
|
|
|
|
The order is important!
|
|
|
|
*/
|
|
|
|
let patterns = [
|
|
|
|
"(?<=\\n|^)\\s*(?:►\\s*)?\\[?(?<start>(?:[0-9]+:){1,2}[0-9]+)\\]?(?:\\s*-\\s*)?(?<end>(?:[0-9]+:){1,2}[0-9]+)?(?:\\s*-\\s*|\\s*[:]\\s*)?(?<title>.*)(?=\\n|$)",
|
|
|
|
"(?<=\\n|^)\\s*(?:►\\s*)?\\[?(?<start>(?:[0-9]+:){1,2}[0-9]+)\\]?\\s*[-:]?\\s*(?<title>.+)(?=\\n|$)",
|
|
|
|
"(?<=\\n|^)(?<index>[0-9]+\\.\\s)(?<title>.+?)(?:\\s*-\\s*)?(?<start>(?:[0-9]+:){1,2}[0-9]+)(?=\\n|$)",
|
|
|
|
"(?<=\\n|^)(?<title>.+?):\\s*\\((?<start>(?:[0-9]+:){1,2}[0-9]+)\\)(?=\\n|$)"
|
|
|
|
]
|
|
|
|
|
|
|
|
for pattern in patterns {
|
|
|
|
guard let chaptersRegularExpression = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else { continue }
|
|
|
|
let chapterLines = chaptersRegularExpression.matches(in: description, range: NSRange(description.startIndex..., in: description))
|
|
|
|
|
|
|
|
if !chapterLines.isEmpty {
|
|
|
|
return chapterLines.compactMap { line in
|
|
|
|
let titleRange = line.range(withName: "title")
|
|
|
|
let startRange = line.range(withName: "start")
|
|
|
|
guard let titleSubstringRange = Range(titleRange, in: description),
|
|
|
|
let startSubstringRange = Range(startRange, in: description)
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let titleCapture = String(description[titleSubstringRange]).trimmingCharacters(in: .whitespaces)
|
|
|
|
let startCapture = String(description[startSubstringRange])
|
|
|
|
let startComponents = startCapture.components(separatedBy: ":")
|
|
|
|
guard startComponents.count <= 3 else { return nil }
|
|
|
|
|
|
|
|
var hours: Double?
|
|
|
|
var minutes: Double?
|
|
|
|
var seconds: Double?
|
|
|
|
|
|
|
|
if startComponents.count == 3 {
|
|
|
|
hours = Double(startComponents[0])
|
|
|
|
minutes = Double(startComponents[1])
|
|
|
|
seconds = Double(startComponents[2])
|
|
|
|
} else if startComponents.count == 2 {
|
|
|
|
minutes = Double(startComponents[0])
|
|
|
|
seconds = Double(startComponents[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
guard var startSeconds = seconds else { return nil }
|
|
|
|
|
|
|
|
startSeconds += (minutes ?? 0) * 60
|
|
|
|
startSeconds += (hours ?? 0) * 60 * 60
|
|
|
|
|
|
|
|
return .init(title: titleCapture, start: startSeconds)
|
|
|
|
}
|
2022-06-18 18:09:49 +05:30
|
|
|
}
|
|
|
|
}
|
2023-12-28 03:06:52 +05:30
|
|
|
return []
|
2022-06-18 18:09:49 +05:30
|
|
|
}
|
2021-10-21 03:51:50 +05:30
|
|
|
}
|