mirror of
https://github.com/cranci1/Sora.git
synced 2026-01-11 20:10:24 +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) {
|
||||
let sendTraktUpdates = UserDefaults.standard.object(forKey: "sendTraktUpdates") as? Bool ?? true
|
||||
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"])))
|
||||
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 {
|
||||
Logger.shared.log("Trakt access token not found in keychain", type: "Error")
|
||||
completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Access token not found"])))
|
||||
return
|
||||
}
|
||||
|
||||
Logger.shared.log("Found Trakt access token, proceeding with API call", type: "Debug")
|
||||
|
||||
let endpoint = "/sync/history"
|
||||
let watchedAt = ISO8601DateFormatter().string(from: Date())
|
||||
let body: [String: Any]
|
||||
|
||||
switch type {
|
||||
case "movie":
|
||||
Logger.shared.log("Preparing movie watch request for TMDB ID: \(tmdbID)", type: "Debug")
|
||||
body = [
|
||||
"movies": [
|
||||
[
|
||||
|
|
@ -118,8 +112,6 @@ class TraktMutation {
|
|||
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
|
||||
if let error = error {
|
||||
Logger.shared.log("Trakt API network error: \(error.localizedDescription)", type: "Error")
|
||||
|
|
@ -133,8 +125,6 @@ class TraktMutation {
|
|||
return
|
||||
}
|
||||
|
||||
Logger.shared.log("Trakt API Response Status: \(httpResponse.statusCode)", type: "Debug")
|
||||
|
||||
if let data = data, let responseString = String(data: data, encoding: .utf8) {
|
||||
Logger.shared.log("Trakt API Response Body: \(responseString)", type: "Debug")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,9 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
|||
private let aniListMaxRetries = 6
|
||||
private let totalEpisodes: Int
|
||||
|
||||
private var traktUpdateSent = false
|
||||
private var traktUpdatedSuccessfully = false
|
||||
|
||||
var player: AVPlayer!
|
||||
var timeObserverToken: Any?
|
||||
var inactivityTimer: Timer?
|
||||
|
|
@ -1294,7 +1297,6 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
|||
dimButtonToRight = dimButton.trailingAnchor.constraint(equalTo: controlsContainerView.trailingAnchor, constant: -16)
|
||||
dimButtonToSlider.isActive = true
|
||||
}
|
||||
|
||||
private func setupLockButton() {
|
||||
let cfg = UIImage.SymbolConfiguration(pointSize: 24, weight: .regular)
|
||||
lockButton = UIButton(type: .system)
|
||||
|
|
@ -1647,42 +1649,8 @@ class CustomMediaPlayerViewController: UIViewController, UIGestureRecognizerDele
|
|||
self.tryAniListUpdate()
|
||||
}
|
||||
|
||||
if let tmdbId = self.tmdbID, tmdbId > 0 {
|
||||
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
||||
|
||||
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")
|
||||
if let tmdbId = self.tmdbID, tmdbId > 0, !self.traktUpdateSent {
|
||||
self.sendTraktUpdate(tmdbId: tmdbId)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -29,6 +29,11 @@ class VideoPlayerViewController: UIViewController {
|
|||
var subtitlesLoader: VTTSubtitlesLoader?
|
||||
var subtitleLabel: UILabel?
|
||||
|
||||
private var aniListUpdateSent = false
|
||||
private var aniListUpdatedSuccessfully = false
|
||||
private var traktUpdateSent = false
|
||||
private var traktUpdatedSuccessfully = false
|
||||
|
||||
init(module: ScrapingModule) {
|
||||
self.module = module
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
|
|
@ -203,54 +208,70 @@ class VideoPlayerViewController: UIViewController {
|
|||
let remainingPercentage = (duration - currentTime) / duration
|
||||
|
||||
if remainingPercentage < 0.1 {
|
||||
if self.aniListID != 0 {
|
||||
let aniListMutation = AniListMutation()
|
||||
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 self.aniListID != 0 && !self.aniListUpdateSent {
|
||||
self.sendAniListUpdate()
|
||||
}
|
||||
|
||||
if let tmdbId = self.tmdbID, tmdbId > 0 {
|
||||
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
||||
|
||||
let traktMutation = TraktMutation()
|
||||
|
||||
if self.isMovie {
|
||||
traktMutation.markAsWatched(type: "movie", tmdbID: tmdbId) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
Logger.shared.log("Updated Trakt progress for movie (TMDB: \(tmdbId))", type: "General")
|
||||
case .failure(let error):
|
||||
Logger.shared.log("Could not 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("Updated Trakt progress for Episode \(self.episodeNumber) (TMDB: \(tmdbId))", type: "General")
|
||||
case .failure(let error):
|
||||
Logger.shared.log("Could not 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")
|
||||
if let tmdbId = self.tmdbID, tmdbId > 0, !self.traktUpdateSent {
|
||||
self.sendTraktUpdate(tmdbId: tmdbId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func sendAniListUpdate() {
|
||||
guard !aniListUpdateSent else { return }
|
||||
|
||||
aniListUpdateSent = true
|
||||
let aniListMutation = AniListMutation()
|
||||
|
||||
aniListMutation.updateAnimeProgress(animeId: self.aniListID, episodeNumber: self.episodeNumber) { [weak self] result in
|
||||
switch result {
|
||||
case .success:
|
||||
self?.aniListUpdatedSuccessfully = true
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func sendTraktUpdate(tmdbId: Int) {
|
||||
guard !traktUpdateSent else { return }
|
||||
|
||||
traktUpdateSent = true
|
||||
Logger.shared.log("Attempting Trakt update - TMDB ID: \(tmdbId), isMovie: \(self.isMovie), episode: \(self.episodeNumber), season: \(self.seasonNumber)", type: "Debug")
|
||||
|
||||
let traktMutation = TraktMutation()
|
||||
|
||||
if self.isMovie {
|
||||
traktMutation.markAsWatched(type: "movie", tmdbID: tmdbId) { [weak self] result in
|
||||
switch result {
|
||||
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