mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 10:44:06 +00:00
Initial PeerTube Support
This commit is contained in:
@@ -8,7 +8,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
var app: VideosApp?
|
||||
let instanceID: String?
|
||||
var name: String?
|
||||
let url: String
|
||||
let urlString: String
|
||||
var username: String
|
||||
var password: String?
|
||||
let anonymous: Bool
|
||||
@@ -20,7 +20,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
app: VideosApp? = nil,
|
||||
instanceID: String? = nil,
|
||||
name: String? = nil,
|
||||
url: String? = nil,
|
||||
urlString: String? = nil,
|
||||
username: String? = nil,
|
||||
password: String? = nil,
|
||||
anonymous: Bool = false,
|
||||
@@ -29,10 +29,10 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
) {
|
||||
self.anonymous = anonymous
|
||||
|
||||
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? url ?? UUID().uuidString)" : UUID().uuidString)
|
||||
self.id = id ?? (anonymous ? "anonymous-\(instanceID ?? urlString ?? UUID().uuidString)" : UUID().uuidString)
|
||||
self.instanceID = instanceID
|
||||
self.name = name
|
||||
self.url = url ?? ""
|
||||
self.urlString = urlString ?? ""
|
||||
self.username = username ?? ""
|
||||
self.password = password ?? ""
|
||||
self.country = country
|
||||
@@ -40,6 +40,10 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
self.app = app ?? instance.app
|
||||
}
|
||||
|
||||
var url: URL! {
|
||||
URL(string: urlString)
|
||||
}
|
||||
|
||||
var token: String? {
|
||||
KeychainModel.shared.getAccountKey(self, "token")
|
||||
}
|
||||
@@ -49,7 +53,7 @@ struct Account: Defaults.Serializable, Hashable, Identifiable {
|
||||
}
|
||||
|
||||
var instance: Instance! {
|
||||
Defaults[.instances].first { $0.id == instanceID } ?? Instance(app: app ?? .invidious, name: url, apiURL: url)
|
||||
Defaults[.instances].first { $0.id == instanceID } ?? Instance(app: app ?? .invidious, name: urlString, apiURLString: urlString)
|
||||
}
|
||||
|
||||
var isPublic: Bool {
|
||||
|
@@ -56,12 +56,23 @@ final class AccountValidator: Service {
|
||||
|
||||
case .piped:
|
||||
return resource("/streams/dQw4w9WgXcQ")
|
||||
|
||||
case .peerTube:
|
||||
// TODO: fixme
|
||||
return resource("")
|
||||
|
||||
case .local:
|
||||
return resource("")
|
||||
}
|
||||
}
|
||||
|
||||
func validateInstance() {
|
||||
reset()
|
||||
|
||||
app.wrappedValue = .peerTube
|
||||
setValidationResult(true)
|
||||
return
|
||||
|
||||
guard let app = appsToValidateInstance.popLast() else { return }
|
||||
tryValidatingUsing(app)
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ struct AccountsBridge: Defaults.Bridge {
|
||||
"id": value.id,
|
||||
"instanceID": value.instanceID ?? "",
|
||||
"name": value.name ?? "",
|
||||
"apiURL": value.url,
|
||||
"apiURL": value.urlString,
|
||||
"username": value.username,
|
||||
"password": value.password ?? ""
|
||||
]
|
||||
@@ -34,6 +34,6 @@ struct AccountsBridge: Defaults.Bridge {
|
||||
let name = object["name"] ?? ""
|
||||
let password = object["password"]
|
||||
|
||||
return Account(id: id, instanceID: instanceID, name: name, url: url, username: username, password: password)
|
||||
return Account(id: id, instanceID: instanceID, name: name, urlString: url, username: username, password: password)
|
||||
}
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ final class AccountsModel: ObservableObject {
|
||||
|
||||
@Published private var invidious = InvidiousAPI()
|
||||
@Published private var piped = PipedAPI()
|
||||
@Published private var peerTube = PeerTubeAPI()
|
||||
|
||||
@Published var publicAccount: Account?
|
||||
|
||||
@@ -31,15 +32,19 @@ final class AccountsModel: ObservableObject {
|
||||
}
|
||||
|
||||
var app: VideosApp {
|
||||
current?.instance?.app ?? .invidious
|
||||
current?.instance?.app ?? .local
|
||||
}
|
||||
|
||||
var api: VideosAPI {
|
||||
var api: VideosAPI! {
|
||||
switch app {
|
||||
case .piped:
|
||||
return piped
|
||||
case .invidious:
|
||||
return invidious
|
||||
case .peerTube:
|
||||
return peerTube
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +88,14 @@ final class AccountsModel: ObservableObject {
|
||||
}
|
||||
|
||||
switch account.instance.app {
|
||||
case .local:
|
||||
return
|
||||
case .invidious:
|
||||
invidious.setAccount(account)
|
||||
case .piped:
|
||||
piped.setAccount(account)
|
||||
case .peerTube:
|
||||
peerTube.setAccount(account)
|
||||
}
|
||||
|
||||
Defaults[.lastAccountIsPublic] = account.isPublic
|
||||
@@ -102,7 +111,7 @@ final class AccountsModel: ObservableObject {
|
||||
}
|
||||
|
||||
static func add(instance: Instance, name: String, username: String, password: String) -> Account {
|
||||
let account = Account(instanceID: instance.id, name: name, url: instance.apiURL)
|
||||
let account = Account(instanceID: instance.id, name: name, urlString: instance.apiURLString)
|
||||
Defaults[.accounts].append(account)
|
||||
|
||||
setCredentials(account, username: username, password: password)
|
||||
|
@@ -7,25 +7,33 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
let app: VideosApp
|
||||
let id: String
|
||||
let name: String
|
||||
let apiURL: String
|
||||
let apiURLString: String
|
||||
var frontendURL: String?
|
||||
var proxiesVideos: Bool
|
||||
|
||||
init(app: VideosApp, id: String? = nil, name: String, apiURL: String, frontendURL: String? = nil, proxiesVideos: Bool = false) {
|
||||
init(app: VideosApp, id: String? = nil, name: String? = nil, apiURLString: String, frontendURL: String? = nil, proxiesVideos: Bool = false) {
|
||||
self.app = app
|
||||
self.id = id ?? UUID().uuidString
|
||||
self.name = name
|
||||
self.apiURL = apiURL
|
||||
self.name = name ?? app.rawValue
|
||||
self.apiURLString = apiURLString
|
||||
self.frontendURL = frontendURL
|
||||
self.proxiesVideos = proxiesVideos
|
||||
}
|
||||
|
||||
var anonymous: VideosAPI {
|
||||
var apiURL: URL! {
|
||||
URL(string: apiURLString)
|
||||
}
|
||||
|
||||
var anonymous: VideosAPI! {
|
||||
switch app {
|
||||
case .invidious:
|
||||
return InvidiousAPI(account: anonymousAccount)
|
||||
case .piped:
|
||||
return PipedAPI(account: anonymousAccount)
|
||||
case .peerTube:
|
||||
return PeerTubeAPI(account: anonymousAccount)
|
||||
case .local:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,23 +42,23 @@ struct Instance: Defaults.Serializable, Hashable, Identifiable {
|
||||
}
|
||||
|
||||
var longDescription: String {
|
||||
name.isEmpty ? "\(app.name) - \(apiURL)" : "\(app.name) - \(name) (\(apiURL))"
|
||||
name.isEmpty ? "\(app.name) - \(apiURLString)" : "\(app.name) - \(name) (\(apiURL))"
|
||||
}
|
||||
|
||||
var shortDescription: String {
|
||||
name.isEmpty ? apiURL : name
|
||||
name.isEmpty ? apiURLString : name
|
||||
}
|
||||
|
||||
var anonymousAccount: Account {
|
||||
Account(instanceID: id, name: "Anonymous".localized(), url: apiURL, anonymous: true)
|
||||
Account(instanceID: id, name: "Anonymous".localized(), urlString: apiURLString, anonymous: true)
|
||||
}
|
||||
|
||||
var urlComponents: URLComponents {
|
||||
URLComponents(string: apiURL)!
|
||||
URLComponents(url: apiURL, resolvingAgainstBaseURL: false)!
|
||||
}
|
||||
|
||||
var frontendHost: String? {
|
||||
guard let url = app == .invidious ? apiURL : frontendURL else {
|
||||
guard let url = app == .invidious ? apiURLString : frontendURL else {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,7 @@ struct InstancesBridge: Defaults.Bridge {
|
||||
"app": value.app.rawValue,
|
||||
"id": value.id,
|
||||
"name": value.name,
|
||||
"apiURL": value.apiURL,
|
||||
"apiURL": value.apiURLString,
|
||||
"frontendURL": value.frontendURL ?? "",
|
||||
"proxiesVideos": value.proxiesVideos ? "true" : "false"
|
||||
]
|
||||
@@ -34,6 +34,6 @@ struct InstancesBridge: Defaults.Bridge {
|
||||
let frontendURL: String? = object["frontendURL"]!.isEmpty ? nil : object["frontendURL"]
|
||||
let proxiesVideos = object["proxiesVideos"] == "true"
|
||||
|
||||
return Instance(app: app, id: id, name: name, apiURL: apiURL, frontendURL: frontendURL, proxiesVideos: proxiesVideos)
|
||||
return Instance(app: app, id: id, name: name, apiURLString: apiURL, frontendURL: frontendURL, proxiesVideos: proxiesVideos)
|
||||
}
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ final class InstancesModel: ObservableObject {
|
||||
|
||||
func add(app: VideosApp, name: String, url: String) -> Instance {
|
||||
let instance = Instance(
|
||||
app: app, id: UUID().uuidString, name: name, apiURL: standardizedURL(url)
|
||||
app: app, id: UUID().uuidString, name: name, apiURLString: standardizedURL(url)
|
||||
)
|
||||
Defaults[.instances].append(instance)
|
||||
|
||||
|
@@ -9,9 +9,12 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
static let basePath = "/api/v1"
|
||||
|
||||
@Published var account: Account!
|
||||
|
||||
@Published var validInstance = true
|
||||
|
||||
static func withAnonymousAccountForInstanceURL(_ url: URL) -> InvidiousAPI {
|
||||
.init(account: Instance(app: .invidious, apiURLString: url.absoluteString).anonymousAccount)
|
||||
}
|
||||
|
||||
var signedIn: Bool {
|
||||
guard let account else { return false }
|
||||
|
||||
@@ -452,7 +455,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
}
|
||||
|
||||
static func proxiedAsset(instance: Instance, asset: AVURLAsset) -> AVURLAsset? {
|
||||
guard let instanceURLComponents = URLComponents(string: instance.apiURL),
|
||||
guard let instanceURLComponents = URLComponents(url: instance.apiURL, resolvingAgainstBaseURL: false),
|
||||
var urlComponents = URLComponents(url: asset.url, resolvingAgainstBaseURL: false) else { return nil }
|
||||
|
||||
urlComponents.scheme = instanceURLComponents.scheme
|
||||
@@ -487,7 +490,9 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
let description = json["description"].stringValue
|
||||
|
||||
return Video(
|
||||
id: id,
|
||||
instanceID: account.instanceID,
|
||||
app: .invidious,
|
||||
instanceURL: account.instance.apiURL,
|
||||
videoID: videoID,
|
||||
title: json["title"].stringValue,
|
||||
author: json["author"].stringValue,
|
||||
@@ -518,7 +523,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
// append protocol to unproxied thumbnail URL if it's missing
|
||||
if thumbnailURL.count > 2,
|
||||
String(thumbnailURL[..<thumbnailURL.index(thumbnailURL.startIndex, offsetBy: 2)]) == "//",
|
||||
let accountUrlComponents = URLComponents(string: account.url)
|
||||
let accountUrlComponents = URLComponents(string: account.urlString)
|
||||
{
|
||||
thumbnailURL = "\(accountUrlComponents.scheme ?? "https"):\(thumbnailURL)"
|
||||
}
|
||||
@@ -553,7 +558,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
guard let url = json["url"].url,
|
||||
var components = URLComponents(url: url, resolvingAgainstBaseURL: false),
|
||||
let quality = json["quality"].string,
|
||||
let accountUrlComponents = URLComponents(string: account.url)
|
||||
let accountUrlComponents = URLComponents(string: account.urlString)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
@@ -677,8 +682,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
|
||||
|
||||
private func extractCaptions(from content: JSON) -> [Captions] {
|
||||
content["captions"].arrayValue.compactMap { details in
|
||||
let baseURL = account.url
|
||||
guard let url = URL(string: baseURL + details["url"].stringValue) else { return nil }
|
||||
guard let url = URL(string: details["url"].stringValue, relativeTo: account.url) else { return nil }
|
||||
|
||||
return Captions(
|
||||
label: details["label"].stringValue,
|
||||
|
592
Model/Applications/PeerTubeAPI.swift
Normal file
592
Model/Applications/PeerTubeAPI.swift
Normal file
@@ -0,0 +1,592 @@
|
||||
import Alamofire
|
||||
import AVKit
|
||||
import Defaults
|
||||
import Foundation
|
||||
import Siesta
|
||||
import SwiftyJSON
|
||||
|
||||
final class PeerTubeAPI: Service, ObservableObject, VideosAPI {
|
||||
static let basePath = "/api/v1"
|
||||
|
||||
@Published var account: Account!
|
||||
|
||||
@Published var validInstance = true
|
||||
|
||||
var signedIn: Bool {
|
||||
guard let account else { return false }
|
||||
|
||||
return !account.anonymous && !(account.token?.isEmpty ?? true)
|
||||
}
|
||||
|
||||
static func withAnonymousAccountForInstanceURL(_ url: URL) -> PeerTubeAPI {
|
||||
.init(account: Instance(app: .peerTube, apiURLString: url.absoluteString).anonymousAccount)
|
||||
}
|
||||
|
||||
init(account: Account? = nil) {
|
||||
super.init()
|
||||
|
||||
guard !account.isNil else {
|
||||
self.account = .init(name: "Empty")
|
||||
return
|
||||
}
|
||||
|
||||
setAccount(account!)
|
||||
}
|
||||
|
||||
func setAccount(_ account: Account) {
|
||||
self.account = account
|
||||
|
||||
validInstance = account.anonymous
|
||||
|
||||
configure()
|
||||
|
||||
if !account.anonymous {
|
||||
validate()
|
||||
}
|
||||
}
|
||||
|
||||
func validate() {
|
||||
validateInstance()
|
||||
validateSID()
|
||||
}
|
||||
|
||||
func validateInstance() {
|
||||
guard !validInstance else {
|
||||
return
|
||||
}
|
||||
|
||||
home?
|
||||
.load()
|
||||
.onSuccess { _ in
|
||||
self.validInstance = true
|
||||
}
|
||||
.onFailure { _ in
|
||||
self.validInstance = false
|
||||
}
|
||||
}
|
||||
|
||||
func validateSID() {
|
||||
guard signedIn, !(account.token?.isEmpty ?? true) else {
|
||||
return
|
||||
}
|
||||
|
||||
feed?
|
||||
.load()
|
||||
.onFailure { _ in
|
||||
self.updateToken(force: true)
|
||||
}
|
||||
}
|
||||
|
||||
func configure() {
|
||||
invalidateConfiguration()
|
||||
|
||||
configure {
|
||||
if let cookie = self.cookieHeader {
|
||||
$0.headers["Cookie"] = cookie
|
||||
}
|
||||
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
|
||||
}
|
||||
|
||||
configure("**", requestMethods: [.post]) {
|
||||
$0.pipeline[.parsing].removeTransformers()
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("popular"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
|
||||
content.json.arrayValue.map(self.extractVideo)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("videos"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
|
||||
content.json.dictionaryValue["data"]?.arrayValue.map(self.extractVideo) ?? []
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("search/videos"), requestMethods: [.get]) { (content: Entity<JSON>) -> SearchPage in
|
||||
let results = content.json.dictionaryValue["data"]?.arrayValue.compactMap { json -> ContentItem in .init(video: self.extractVideo(from: json)) } ?? []
|
||||
return SearchPage(results: results, last: results.isEmpty)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("search/suggestions"), requestMethods: [.get]) { (content: Entity<JSON>) -> [String] in
|
||||
if let suggestions = content.json.dictionaryValue["suggestions"] {
|
||||
return suggestions.arrayValue.map(String.init)
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("auth/playlists"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Playlist] in
|
||||
content.json.arrayValue.map(self.extractPlaylist)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("auth/playlists/*"), requestMethods: [.get]) { (content: Entity<JSON>) -> Playlist in
|
||||
self.extractPlaylist(from: content.json)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("auth/playlists"), requestMethods: [.post, .patch]) { (content: Entity<Data>) -> Playlist in
|
||||
// hacky, to verify if possible to get it in easier way
|
||||
Playlist(JSON(parseJSON: String(data: content.content, encoding: .utf8)!))
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("auth/feed"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
|
||||
if let feedVideos = content.json.dictionaryValue["videos"] {
|
||||
return feedVideos.arrayValue.map(self.extractVideo)
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("auth/subscriptions"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Channel] in
|
||||
content.json.arrayValue.map(self.extractChannel)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("channels/*"), requestMethods: [.get]) { (content: Entity<JSON>) -> Channel in
|
||||
self.extractChannel(from: content.json)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("channels/*/latest"), requestMethods: [.get]) { (content: Entity<JSON>) -> [Video] in
|
||||
content.json.arrayValue.map(self.extractVideo)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("channels/*/playlists"), requestMethods: [.get]) { (content: Entity<JSON>) -> [ContentItem] in
|
||||
let playlists = (content.json.dictionaryValue["playlists"]?.arrayValue ?? []).compactMap { self.extractChannelPlaylist(from: $0) }
|
||||
return ContentItem.array(of: playlists)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("playlists/*"), requestMethods: [.get]) { (content: Entity<JSON>) -> ChannelPlaylist in
|
||||
self.extractChannelPlaylist(from: content.json)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("videos/*"), requestMethods: [.get]) { (content: Entity<JSON>) -> Video in
|
||||
self.extractVideo(from: content.json)
|
||||
}
|
||||
|
||||
configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in
|
||||
let details = content.json.dictionaryValue
|
||||
let comments = details["comments"]?.arrayValue.compactMap { self.extractComment(from: $0) } ?? []
|
||||
let nextPage = details["continuation"]?.string
|
||||
let disabled = !details["error"].isNil
|
||||
|
||||
return CommentsPage(comments: comments, nextPage: nextPage, disabled: disabled)
|
||||
}
|
||||
|
||||
updateToken()
|
||||
}
|
||||
|
||||
func updateToken(force: Bool = false) {
|
||||
let (username, password) = AccountsModel.getCredentials(account)
|
||||
guard !account.anonymous,
|
||||
(account.token?.isEmpty ?? true) || force
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
guard let username,
|
||||
let password,
|
||||
!username.isEmpty,
|
||||
!password.isEmpty
|
||||
else {
|
||||
NavigationModel.shared.presentAlert(
|
||||
title: "Account Error",
|
||||
message: "Remove and add your account again in Settings."
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
let presentTokenUpdateFailedAlert: (AFDataResponse<Data?>?, String?) -> Void = { response, message in
|
||||
NavigationModel.shared.presentAlert(
|
||||
title: "Account Error",
|
||||
message: message ?? "\(response?.response?.statusCode ?? -1) - \(response?.error?.errorDescription ?? "unknown")\nIf this issue persists, try removing and adding your account again in Settings."
|
||||
)
|
||||
}
|
||||
|
||||
AF
|
||||
.request(login.url, method: .post, parameters: ["email": username, "password": password], encoding: URLEncoding.default)
|
||||
.redirect(using: .doNotFollow)
|
||||
.response { response in
|
||||
guard let headers = response.response?.headers,
|
||||
let cookies = headers["Set-Cookie"]
|
||||
else {
|
||||
presentTokenUpdateFailedAlert(response, nil)
|
||||
return
|
||||
}
|
||||
|
||||
let sidRegex = #"SID=(?<sid>[^;]*);"#
|
||||
guard let sidRegex = try? NSRegularExpression(pattern: sidRegex),
|
||||
let match = sidRegex.matches(in: cookies, range: NSRange(cookies.startIndex..., in: cookies)).first
|
||||
else {
|
||||
presentTokenUpdateFailedAlert(nil, String(format: "Could not extract SID from received cookies: %@".localized(), cookies))
|
||||
return
|
||||
}
|
||||
|
||||
let matchRange = match.range(withName: "sid")
|
||||
|
||||
if let substringRange = Range(matchRange, in: cookies) {
|
||||
let sid = String(cookies[substringRange])
|
||||
AccountsModel.setToken(self.account, sid)
|
||||
self.objectWillChange.send()
|
||||
} else {
|
||||
presentTokenUpdateFailedAlert(nil, String(format: "Could not extract SID from received cookies: %@".localized(), cookies))
|
||||
}
|
||||
|
||||
self.configure()
|
||||
}
|
||||
}
|
||||
|
||||
var login: Resource {
|
||||
resource(baseURL: account.url, path: "login")
|
||||
}
|
||||
|
||||
private func pathPattern(_ path: String) -> String {
|
||||
"**\(Self.basePath)/\(path)"
|
||||
}
|
||||
|
||||
private func basePathAppending(_ path: String) -> String {
|
||||
"\(Self.basePath)/\(path)"
|
||||
}
|
||||
|
||||
private var cookieHeader: String? {
|
||||
guard let token = account?.token, !token.isEmpty else { return nil }
|
||||
return "SID=\(token)"
|
||||
}
|
||||
|
||||
var popular: Resource? {
|
||||
resource(baseURL: account.url, path: "\(Self.basePath)/popular")
|
||||
}
|
||||
|
||||
func trending(country: Country, category: TrendingCategory?) -> Resource {
|
||||
resource(baseURL: account.url, path: "\(Self.basePath)/videos")
|
||||
.withParam("isLocal", "true")
|
||||
// .withParam("type", category?.name)
|
||||
// .withParam("region", country.rawValue)
|
||||
}
|
||||
|
||||
var home: Resource? {
|
||||
resource(baseURL: account.url, path: "/feed/subscriptions")
|
||||
}
|
||||
|
||||
var feed: Resource? {
|
||||
resource(baseURL: account.url, path: "\(Self.basePath)/auth/feed")
|
||||
}
|
||||
|
||||
var subscriptions: Resource? {
|
||||
resource(baseURL: account.url, path: basePathAppending("auth/subscriptions"))
|
||||
}
|
||||
|
||||
func subscribe(_ channelID: String, onCompletion: @escaping () -> Void = {}) {
|
||||
resource(baseURL: account.url, path: basePathAppending("auth/subscriptions"))
|
||||
.child(channelID)
|
||||
.request(.post)
|
||||
.onCompletion { _ in onCompletion() }
|
||||
}
|
||||
|
||||
func unsubscribe(_ channelID: String, onCompletion: @escaping () -> Void) {
|
||||
resource(baseURL: account.url, path: basePathAppending("auth/subscriptions"))
|
||||
.child(channelID)
|
||||
.request(.delete)
|
||||
.onCompletion { _ in onCompletion() }
|
||||
}
|
||||
|
||||
func channel(_ id: String, contentType: Channel.ContentType, data _: String?) -> Resource {
|
||||
if contentType == .playlists {
|
||||
return resource(baseURL: account.url, path: basePathAppending("channels/\(id)/playlists"))
|
||||
}
|
||||
return resource(baseURL: account.url, path: basePathAppending("channels/\(id)"))
|
||||
}
|
||||
|
||||
func channelByName(_: String) -> Resource? {
|
||||
nil
|
||||
}
|
||||
|
||||
func channelByUsername(_: String) -> Resource? {
|
||||
nil
|
||||
}
|
||||
|
||||
func channelVideos(_ id: String) -> Resource {
|
||||
resource(baseURL: account.url, path: basePathAppending("channels/\(id)/latest"))
|
||||
}
|
||||
|
||||
func video(_ id: String) -> Resource {
|
||||
resource(baseURL: account.url, path: basePathAppending("videos/\(id)"))
|
||||
}
|
||||
|
||||
var playlists: Resource? {
|
||||
if account.isNil || account.anonymous {
|
||||
return nil
|
||||
}
|
||||
|
||||
return resource(baseURL: account.url, path: basePathAppending("auth/playlists"))
|
||||
}
|
||||
|
||||
func playlist(_ id: String) -> Resource? {
|
||||
resource(baseURL: account.url, path: basePathAppending("auth/playlists/\(id)"))
|
||||
}
|
||||
|
||||
func playlistVideos(_ id: String) -> Resource? {
|
||||
playlist(id)?.child("videos")
|
||||
}
|
||||
|
||||
func playlistVideo(_ playlistID: String, _ videoID: String) -> Resource? {
|
||||
playlist(playlistID)?.child("videos").child(videoID)
|
||||
}
|
||||
|
||||
func addVideoToPlaylist(
|
||||
_ videoID: String,
|
||||
_ playlistID: String,
|
||||
onFailure: @escaping (RequestError) -> Void = { _ in },
|
||||
onSuccess: @escaping () -> Void = {}
|
||||
) {
|
||||
let resource = playlistVideos(playlistID)
|
||||
let body = ["videoId": videoID]
|
||||
|
||||
resource?
|
||||
.request(.post, json: body)
|
||||
.onSuccess { _ in onSuccess() }
|
||||
.onFailure(onFailure)
|
||||
}
|
||||
|
||||
func removeVideoFromPlaylist(
|
||||
_ index: String,
|
||||
_ playlistID: String,
|
||||
onFailure: @escaping (RequestError) -> Void,
|
||||
onSuccess: @escaping () -> Void
|
||||
) {
|
||||
let resource = playlistVideo(playlistID, index)
|
||||
|
||||
resource?
|
||||
.request(.delete)
|
||||
.onSuccess { _ in onSuccess() }
|
||||
.onFailure(onFailure)
|
||||
}
|
||||
|
||||
func playlistForm(
|
||||
_ name: String,
|
||||
_ visibility: String,
|
||||
playlist: Playlist?,
|
||||
onFailure: @escaping (RequestError) -> Void,
|
||||
onSuccess: @escaping (Playlist?) -> Void
|
||||
) {
|
||||
let body = ["title": name, "privacy": visibility]
|
||||
let resource = !playlist.isNil ? self.playlist(playlist!.id) : playlists
|
||||
|
||||
resource?
|
||||
.request(!playlist.isNil ? .patch : .post, json: body)
|
||||
.onSuccess { response in
|
||||
if let modifiedPlaylist: Playlist = response.typedContent() {
|
||||
onSuccess(modifiedPlaylist)
|
||||
}
|
||||
}
|
||||
.onFailure(onFailure)
|
||||
}
|
||||
|
||||
func deletePlaylist(
|
||||
_ playlist: Playlist,
|
||||
onFailure: @escaping (RequestError) -> Void,
|
||||
onSuccess: @escaping () -> Void
|
||||
) {
|
||||
self.playlist(playlist.id)?
|
||||
.request(.delete)
|
||||
.onSuccess { _ in onSuccess() }
|
||||
.onFailure(onFailure)
|
||||
}
|
||||
|
||||
func channelPlaylist(_ id: String) -> Resource? {
|
||||
resource(baseURL: account.url, path: basePathAppending("playlists/\(id)"))
|
||||
}
|
||||
|
||||
func search(_ query: SearchQuery, page: String?) -> Resource {
|
||||
var resource = resource(baseURL: account.url, path: basePathAppending("search/videos"))
|
||||
.withParam("search", query.query)
|
||||
// .withParam("sort_by", query.sortBy.parameter)
|
||||
// .withParam("type", "all")
|
||||
//
|
||||
// if let date = query.date, date != .any {
|
||||
// resource = resource.withParam("date", date.rawValue)
|
||||
// }
|
||||
//
|
||||
// if let duration = query.duration, duration != .any {
|
||||
// resource = resource.withParam("duration", duration.rawValue)
|
||||
// }
|
||||
//
|
||||
// if let page {
|
||||
// resource = resource.withParam("page", page)
|
||||
// }
|
||||
|
||||
return resource
|
||||
}
|
||||
|
||||
func searchSuggestions(query: String) -> Resource {
|
||||
resource(baseURL: account.url, path: basePathAppending("search/suggestions"))
|
||||
.withParam("q", query.lowercased())
|
||||
}
|
||||
|
||||
func comments(_ id: Video.ID, page: String?) -> Resource? {
|
||||
let resource = resource(baseURL: account.url, path: basePathAppending("comments/\(id)"))
|
||||
guard let page else { return resource }
|
||||
|
||||
return resource.withParam("continuation", page)
|
||||
}
|
||||
|
||||
static func proxiedAsset(instance: Instance, asset: AVURLAsset) -> AVURLAsset? {
|
||||
guard let instanceURLComponents = URLComponents(string: instance.apiURLString),
|
||||
var urlComponents = URLComponents(url: asset.url, resolvingAgainstBaseURL: false) else { return nil }
|
||||
|
||||
urlComponents.scheme = instanceURLComponents.scheme
|
||||
urlComponents.host = instanceURLComponents.host
|
||||
|
||||
guard let url = urlComponents.url else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return AVURLAsset(url: url)
|
||||
}
|
||||
|
||||
func extractVideo(from json: JSON) -> Video {
|
||||
let id = json["uuid"].stringValue
|
||||
let url = json["url"].url
|
||||
let dateFormatter = ISO8601DateFormatter()
|
||||
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
||||
let publishedAt = dateFormatter.date(from: json["publishedAt"].stringValue)
|
||||
|
||||
return Video(
|
||||
instanceID: account.instanceID,
|
||||
app: .peerTube,
|
||||
instanceURL: account.instance.apiURL,
|
||||
id: id,
|
||||
videoID: id,
|
||||
videoURL: url,
|
||||
title: json["name"].stringValue,
|
||||
author: json["channel"].dictionaryValue["name"]?.stringValue ?? "",
|
||||
length: json["duration"].doubleValue,
|
||||
views: json["views"].intValue,
|
||||
description: json["description"].stringValue,
|
||||
channel: extractChannel(from: json["channel"]),
|
||||
thumbnails: extractThumbnails(from: json),
|
||||
live: json["isLive"].boolValue,
|
||||
publishedAt: publishedAt,
|
||||
likes: json["likes"].int,
|
||||
dislikes: json["dislikes"].int,
|
||||
streams: extractStreams(from: json)
|
||||
// related: extractRelated(from: json),
|
||||
// chapters: extractChapters(from: description),
|
||||
// captions: extractCaptions(from: json)
|
||||
)
|
||||
}
|
||||
|
||||
func extractChannel(from json: JSON) -> Channel {
|
||||
Channel(
|
||||
id: json["id"].stringValue,
|
||||
name: json["name"].stringValue
|
||||
)
|
||||
}
|
||||
|
||||
func extractChannelPlaylist(from json: JSON) -> ChannelPlaylist {
|
||||
let details = json.dictionaryValue
|
||||
return ChannelPlaylist(
|
||||
id: details["playlistId"]?.string ?? details["mixId"]?.string ?? UUID().uuidString,
|
||||
title: details["title"]?.stringValue ?? "",
|
||||
thumbnailURL: details["playlistThumbnail"]?.url,
|
||||
channel: extractChannel(from: json),
|
||||
videos: details["videos"]?.arrayValue.compactMap(extractVideo) ?? [],
|
||||
videosCount: details["videoCount"]?.int
|
||||
)
|
||||
}
|
||||
|
||||
private func extractThumbnails(from details: JSON) -> [Thumbnail] {
|
||||
if let thumbnailPath = details["thumbnailPath"].string {
|
||||
return [Thumbnail(url: URL(string: thumbnailPath, relativeTo: account.url)!, quality: .medium)]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
private func extractStreams(from json: JSON) -> [Stream] {
|
||||
let hls = extractHLSStreams(from: json)
|
||||
|
||||
if json["isLive"].boolValue {
|
||||
return hls
|
||||
}
|
||||
|
||||
return extractFormatStreams(from: json) +
|
||||
extractAdaptiveFormats(from: json) +
|
||||
hls
|
||||
}
|
||||
|
||||
private func extractFormatStreams(from json: JSON) -> [Stream] {
|
||||
var streams = [Stream]()
|
||||
if let fileURL = json.dictionaryValue["streamingPlaylists"]?.arrayValue.first?
|
||||
.dictionaryValue["files"]?.arrayValue.first?
|
||||
.dictionaryValue["fileUrl"]?.url
|
||||
{
|
||||
streams.append(SingleAssetStream(instance: account.instance, avAsset: AVURLAsset(url: fileURL), resolution: .hd720p30, kind: .stream))
|
||||
}
|
||||
|
||||
return streams
|
||||
}
|
||||
|
||||
private func extractAdaptiveFormats(from json: JSON) -> [Stream] {
|
||||
json.dictionaryValue["files"]?.arrayValue.compactMap { file in
|
||||
if let resolution = file.dictionaryValue["resolution"]?.dictionaryValue["label"]?.stringValue, let url = file.dictionaryValue["fileUrl"]?.url {
|
||||
return SingleAssetStream(instance: account.instance, avAsset: AVURLAsset(url: url), resolution: Stream.Resolution.from(resolution: resolution), kind: .adaptive, videoFormat: "mp4")
|
||||
}
|
||||
|
||||
return nil
|
||||
} ?? []
|
||||
}
|
||||
|
||||
private func extractHLSStreams(from content: JSON) -> [Stream] {
|
||||
if let hlsURL = content.dictionaryValue["streamingPlaylists"]?.arrayValue.first?.dictionaryValue["playlistUrl"]?.url {
|
||||
return [Stream(instance: account.instance, hlsURL: hlsURL)]
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
private func extractRelated(from content: JSON) -> [Video] {
|
||||
content
|
||||
.dictionaryValue["recommendedVideos"]?
|
||||
.arrayValue
|
||||
.compactMap(extractVideo(from:)) ?? []
|
||||
}
|
||||
|
||||
private func extractPlaylist(from content: JSON) -> Playlist {
|
||||
let id = content["playlistId"].stringValue
|
||||
return Playlist(
|
||||
id: id,
|
||||
title: content["title"].stringValue,
|
||||
visibility: content["isListed"].boolValue ? .public : .private,
|
||||
editable: id.starts(with: "IV"),
|
||||
updated: content["updated"].doubleValue,
|
||||
videos: content["videos"].arrayValue.map { extractVideo(from: $0) }
|
||||
)
|
||||
}
|
||||
|
||||
private func extractComment(from content: JSON) -> Comment? {
|
||||
let details = content.dictionaryValue
|
||||
let author = details["author"]?.string ?? ""
|
||||
let channelId = details["authorId"]?.string ?? UUID().uuidString
|
||||
let authorAvatarURL = details["authorThumbnails"]?.arrayValue.last?.dictionaryValue["url"]?.string ?? ""
|
||||
return Comment(
|
||||
id: UUID().uuidString,
|
||||
author: author,
|
||||
authorAvatarURL: authorAvatarURL,
|
||||
time: details["publishedText"]?.string ?? "",
|
||||
pinned: false,
|
||||
hearted: false,
|
||||
likeCount: details["likeCount"]?.int ?? 0,
|
||||
text: details["content"]?.string ?? "",
|
||||
repliesPage: details["replies"]?.dictionaryValue["continuation"]?.string,
|
||||
channel: Channel(id: channelId, name: author)
|
||||
)
|
||||
}
|
||||
|
||||
private func extractCaptions(from content: JSON) -> [Captions] {
|
||||
content["captions"].arrayValue.compactMap { _ in
|
||||
nil
|
||||
// let baseURL = account.url
|
||||
// guard let url = URL(string: baseURL + details["url"].stringValue) else { return nil }
|
||||
//
|
||||
// return Captions(
|
||||
// label: details["label"].stringValue,
|
||||
// code: details["language_code"].stringValue,
|
||||
// url: url
|
||||
// )
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,10 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
|
||||
|
||||
@Published var account: Account!
|
||||
|
||||
static func withAnonymousAccountForInstanceURL(_ url: URL) -> PipedAPI {
|
||||
.init(account: Instance(app: .piped, apiURLString: url.absoluteString).anonymousAccount)
|
||||
}
|
||||
|
||||
init(account: Account? = nil) {
|
||||
super.init()
|
||||
|
||||
@@ -473,6 +477,9 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
|
||||
}
|
||||
|
||||
return Video(
|
||||
instanceID: account.instanceID,
|
||||
app: .piped,
|
||||
instanceURL: account.instance.apiURL,
|
||||
videoID: extractID(from: content),
|
||||
title: details["title"]?.string ?? "",
|
||||
author: author,
|
||||
|
@@ -6,6 +6,8 @@ protocol VideosAPI {
|
||||
var account: Account! { get }
|
||||
var signedIn: Bool { get }
|
||||
|
||||
static func withAnonymousAccountForInstanceURL(_ url: URL) -> Self
|
||||
|
||||
func channel(_ id: String, contentType: Channel.ContentType, data: String?) -> Resource
|
||||
func channelByName(_ name: String) -> Resource?
|
||||
func channelByUsername(_ username: String) -> Resource?
|
||||
|
@@ -1,14 +1,17 @@
|
||||
import Foundation
|
||||
|
||||
enum VideosApp: String, CaseIterable {
|
||||
case invidious, piped
|
||||
case local
|
||||
case invidious
|
||||
case piped
|
||||
case peerTube
|
||||
|
||||
var name: String {
|
||||
rawValue.capitalized
|
||||
}
|
||||
|
||||
var supportsAccounts: Bool {
|
||||
true
|
||||
self != .local
|
||||
}
|
||||
|
||||
var supportsPopular: Bool {
|
||||
@@ -19,6 +22,10 @@ enum VideosApp: String, CaseIterable {
|
||||
self == .invidious
|
||||
}
|
||||
|
||||
var supportsSearchSuggestions: Bool {
|
||||
self != .peerTube
|
||||
}
|
||||
|
||||
var supportsSubscriptions: Bool {
|
||||
supportsAccounts
|
||||
}
|
||||
@@ -28,7 +35,7 @@ enum VideosApp: String, CaseIterable {
|
||||
}
|
||||
|
||||
var supportsUserPlaylists: Bool {
|
||||
true
|
||||
self != .local
|
||||
}
|
||||
|
||||
var userPlaylistsEndpointIncludesVideos: Bool {
|
||||
@@ -64,6 +71,6 @@ enum VideosApp: String, CaseIterable {
|
||||
}
|
||||
|
||||
var supportsOpeningVideosByID: Bool {
|
||||
true
|
||||
self != .local
|
||||
}
|
||||
}
|
||||
|
@@ -42,7 +42,7 @@ final class CommentsModel: ObservableObject {
|
||||
|
||||
firstPage = page.isNil || page!.isEmpty
|
||||
|
||||
player.playerAPI.comments(video.videoID, page: page)?
|
||||
player.playerAPI(video).comments(video.videoID, page: page)?
|
||||
.load()
|
||||
.onSuccess { [weak self] response in
|
||||
if let page: CommentsPage = response.typedContent() {
|
||||
|
@@ -2,6 +2,7 @@ import CoreData
|
||||
import CoreMedia
|
||||
import Defaults
|
||||
import Foundation
|
||||
import Siesta
|
||||
import SwiftyJSON
|
||||
|
||||
extension PlayerModel {
|
||||
@@ -9,18 +10,18 @@ extension PlayerModel {
|
||||
historyVideos.first { $0.videoID == id }
|
||||
}
|
||||
|
||||
func loadHistoryVideoDetails(_ id: Video.ID) {
|
||||
guard historyVideo(id).isNil else {
|
||||
func loadHistoryVideoDetails(_ watch: Watch) {
|
||||
logger.info("id: \(watch.videoID), instance \(watch.instanceURL), app \(watch.appName)")
|
||||
guard historyVideo(watch.videoID).isNil else {
|
||||
return
|
||||
}
|
||||
|
||||
if !Video.VideoID.isValid(id), let url = URL(string: id) {
|
||||
if !Video.VideoID.isValid(watch.videoID), let url = URL(string: watch.videoID) {
|
||||
historyVideos.append(.local(url))
|
||||
return
|
||||
}
|
||||
|
||||
playerAPI.video(id)
|
||||
.load()
|
||||
playerAPI(watch.video).video(watch.videoID).load()
|
||||
.onSuccess { [weak self] response in
|
||||
guard let self else { return }
|
||||
|
||||
@@ -29,26 +30,23 @@ extension PlayerModel {
|
||||
}
|
||||
}
|
||||
.onCompletion { _ in
|
||||
self.logger.info("LOADED history details: \(id)")
|
||||
self.logger.info("LOADED history details: \(watch.videoID)")
|
||||
|
||||
if self.historyItemBeingLoaded == id {
|
||||
if self.historyItemBeingLoaded == watch.videoID {
|
||||
self.logger.info("setting no history loaded")
|
||||
self.historyItemBeingLoaded = nil
|
||||
}
|
||||
|
||||
if let id = self.historyItemsToLoad.popLast() {
|
||||
self.loadHistoryVideoDetails(id)
|
||||
if let watch = self.historyItemsToLoad.popLast() {
|
||||
self.loadHistoryVideoDetails(watch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func updateWatch(finished: Bool = false) {
|
||||
guard let id = currentVideo?.videoID,
|
||||
Defaults[.saveHistory]
|
||||
else {
|
||||
return
|
||||
}
|
||||
guard let currentVideo, saveHistory else { return }
|
||||
|
||||
let id = currentVideo.videoID
|
||||
let time = backend.currentTime
|
||||
let seconds = time?.seconds ?? 0
|
||||
|
||||
@@ -70,6 +68,8 @@ extension PlayerModel {
|
||||
}
|
||||
watch = Watch(context: self.backgroundContext)
|
||||
watch.videoID = id
|
||||
watch.appName = currentVideo.app.rawValue
|
||||
watch.instanceURL = currentVideo.instanceURL
|
||||
} else {
|
||||
watch = results?.first
|
||||
}
|
||||
|
@@ -63,8 +63,8 @@ final class InstancesManifest: Service, ObservableObject {
|
||||
var regionInstances = instances.filter { $0.region == region }
|
||||
|
||||
if let publicAccountUrl = AccountsModel.shared.publicAccount?.url {
|
||||
countryInstances = countryInstances.filter { $0.url.absoluteString != publicAccountUrl }
|
||||
regionInstances = regionInstances.filter { $0.url.absoluteString != publicAccountUrl }
|
||||
countryInstances = countryInstances.filter { $0.url != publicAccountUrl }
|
||||
regionInstances = regionInstances.filter { $0.url != publicAccountUrl }
|
||||
}
|
||||
|
||||
var instance: ManifestedInstance?
|
||||
|
@@ -9,7 +9,7 @@ struct ManifestedInstance: Identifiable, Hashable {
|
||||
let url: URL
|
||||
|
||||
var instance: Instance {
|
||||
.init(app: app, name: "Public - \(country)", apiURL: url.absoluteString)
|
||||
.init(app: app, name: "Public - \(country)", apiURLString: url.absoluteString)
|
||||
}
|
||||
|
||||
var location: String {
|
||||
@@ -21,7 +21,7 @@ struct ManifestedInstance: Identifiable, Hashable {
|
||||
id: UUID().uuidString,
|
||||
app: app,
|
||||
name: location,
|
||||
url: url.absoluteString,
|
||||
urlString: url.absoluteString,
|
||||
anonymous: true,
|
||||
country: country,
|
||||
region: region
|
||||
|
@@ -128,7 +128,7 @@ struct OpenVideosModel {
|
||||
let parser = URLParser(url: url)
|
||||
|
||||
if parser.destination == .video, let id = parser.videoID {
|
||||
video = Video(videoID: id)
|
||||
video = Video(app: .local, videoID: id)
|
||||
logger.info("identified remote video: \(id)")
|
||||
} else {
|
||||
video = .local(url)
|
||||
|
@@ -65,6 +65,7 @@ final class MPVClient: ObservableObject {
|
||||
checkError(mpv_set_option_string(mpv, "keep-open", "yes"))
|
||||
checkError(mpv_set_option_string(mpv, "hwdec", machine == "x86_64" ? "no" : "auto-safe"))
|
||||
checkError(mpv_set_option_string(mpv, "vo", "libmpv"))
|
||||
checkError(mpv_set_option_string(mpv, "demuxer-lavf-analyzeduration", "1"))
|
||||
|
||||
checkError(mpv_initialize(mpv))
|
||||
|
||||
@@ -134,8 +135,9 @@ final class MPVClient: ObservableObject {
|
||||
var args = [url.absoluteString]
|
||||
var options = [String]()
|
||||
|
||||
if let time {
|
||||
args.append("replace")
|
||||
args.append("replace")
|
||||
|
||||
if let time, time.seconds > 0 {
|
||||
options.append("start=\(Int(time.seconds))")
|
||||
}
|
||||
|
||||
@@ -148,9 +150,11 @@ final class MPVClient: ObservableObject {
|
||||
}
|
||||
|
||||
if forceSeekable {
|
||||
options.append("force-seekable=yes")
|
||||
// options.append("stream-lavf-o=seekable=0")
|
||||
}
|
||||
|
||||
options.append("stream-lavf-o=seekable=0")
|
||||
|
||||
if !options.isEmpty {
|
||||
args.append(options.joined(separator: ","))
|
||||
}
|
||||
|
@@ -99,7 +99,7 @@ final class PlayerModel: ObservableObject {
|
||||
@Published var queueItemBeingLoaded: PlayerQueueItem?
|
||||
@Published var queueItemsToLoad = [PlayerQueueItem]()
|
||||
@Published var historyItemBeingLoaded: Video.ID?
|
||||
@Published var historyItemsToLoad = [Video.ID]()
|
||||
@Published var historyItemsToLoad = [Watch]()
|
||||
|
||||
@Published var preservedTime: CMTime?
|
||||
|
||||
@@ -125,6 +125,7 @@ final class PlayerModel: ObservableObject {
|
||||
@Default(.rotateToPortraitOnExitFullScreen) private var rotateToPortraitOnExitFullScreen
|
||||
#endif
|
||||
|
||||
var accounts: AccountsModel { .shared }
|
||||
var comments: CommentsModel { .shared }
|
||||
var controls: PlayerControlsModel { .shared }
|
||||
var playerTime: PlayerTimeModel { .shared }
|
||||
@@ -152,6 +153,7 @@ final class PlayerModel: ObservableObject {
|
||||
}
|
||||
}}
|
||||
|
||||
@Default(.saveHistory) var saveHistory
|
||||
@Default(.saveLastPlayed) var saveLastPlayed
|
||||
@Default(.lastPlayed) var lastPlayed
|
||||
@Default(.qualityProfiles) var qualityProfiles
|
||||
@@ -709,7 +711,7 @@ final class PlayerModel: ObservableObject {
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
|
||||
guard let self else { return }
|
||||
self.playerAPI.loadDetails(item, completionHandler: { newItem in
|
||||
self.playerAPI(item.video).loadDetails(item, completionHandler: { newItem in
|
||||
guard newItem.videoID == self.autoplayItem?.videoID else { return }
|
||||
self.autoplayItem = newItem
|
||||
self.updateRemoteCommandCenter()
|
||||
|
@@ -83,11 +83,21 @@ extension PlayerModel {
|
||||
}
|
||||
|
||||
var playerInstance: Instance? {
|
||||
InstancesModel.shared.forPlayer ?? AccountsModel.shared.current?.instance ?? InstancesModel.shared.all.first
|
||||
InstancesModel.shared.forPlayer ?? accounts.current?.instance ?? InstancesModel.shared.all.first
|
||||
}
|
||||
|
||||
var playerAPI: VideosAPI {
|
||||
playerInstance?.anonymous ?? AccountsModel.shared.api
|
||||
func playerAPI(_ video: Video) -> VideosAPI! {
|
||||
guard let url = video.instanceURL else { return nil }
|
||||
switch video.app {
|
||||
case .local:
|
||||
return nil
|
||||
case .peerTube:
|
||||
return PeerTubeAPI.withAnonymousAccountForInstanceURL(url)
|
||||
case .invidious:
|
||||
return InvidiousAPI.withAnonymousAccountForInstanceURL(url)
|
||||
case .piped:
|
||||
return PipedAPI.withAnonymousAccountForInstanceURL(url)
|
||||
}
|
||||
}
|
||||
|
||||
var qualityProfile: QualityProfile? {
|
||||
@@ -155,7 +165,7 @@ extension PlayerModel {
|
||||
currentItem.playbackTime = time
|
||||
|
||||
let playTime = currentItem.shouldRestartPlaying ? CMTime.zero : time
|
||||
playerAPI.loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in
|
||||
playerAPI(newItem.video).loadDetails(currentItem, failureHandler: { self.videoLoadFailureHandler($0, video: self.currentItem.video) }) { newItem in
|
||||
self.playItem(newItem, at: playTime)
|
||||
}
|
||||
}
|
||||
@@ -198,7 +208,7 @@ extension PlayerModel {
|
||||
}
|
||||
|
||||
if loadDetails {
|
||||
playerAPI.loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in
|
||||
playerAPI(item.video).loadDetails(item, failureHandler: { self.videoLoadFailureHandler($0, video: video) }) { [weak self] newItem in
|
||||
guard let self else { return }
|
||||
videoDetailsLoadHandler(newItem.video, newItem)
|
||||
|
||||
@@ -269,7 +279,7 @@ extension PlayerModel {
|
||||
}
|
||||
|
||||
func loadQueueVideoDetails(_ item: PlayerQueueItem) {
|
||||
guard !AccountsModel.shared.current.isNil, !item.hasDetailsLoaded else { return }
|
||||
guard !accounts.current.isNil, !item.hasDetailsLoaded else { return }
|
||||
|
||||
let videoID = item.video?.videoID ?? item.videoID
|
||||
|
||||
@@ -282,7 +292,7 @@ extension PlayerModel {
|
||||
return
|
||||
}
|
||||
|
||||
playerAPI.loadDetails(item, completionHandler: { [weak self] newItem in
|
||||
playerAPI(item.video).loadDetails(item, completionHandler: { [weak self] newItem in
|
||||
guard let self else { return }
|
||||
|
||||
self.queue.filter { $0.videoID == item.videoID }.forEach { item in
|
||||
|
@@ -21,7 +21,7 @@ extension PlayerModel {
|
||||
guard let playerInstance else { return }
|
||||
|
||||
logger.info("loading streams from \(playerInstance.description)")
|
||||
fetchStreams(playerAPI.video(video.videoID), instance: playerInstance, video: video)
|
||||
fetchStreams(playerAPI(video).video(video.videoID), instance: playerInstance, video: video)
|
||||
}
|
||||
|
||||
private func fetchStreams(
|
||||
|
@@ -23,6 +23,10 @@ final class SearchModel: ObservableObject {
|
||||
resource?.isLoading ?? false
|
||||
}
|
||||
|
||||
func reloadQuery() {
|
||||
changeQuery()
|
||||
}
|
||||
|
||||
func changeQuery(_ changeHandler: @escaping (SearchQuery) -> Void = { _ in }) {
|
||||
changeHandler(query)
|
||||
|
||||
@@ -78,6 +82,10 @@ final class SearchModel: ObservableObject {
|
||||
}}
|
||||
|
||||
func loadSuggestions(_ query: String) {
|
||||
guard accounts.app.supportsSearchSuggestions else {
|
||||
querySuggestions.removeAll()
|
||||
return
|
||||
}
|
||||
suggestionsDebouncer.callback = {
|
||||
guard !query.isEmpty else { return }
|
||||
DispatchQueue.main.async {
|
||||
|
@@ -4,9 +4,9 @@ import Foundation
|
||||
final class SingleAssetStream: Stream {
|
||||
var avAsset: AVURLAsset
|
||||
|
||||
init(instance: Instance? = nil, avAsset: AVURLAsset, resolution: Resolution, kind: Kind, encoding: String = "") {
|
||||
init(instance: Instance? = nil, avAsset: AVURLAsset, resolution: Resolution, kind: Kind, encoding: String = "", videoFormat: String? = nil) {
|
||||
self.avAsset = avAsset
|
||||
|
||||
super.init(instance: instance, audioAsset: avAsset, videoAsset: avAsset, resolution: resolution, kind: kind, encoding: encoding)
|
||||
super.init(instance: instance, audioAsset: avAsset, videoAsset: avAsset, resolution: resolution, kind: kind, encoding: encoding, videoFormat: videoFormat)
|
||||
}
|
||||
}
|
||||
|
@@ -7,12 +7,25 @@ import SwiftyJSON
|
||||
struct Video: Identifiable, Equatable, Hashable {
|
||||
enum VideoID {
|
||||
static func isValid(_ id: Video.ID) -> Bool {
|
||||
isYouTube(id) || isPeerTube(id)
|
||||
}
|
||||
|
||||
static func isYouTube(_ id: Video.ID) -> Bool {
|
||||
id.count == 11
|
||||
}
|
||||
|
||||
static func isPeerTube(_ id: Video.ID) -> Bool {
|
||||
id.count == 36
|
||||
}
|
||||
}
|
||||
|
||||
let id: String
|
||||
let videoID: String
|
||||
var instanceID: Instance.ID?
|
||||
var app: VideosApp
|
||||
var instanceURL: URL?
|
||||
|
||||
var id: String
|
||||
var videoID: String
|
||||
var videoURL: URL?
|
||||
var title: String
|
||||
var thumbnails: [Thumbnail]
|
||||
var author: String
|
||||
@@ -43,8 +56,12 @@ struct Video: Identifiable, Equatable, Hashable {
|
||||
var captions = [Captions]()
|
||||
|
||||
init(
|
||||
instanceID: Instance.ID? = nil,
|
||||
app: VideosApp,
|
||||
instanceURL: URL? = nil,
|
||||
id: String? = nil,
|
||||
videoID: String,
|
||||
videoURL: URL? = nil,
|
||||
title: String = "",
|
||||
author: String = "",
|
||||
length: TimeInterval = .zero,
|
||||
@@ -66,8 +83,12 @@ struct Video: Identifiable, Equatable, Hashable {
|
||||
chapters: [Chapter] = [],
|
||||
captions: [Captions] = []
|
||||
) {
|
||||
self.instanceID = instanceID
|
||||
self.app = app
|
||||
self.instanceURL = instanceURL
|
||||
self.id = id ?? UUID().uuidString
|
||||
self.videoID = videoID
|
||||
self.videoURL = videoURL
|
||||
self.title = title
|
||||
self.author = author
|
||||
self.length = length
|
||||
@@ -92,11 +113,24 @@ struct Video: Identifiable, Equatable, Hashable {
|
||||
|
||||
static func local(_ url: URL) -> Video {
|
||||
Video(
|
||||
app: .local,
|
||||
videoID: url.absoluteString,
|
||||
streams: [.init(localURL: url)]
|
||||
)
|
||||
}
|
||||
|
||||
var instance: Instance! {
|
||||
if let instance = InstancesModel.shared.find(instanceID) {
|
||||
return instance
|
||||
}
|
||||
|
||||
if let url = instanceURL?.absoluteString {
|
||||
return Instance(app: app, id: instanceID, apiURLString: url, proxiesVideos: false)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var isLocal: Bool {
|
||||
!VideoID.isValid(videoID)
|
||||
}
|
||||
@@ -118,7 +152,7 @@ struct Video: Identifiable, Equatable, Hashable {
|
||||
}
|
||||
|
||||
var publishedDate: String? {
|
||||
(published.isEmpty || published == "0 seconds ago") ? nil : published
|
||||
(published.isEmpty || published == "0 seconds ago") ? publishedAt?.timeIntervalSince1970.formattedAsRelativeTime() : published
|
||||
}
|
||||
|
||||
var viewsCount: String? {
|
||||
|
@@ -14,7 +14,7 @@ extension Watch {
|
||||
NSFetchRequest<Watch>(entityName: "Watch")
|
||||
}
|
||||
|
||||
@nonobjc class func markAsWatched(videoID: String, duration: Double, context: NSManagedObjectContext) {
|
||||
@nonobjc class func markAsWatched(videoID: String, account: Account, duration: Double, context: NSManagedObjectContext) {
|
||||
let watchFetchRequest = Watch.fetchRequest()
|
||||
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", videoID as String)
|
||||
|
||||
@@ -26,6 +26,8 @@ extension Watch {
|
||||
if results?.isEmpty ?? true {
|
||||
watch = Watch(context: context)
|
||||
watch?.videoID = videoID
|
||||
watch?.appName = account.app?.rawValue
|
||||
watch?.instanceURL = account.url
|
||||
} else {
|
||||
watch = results?.first
|
||||
}
|
||||
@@ -46,6 +48,14 @@ extension Watch {
|
||||
@NSManaged var watchedAt: Date?
|
||||
@NSManaged var stoppedAt: Double
|
||||
|
||||
@NSManaged var appName: String?
|
||||
@NSManaged var instanceURL: URL?
|
||||
|
||||
var app: VideosApp! {
|
||||
guard let appName else { return nil }
|
||||
return .init(rawValue: appName)
|
||||
}
|
||||
|
||||
var progress: Double {
|
||||
guard videoDuration.isFinite, !videoDuration.isZero else {
|
||||
return 0
|
||||
@@ -83,6 +93,6 @@ extension Watch {
|
||||
return .local(url)
|
||||
}
|
||||
|
||||
return Video(videoID: videoID)
|
||||
return Video(app: app, instanceURL: instanceURL, videoID: videoID)
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21D5025f" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="1">
|
||||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21513" systemVersion="22A400" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="1">
|
||||
<entity name="Watch" representedClassName="Watch" syncable="YES">
|
||||
<attribute name="appName" optional="YES" attributeType="String"/>
|
||||
<attribute name="instanceURL" optional="YES" attributeType="URI"/>
|
||||
<attribute name="stoppedAt" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
<attribute name="videoDuration" optional="YES" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
|
||||
<attribute name="videoID" attributeType="String"/>
|
||||
@@ -11,7 +13,4 @@
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<elements>
|
||||
<element name="Watch" positionX="-63" positionY="-18" width="128" height="89"/>
|
||||
</elements>
|
||||
</model>
|
Reference in New Issue
Block a user