mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 16:42:01 +00:00
fixed multi API calls
This commit is contained in:
parent
51dcae1a54
commit
b5efb1ad19
3 changed files with 101 additions and 93 deletions
|
|
@ -33,28 +33,22 @@ class TraktMutation {
|
||||||
func markAsWatched(type: String, tmdbID: Int, episodeNumber: Int? = nil, seasonNumber: Int? = nil, completion: @escaping (Result<Void, Error>) -> Void) {
|
func markAsWatched(type: String, tmdbID: Int, episodeNumber: Int? = nil, seasonNumber: Int? = nil, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||||
let sendTraktUpdates = UserDefaults.standard.object(forKey: "sendTraktUpdates") as? Bool ?? true
|
let sendTraktUpdates = UserDefaults.standard.object(forKey: "sendTraktUpdates") as? Bool ?? true
|
||||||
if !sendTraktUpdates {
|
if !sendTraktUpdates {
|
||||||
Logger.shared.log("Trakt updates disabled by user preference", type: "Debug")
|
|
||||||
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Trakt updates disabled by user"])))
|
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Trakt updates disabled by user"])))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.shared.log("Attempting to mark \(type) as watched - TMDB ID: \(tmdbID), Episode: \(episodeNumber ?? 0), Season: \(seasonNumber ?? 0)", type: "Debug")
|
|
||||||
|
|
||||||
guard let userToken = getTokenFromKeychain() else {
|
guard let userToken = getTokenFromKeychain() else {
|
||||||
Logger.shared.log("Trakt access token not found in keychain", type: "Error")
|
Logger.shared.log("Trakt access token not found in keychain", type: "Error")
|
||||||
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Access token not found"])))
|
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Access token not found"])))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.shared.log("Found Trakt access token, proceeding with API call", type: "Debug")
|
|
||||||
|
|
||||||
let endpoint = "/sync/history"
|
let endpoint = "/sync/history"
|
||||||
let watchedAt = ISO8601DateFormatter().string(from: Date())
|
let watchedAt = ISO8601DateFormatter().string(from: Date())
|
||||||
let body: [String: Any]
|
let body: [String: Any]
|
||||||
|
|
||||||
switch type {
|
switch type {
|
||||||
case "movie":
|
case "movie":
|
||||||
Logger.shared.log("Preparing movie watch request for TMDB ID: \(tmdbID)", type: "Debug")
|
|
||||||
body = [
|
body = [
|
||||||
"movies": [
|
"movies": [
|
||||||
[
|
[
|
||||||
|
|
@ -118,8 +112,6 @@ class TraktMutation {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.shared.log("Sending Trakt API request to: \(request.url?.absoluteString ?? "unknown")", type: "Debug")
|
|
||||||
|
|
||||||
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
let task = URLSession.shared.dataTask(with: request) { data, response, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
Logger.shared.log("Trakt API network error: \(error.localizedDescription)", type: "Error")
|
Logger.shared.log("Trakt API network error: \(error.localizedDescription)", type: "Error")
|
||||||
|
|
@ -133,8 +125,6 @@ class TraktMutation {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.shared.log("Trakt API Response Status: \(httpResponse.statusCode)", type: "Debug")
|
|
||||||
|
|
||||||
if let data = data, let responseString = String(data: data, encoding: .utf8) {
|
if let data = data, let responseString = String(data: data, encoding: .utf8) {
|
||||||
Logger.shared.log("Trakt API Response Body: \(responseString)", type: "Debug")
|
Logger.shared.log("Trakt API Response Body: \(responseString)", type: "Debug")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,9 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
||||||
private let aniListMaxRetries = 6
|
private let aniListMaxRetries = 6
|
||||||
private let totalEpisodes: Int
|
private let totalEpisodes: Int
|
||||||
|
|
||||||
|
private var traktUpdateSent = false
|
||||||
|
private var traktUpdatedSuccessfully = false
|
||||||
|
|
||||||
var player: AVPlayer!
|
var player: AVPlayer!
|
||||||
var timeObserverToken: Any?
|
var timeObserverToken: Any?
|
||||||
var inactivityTimer: Timer?
|
var inactivityTimer: Timer?
|
||||||
|
|
@ -1294,7 +1297,6 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
||||||
dimButtonToRight = dimButton.trailingAnchor.constraint(equalTo: controlsContainerView.trailingAnchor, constant: -16)
|
dimButtonToRight = dimButton.trailingAnchor.constraint(equalTo: controlsContainerView.trailingAnchor, constant: -16)
|
||||||
dimButtonToSlider.isActive = true
|
dimButtonToSlider.isActive = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupLockButton() {
|
private func setupLockButton() {
|
||||||
let cfg = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular)
|
let cfg = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular)
|
||||||
lockButton = UIButton(type: .system)
|
lockButton = UIButton(type: .system)
|
||||||
|
|
@ -1647,42 +1649,8 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
||||||
self.tryAniListUpdate()
|
self.tryAniListUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
if let tmdbId = self.tmdbID, tmdbId > 0 {
|
if let tmdbId = self.tmdbID, tmdbId > 0, !self.traktUpdateSent {
|
||||||
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
self.sendTraktUpdate(tmdbId: tmdbId)
|
||||||
|
|
||||||
let traktMutation = TraktMutation()
|
|
||||||
|
|
||||||
if self.isMovie {
|
|
||||||
traktMutation.markAsWatched(type: "movie", tmdbID: tmdbId) { result in
|
|
||||||
switch result {
|
|
||||||
case .success:
|
|
||||||
Logger.shared.log("Successfully updated Trakt progress for movie (TMDB: \(tmdbId))", type: "General")
|
|
||||||
case .failure(let error):
|
|
||||||
Logger.shared.log("Failed to update Trakt progress for movie: \(error.localizedDescription)", type: "Error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
guard self.episodeNumber > 0 && self.seasonNumber > 0 else {
|
|
||||||
Logger.shared.log("Invalid episode (\(self.episodeNumber)) or season (\(self.seasonNumber)) number for Trakt update", type: "Error")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
traktMutation.markAsWatched(
|
|
||||||
type: "episode",
|
|
||||||
tmdbID: tmdbId,
|
|
||||||
episodeNumber: self.episodeNumber,
|
|
||||||
seasonNumber: self.seasonNumber
|
|
||||||
) { result in
|
|
||||||
switch result {
|
|
||||||
case .success:
|
|
||||||
Logger.shared.log("Successfully updated Trakt progress for episode \(self.episodeNumber) (TMDB: \(tmdbId))", type: "General")
|
|
||||||
case .failure(let error):
|
|
||||||
Logger.shared.log("Failed to update Trakt progress for episode: \(error.localizedDescription)", type: "Error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logger.shared.log("Skipping Trakt update - TMDB ID not set or invalid: \(self.tmdbID ?? -1)", type: "Warning")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2874,6 +2842,35 @@ extension CustomMediaPlayerViewController: AVPictureInPictureControllerDelegate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// yes? Like the plural of the famous american rapper ye? -IBHRAD
|
||||||
|
// low taper fade the meme is massive -cranci
|
||||||
|
// The mind is the source of good and evil, only you yourself can decide which you will bring yourself. -seiike
|
||||||
|
// guys watch Clannad already - ibro
|
||||||
|
// May the Divine Providence bestow its infinite mercy upon your soul, and may eternal grace find you beyond the shadows of this mortal realm. - paul, 15/11/2005 - 13/05/2023
|
||||||
|
// this dumbass ↑ defo used gpt, ong he did bro
|
||||||
|
let maskLayer = CAShapeLayer()
|
||||||
|
maskLayer.path = path.cgPath
|
||||||
|
maskLayer.fillColor = nil
|
||||||
|
maskLayer.strokeColor = UIColor.white.cgColor
|
||||||
|
maskLayer.lineWidth = 0.5
|
||||||
|
gradientLayer.mask = maskLayer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension CustomMediaPlayerViewController: AVPictureInPictureControllerDelegate {
|
||||||
|
func pictureInPictureControllerWillStartPictureInPicture(_ pipController: AVPictureInPictureController) {
|
||||||
|
pipButton.alpha = 0.5
|
||||||
|
}
|
||||||
|
|
||||||
|
func pictureInPictureControllerDidStopPictureInPicture(_ pipController: AVPictureInPictureController) {
|
||||||
|
pipButton.alpha = 1.0
|
||||||
|
}
|
||||||
|
|
||||||
|
func pictureInPictureController(_ pipController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
|
||||||
|
Logger.shared.log("PiP failed to start: \(error.localizedDescription)", type: "Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// yes? Like the plural of the famous american rapper ye? -IBHRAD
|
// yes? Like the plural of the famous american rapper ye? -IBHRAD
|
||||||
// low taper fade the meme is massive -cranci
|
// low taper fade the meme is massive -cranci
|
||||||
// The mind is the source of good and evil, only you yourself can decide which you will bring yourself. -seiike
|
// The mind is the source of good and evil, only you yourself can decide which you will bring yourself. -seiike
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,11 @@ class VideoPlayerViewController: UIViewController {
|
||||||
var subtitlesLoader: VTTSubtitlesLoader?
|
var subtitlesLoader: VTTSubtitlesLoader?
|
||||||
var subtitleLabel: UILabel?
|
var subtitleLabel: UILabel?
|
||||||
|
|
||||||
|
private var aniListUpdateSent = false
|
||||||
|
private var aniListUpdatedSuccessfully = false
|
||||||
|
private var traktUpdateSent = false
|
||||||
|
private var traktUpdatedSuccessfully = false
|
||||||
|
|
||||||
init(module: ScrapingModule) {
|
init(module: ScrapingModule) {
|
||||||
self.module = module
|
self.module = module
|
||||||
super.init(nibName: nil, bundle: nil)
|
super.init(nibName: nil, bundle: nil)
|
||||||
|
|
@ -203,54 +208,70 @@ class VideoPlayerViewController: UIViewController {
|
||||||
let remainingPercentage = (duration - currentTime) / duration
|
let remainingPercentage = (duration - currentTime) / duration
|
||||||
|
|
||||||
if remainingPercentage < 0.1 {
|
if remainingPercentage < 0.1 {
|
||||||
if self.aniListID != 0 {
|
if self.aniListID != 0 && !self.aniListUpdateSent {
|
||||||
let aniListMutation = AniListMutation()
|
self.sendAniListUpdate()
|
||||||
aniListMutation.updateAnimeProgress(animeId: self.aniListID, episodeNumber: self.episodeNumber) { result in
|
|
||||||
switch result {
|
|
||||||
case .success:
|
|
||||||
Logger.shared.log("Updated AniList progress for Episode \(self.episodeNumber)", type: "General")
|
|
||||||
case .failure(let error):
|
|
||||||
Logger.shared.log("Could not update AniList progress: \(error.localizedDescription)", type: "Error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let tmdbId = self.tmdbID, tmdbId > 0 {
|
if let tmdbId = self.tmdbID, tmdbId > 0, !self.traktUpdateSent {
|
||||||
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
self.sendTraktUpdate(tmdbId: tmdbId)
|
||||||
|
}
|
||||||
let traktMutation = TraktMutation()
|
}
|
||||||
|
}
|
||||||
if self.isMovie {
|
}
|
||||||
traktMutation.markAsWatched(type: "movie", tmdbID: tmdbId) { result in
|
|
||||||
switch result {
|
private func sendAniListUpdate() {
|
||||||
case .success:
|
guard !aniListUpdateSent else { return }
|
||||||
Logger.shared.log("Updated Trakt progress for movie (TMDB: \(tmdbId))", type: "General")
|
|
||||||
case .failure(let error):
|
aniListUpdateSent = true
|
||||||
Logger.shared.log("Could not update Trakt progress for movie: \(error.localizedDescription)", type: "Error")
|
let aniListMutation = AniListMutation()
|
||||||
}
|
|
||||||
}
|
aniListMutation.updateAnimeProgress(animeId: self.aniListID, episodeNumber: self.episodeNumber) { [weak self] result in
|
||||||
} else {
|
switch result {
|
||||||
guard self.episodeNumber > 0 && self.seasonNumber > 0 else {
|
case .success:
|
||||||
Logger.shared.log("Invalid episode (\(self.episodeNumber)) or season (\(self.seasonNumber)) number for Trakt update", type: "Error")
|
self?.aniListUpdatedSuccessfully = true
|
||||||
return
|
Logger.shared.log("Successfully updated AniList progress for Episode \(self?.episodeNumber ?? 0)", type: "General")
|
||||||
}
|
case .failure(let error):
|
||||||
|
Logger.shared.log("Failed to update AniList progress: \(error.localizedDescription)", type: "Error")
|
||||||
traktMutation.markAsWatched(
|
}
|
||||||
type: "episode",
|
}
|
||||||
tmdbID: tmdbId,
|
}
|
||||||
episodeNumber: self.episodeNumber,
|
|
||||||
seasonNumber: self.seasonNumber
|
private func sendTraktUpdate(tmdbId: Int) {
|
||||||
) { result in
|
guard !traktUpdateSent else { return }
|
||||||
switch result {
|
|
||||||
case .success:
|
traktUpdateSent = true
|
||||||
Logger.shared.log("Updated Trakt progress for Episode \(self.episodeNumber) (TMDB: \(tmdbId))", type: "General")
|
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
||||||
case .failure(let error):
|
|
||||||
Logger.shared.log("Could not update Trakt progress for episode: \(error.localizedDescription)", type: "Error")
|
let traktMutation = TraktMutation()
|
||||||
}
|
|
||||||
}
|
if self.isMovie {
|
||||||
}
|
traktMutation.markAsWatched(type: "movie", tmdbID: tmdbId) { [weak self] result in
|
||||||
} else {
|
switch result {
|
||||||
Logger.shared.log("Skipping Trakt update - TMDB ID not set or invalid: \(self.tmdbID ?? -1)", type: "Warning")
|
case .success:
|
||||||
|
self?.traktUpdatedSuccessfully = true
|
||||||
|
Logger.shared.log("Successfully updated Trakt progress for movie (TMDB: \(tmdbId))", type: "General")
|
||||||
|
case .failure(let error):
|
||||||
|
Logger.shared.log("Failed to update Trakt progress for movie: \(error.localizedDescription)", type: "Error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
guard self.episodeNumber > 0 && self.seasonNumber > 0 else {
|
||||||
|
Logger.shared.log("Invalid episode (\(self.episodeNumber)) or season (\(self.seasonNumber)) number for Trakt update", type: "Error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
traktMutation.markAsWatched(
|
||||||
|
type: "episode",
|
||||||
|
tmdbID: tmdbId,
|
||||||
|
episodeNumber: self.episodeNumber,
|
||||||
|
seasonNumber: self.seasonNumber
|
||||||
|
) { [weak self] result in
|
||||||
|
switch result {
|
||||||
|
case .success:
|
||||||
|
self?.traktUpdatedSuccessfully = true
|
||||||
|
Logger.shared.log("Successfully updated Trakt progress for Episode \(self?.episodeNumber ?? 0) (TMDB: \(tmdbId))", type: "General")
|
||||||
|
case .failure(let error):
|
||||||
|
Logger.shared.log("Failed to update Trakt progress for episode: \(error.localizedDescription)", type: "Error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue