Commit 9dde998141
Unsigned
Layout: unified · split
internal/curd.go +4
| @@ -587,6 +587,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | |||
| 587 | "COMPLETED": "Completed", | 587 | "COMPLETED": "Completed", |
| 588 | "DROPPED": "Dropped", | 588 | "DROPPED": "Dropped", |
| 589 | "UPDATE": "Update Anime Entry", | 589 | "UPDATE": "Update Anime Entry", |
| 590 | "UNTRACKED": "Untracked Watching", | ||
| 590 | } | 591 | } |
| 591 | 592 | ||
| 592 | // Select category using DynamicSelect | 593 | // Select category using DynamicSelect |
| @@ -606,6 +607,9 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | |||
| 606 | ClearScreen() | 607 | ClearScreen() |
| 607 | UpdateAnimeEntry(userCurdConfig, user, logFile) | 608 | UpdateAnimeEntry(userCurdConfig, user, logFile) |
| 608 | ExitCurd(nil) | 609 | ExitCurd(nil) |
| 610 | } else if categorySelection.Key == "UNTRACKED" { | ||
| 611 | ClearScreen() | ||
| 612 | WatchUntracked(userCurdConfig, logFile) | ||
| 609 | } | 613 | } |
| 610 | 614 | ||
| 611 | ClearScreen() | 615 | ClearScreen() |
internal/localTracking.go +167
| @@ -6,6 +6,7 @@ import ( | |||
| 6 | "os" | 6 | "os" |
| 7 | "path/filepath" | 7 | "path/filepath" |
| 8 | "strconv" | 8 | "strconv" |
| 9 | "time" | ||
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| 11 | // Function to add an anime entry | 12 | // Function to add an anime entry |
| @@ -245,3 +246,169 @@ func LocalFindAnime(animeList []Anime, anilistID int, allanimeID string) *Anime | |||
| 245 | } | 246 | } |
| 246 | return nil | 247 | return nil |
| 247 | } | 248 | } |
| 249 | |||
| 250 | func WatchUntracked(userCurdConfig *CurdConfig, logFile string) { | ||
| 251 | var query string | ||
| 252 | var animeList map[string]string | ||
| 253 | var err error | ||
| 254 | var anime Anime | ||
| 255 | |||
| 256 | // Get anime name from user | ||
| 257 | if userCurdConfig.RofiSelection { | ||
| 258 | userInput, err := GetUserInputFromRofi("Enter the anime name") | ||
| 259 | if err != nil { | ||
| 260 | Log("Error getting user input: "+err.Error(), logFile) | ||
| 261 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) | ||
| 262 | } | ||
| 263 | query = userInput | ||
| 264 | } else { | ||
| 265 | CurdOut("Enter the anime name:") | ||
| 266 | fmt.Scanln(&query) | ||
| 267 | } | ||
| 268 | |||
| 269 | // Search for the anime | ||
| 270 | animeList, err = SearchAnime(query, userCurdConfig.SubOrDub) | ||
| 271 | if err != nil { | ||
| 272 | Log(fmt.Sprintf("Failed to search anime: %v", err), logFile) | ||
| 273 | ExitCurd(fmt.Errorf("Failed to search anime")) | ||
| 274 | } | ||
| 275 | |||
| 276 | if len(animeList) == 0 { | ||
| 277 | ExitCurd(fmt.Errorf("No results found.")) | ||
| 278 | } | ||
| 279 | |||
| 280 | // Select anime from search results | ||
| 281 | selectedAnime, err := DynamicSelect(animeList, false) | ||
| 282 | if err != nil { | ||
| 283 | Log(fmt.Sprintf("Failed to select anime: %v", err), logFile) | ||
| 284 | ExitCurd(fmt.Errorf("Failed to select anime")) | ||
| 285 | } | ||
| 286 | |||
| 287 | if selectedAnime.Key == "-1" { | ||
| 288 | ExitCurd(nil) | ||
| 289 | } | ||
| 290 | |||
| 291 | anime.AllanimeId = selectedAnime.Key | ||
| 292 | anime.Title.English = selectedAnime.Label | ||
| 293 | |||
| 294 | // Get episode number | ||
| 295 | var episodeNumber int | ||
| 296 | if userCurdConfig.RofiSelection { | ||
| 297 | userInput, err := GetUserInputFromRofi("Enter the episode number") | ||
| 298 | if err != nil { | ||
| 299 | Log("Error getting episode number: "+err.Error(), logFile) | ||
| 300 | ExitCurd(fmt.Errorf("Error getting episode number: "+err.Error())) | ||
| 301 | } | ||
| 302 | episodeNumber, err = strconv.Atoi(userInput) | ||
| 303 | if err != nil { | ||
| 304 | Log(fmt.Sprintf("Invalid episode number: %v", err), logFile) | ||
| 305 | ExitCurd(fmt.Errorf("Invalid episode number")) | ||
| 306 | } | ||
| 307 | } else { | ||
| 308 | CurdOut("Enter the episode number:") | ||
| 309 | fmt.Scanln(&episodeNumber) | ||
| 310 | } | ||
| 311 | |||
| 312 | anime.Ep.Number = episodeNumber | ||
| 313 | |||
| 314 | for { | ||
| 315 | // Get episode link | ||
| 316 | link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) | ||
| 317 | if err != nil { | ||
| 318 | Log(fmt.Sprintf("Failed to get episode link: %v", err), logFile) | ||
| 319 | ExitCurd(fmt.Errorf("Failed to get episode link")) | ||
| 320 | } | ||
| 321 | |||
| 322 | if len(link) == 0 { | ||
| 323 | ExitCurd(fmt.Errorf("No episode links found")) | ||
| 324 | } | ||
| 325 | |||
| 326 | CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number)) | ||
| 327 | |||
| 328 | // Start video playback | ||
| 329 | mpvSocketPath, err := StartVideo(PrioritizeLink(link), []string{}) | ||
| 330 | if err != nil { | ||
| 331 | Log("Failed to start mpv", logFile) | ||
| 332 | os.Exit(1) | ||
| 333 | } | ||
| 334 | |||
| 335 | anime.Ep.Player.SocketPath = mpvSocketPath | ||
| 336 | anime.Ep.Started = false | ||
| 337 | |||
| 338 | Log(fmt.Sprintf("Started mpvsocketpath ", anime.Ep.Player.SocketPath), logFile) | ||
| 339 | |||
| 340 | // Get video duration | ||
| 341 | go func() { | ||
| 342 | for { | ||
| 343 | if anime.Ep.Started { | ||
| 344 | if anime.Ep.Duration == 0 { | ||
| 345 | // Get video duration | ||
| 346 | durationPos, err := MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{"get_property", "duration"}) | ||
| 347 | if err != nil { | ||
| 348 | Log("Error getting video duration: "+err.Error(), logFile) | ||
| 349 | } else if durationPos != nil { | ||
| 350 | if duration, ok := durationPos.(float64); ok { | ||
| 351 | anime.Ep.Duration = int(duration + 0.5) // Round to nearest integer | ||
| 352 | Log(fmt.Sprintf("Video duration: %d seconds", anime.Ep.Duration), logFile) | ||
| 353 | } else { | ||
| 354 | Log("Error: duration is not a float64", logFile) | ||
| 355 | } | ||
| 356 | } | ||
| 357 | break | ||
| 358 | } | ||
| 359 | } | ||
| 360 | time.Sleep(1 * time.Second) | ||
| 361 | } | ||
| 362 | }() | ||
| 363 | |||
| 364 | // Listen for video started | ||
| 365 | for { | ||
| 366 | timePos, err := MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{"get_property", "time-pos"}) | ||
| 367 | if err != nil { | ||
| 368 | Log("Error getting playback time: "+err.Error(), logFile) | ||
| 369 | |||
| 370 | // Check if the error is due to invalid JSON | ||
| 371 | // User closed the video | ||
| 372 | if anime.Ep.Started { | ||
| 373 | percentageWatched := PercentageWatched(anime.Ep.Player.PlaybackTime, anime.Ep.Duration) | ||
| 374 | // Episode is completed | ||
| 375 | Log(fmt.Sprint(percentageWatched), logFile) | ||
| 376 | Log(fmt.Sprint(anime.Ep.Player.PlaybackTime), logFile) | ||
| 377 | Log(fmt.Sprint(anime.Ep.Duration), logFile) | ||
| 378 | Log(fmt.Sprint(userCurdConfig.PercentageToMarkComplete), logFile) | ||
| 379 | if int(percentageWatched) >= userCurdConfig.PercentageToMarkComplete { | ||
| 380 | anime.Ep.Number++ | ||
| 381 | anime.Ep.Started = false | ||
| 382 | Log("Completed episode, starting next.", logFile) | ||
| 383 | anime.Ep.IsCompleted = true | ||
| 384 | // Exit the skip loop | ||
| 385 | break | ||
| 386 | } else if fmt.Sprintf("%v", err) == "invalid character '{' after top-level value" { // Episode is not completed | ||
| 387 | Log("Received invalid JSON response, continuing...", logFile) | ||
| 388 | } else { | ||
| 389 | Log("Episode is not completed, exiting", logFile) | ||
| 390 | ExitCurd(nil) | ||
| 391 | } | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | // Convert timePos to integer | ||
| 396 | if timePos != nil { | ||
| 397 | if !anime.Ep.Started { | ||
| 398 | anime.Ep.Started = true | ||
| 399 | } | ||
| 400 | |||
| 401 | animePosition, ok := timePos.(float64) | ||
| 402 | if !ok { | ||
| 403 | Log("Error: timePos is not a float64", logFile) | ||
| 404 | continue | ||
| 405 | } | ||
| 406 | |||
| 407 | anime.Ep.Player.PlaybackTime = int(animePosition + 0.5) // Round to nearest integer | ||
| 408 | } | ||
| 409 | time.Sleep(1 * time.Second) | ||
| 410 | |||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | } | ||