| @@ -1,6 +1,7 @@ |
| 1 | 1 | package internal |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "crypto/md5" |
| 4 | 5 | "encoding/json" |
| 5 | 6 | "fmt" |
| 6 | 7 | "io" |
| @@ -148,10 +149,28 @@ func CurdOut(data interface{}) { |
| 148 | 149 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| 149 | 150 | } |
| 150 | 151 | case "linux": |
| 151 | | cmd := exec.Command("notify-send", fmt.Sprintf("%v", data)) |
| 152 | | err := cmd.Run() |
| 153 | | if err != nil { |
| 154 | | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| 152 | // Check if the input starts with "-i" for image notification |
| 153 | dataStr := fmt.Sprintf("%v", data) |
| 154 | if strings.HasPrefix(dataStr, "-i") && userCurdConfig.ImagePreview && userCurdConfig.RofiSelection { |
| 155 | // Split the string to get image path and message |
| 156 | parts := strings.SplitN(dataStr, " ", 3) |
| 157 | if len(parts) == 3 { |
| 158 | // Remove quotes from the message |
| 159 | message := strings.Trim(parts[2], "\"") |
| 160 | cmd := exec.Command("notify-send", "-i", parts[1], message) |
| 161 | err := cmd.Run() |
| 162 | if err != nil { |
| 163 | Log(fmt.Sprintf("%v", cmd), logFile) |
| 164 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | cmd := exec.Command("notify-send", dataStr) |
| 169 | err := cmd.Run() |
| 170 | if err != nil { |
| 171 | Log(fmt.Sprintf("%v", cmd), logFile) |
| 172 | Log(fmt.Sprintf("Failed to send notification: %v", err), logFile) |
| 173 | } |
| 155 | 174 | } |
| 156 | 175 | } |
| 157 | 176 | } |
| @@ -293,226 +312,283 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 293 | 312 | } |
| 294 | 313 | |
| 295 | 314 | func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime, logFile string) { |
| 296 | | var err error |
| 297 | | var anilistUserData map[string]interface{} |
| 298 | | var anilistUserDataPreview map[string]interface{} |
| 299 | | var animeListMap map[string]string |
| 300 | | var animeListMapPreview map[string]RofiSelectPreview |
| 301 | | |
| 302 | | // Get user id, username and Anime list |
| 303 | | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 304 | | if err != nil { |
| 305 | | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 306 | | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 307 | | } |
| 308 | | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 309 | | anilistUserDataPreview, err = GetUserDataPreview(user.Token, user.Id) |
| 310 | | if err != nil { |
| 311 | | Log(fmt.Sprintf("Failed to get user data preview: %v", err), logFile) |
| 312 | | ExitCurd(fmt.Errorf("Failed to get user data preview")) |
| 313 | | } |
| 314 | | user.AnimeList = ParseAnimeList(anilistUserDataPreview) |
| 315 | | animeListMapPreview = GetAnimeMapPreview(user.AnimeList) |
| 316 | | } else { |
| 317 | | anilistUserData, err = GetUserData(user.Token, user.Id) |
| 318 | | if err != nil { |
| 319 | | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 320 | | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 321 | | } |
| 322 | | user.AnimeList = ParseAnimeList(anilistUserData) |
| 323 | | animeListMap = GetAnimeMap(user.AnimeList) |
| 324 | | } |
| 325 | | var anilistSelectedOption SelectionOption |
| 326 | | var selectedAllanimeAnime SelectionOption |
| 327 | | var userQuery string |
| 328 | | |
| 329 | | if anime.Ep.ContinueLast { |
| 330 | | // Get the last watched anime ID from the curd_id file |
| 331 | | curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id") |
| 332 | | curdIDBytes, err := os.ReadFile(curdIDPath) |
| 333 | | if err != nil { |
| 334 | | Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) |
| 335 | | ExitCurd(fmt.Errorf("Error reading curd_id file")) |
| 336 | | } |
| 337 | | |
| 338 | | lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) |
| 339 | | if err != nil { |
| 340 | | Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile) |
| 341 | | ExitCurd(fmt.Errorf("Error converting curd_id to integer")) |
| 342 | | } |
| 343 | | |
| 344 | | anime.AnilistId = lastWatchedID |
| 345 | | anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) |
| 346 | | } else { |
| 347 | | // Select anime to watch (Anilist) |
| 348 | | var err error |
| 349 | | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 350 | | anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) |
| 351 | | } else { |
| 352 | | anilistSelectedOption, err = DynamicSelect(animeListMap, true) |
| 353 | | } |
| 354 | | if err != nil { |
| 355 | | Log(fmt.Sprintf("Error selecting anime: %v", err), logFile) |
| 356 | | ExitCurd(fmt.Errorf("Error selecting anime")) |
| 357 | | } |
| 358 | | |
| 359 | | Log(anilistSelectedOption, logFile) |
| 315 | var err error |
| 316 | var anilistUserData map[string]interface{} |
| 317 | var anilistUserDataPreview map[string]interface{} |
| 318 | |
| 319 | // Get user id, username and Anime list |
| 320 | user.Id, user.Username, err = GetAnilistUserID(user.Token) |
| 321 | if err != nil { |
| 322 | Log(fmt.Sprintf("Failed to get user ID: %v", err), logFile) |
| 323 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 324 | } |
| 360 | 325 | |
| 361 | | if anilistSelectedOption.Key == "-1" { |
| 362 | | ExitCurd(nil) |
| 363 | | } |
| 364 | | |
| 365 | | if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { |
| 366 | | anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes, logFile) |
| 367 | | } |
| 368 | | |
| 369 | | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
| 370 | | if err != nil { |
| 371 | | Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile) |
| 372 | | ExitCurd(fmt.Errorf("Error converting Anilist ID")) |
| 373 | | } |
| 374 | | } |
| 375 | | // Find anime in Local history |
| 376 | | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") |
| 377 | | |
| 378 | | // Get anime entry |
| 379 | | selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, anilistSelectedOption.Key) |
| 380 | | if err != nil { |
| 381 | | Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err), logFile) |
| 382 | | ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist")) |
| 383 | | } |
| 326 | // Get the anime list data |
| 327 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 328 | anilistUserDataPreview, err = GetUserDataPreview(user.Token, user.Id) |
| 329 | if err != nil { |
| 330 | Log(fmt.Sprintf("Failed to get user data preview: %v", err), logFile) |
| 331 | ExitCurd(fmt.Errorf("Failed to get user data preview")) |
| 332 | } |
| 333 | user.AnimeList = ParseAnimeList(anilistUserDataPreview) |
| 334 | } else { |
| 335 | anilistUserData, err = GetUserData(user.Token, user.Id) |
| 336 | if err != nil { |
| 337 | Log(fmt.Sprintf("Failed to get user data: %v", err), logFile) |
| 338 | ExitCurd(fmt.Errorf("Failed to get user ID\nYou can reset the token by running `curd -change-token`")) |
| 339 | } |
| 340 | user.AnimeList = ParseAnimeList(anilistUserData) |
| 341 | } |
| 384 | 342 | |
| 385 | | // Set anime entry |
| 386 | | anime.Title = selectedAnilistAnime.Media.Title |
| 387 | | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes |
| 388 | | anime.Ep.Number = selectedAnilistAnime.Progress+1 |
| 389 | | var animeList map[string]string |
| 390 | | userQuery = anime.Title.Romaji |
| 391 | | |
| 392 | | // if anime not found in database, find it in animeList |
| 393 | | if animePointer == nil { |
| 394 | | Log("Anime not found in database, searching in animeList...", logFile) |
| 395 | | // Get Anime list (All anime) |
| 396 | | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub), logFile) |
| 397 | | |
| 398 | | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 399 | | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err), logFile) |
| 400 | | if err != nil { |
| 401 | | Log(fmt.Sprintf("Failed to select anime: %v", err), logFile) |
| 402 | | ExitCurd(fmt.Errorf("Failed to select anime")) |
| 403 | | } |
| 404 | | if len(animeList) == 0 { |
| 405 | | ExitCurd(fmt.Errorf("No results found.")) |
| 406 | | } |
| 343 | // Skip category selection if Current flag is set |
| 344 | var categorySelection SelectionOption |
| 345 | if userCurdConfig.CurrentCategory { |
| 346 | categorySelection = SelectionOption{ |
| 347 | Key: "CURRENT", |
| 348 | Label: "Currently Watching", |
| 349 | } |
| 350 | } else { |
| 351 | // Create category selection map |
| 352 | categories := map[string]string{ |
| 353 | "CURRENT": "Currently Watching", |
| 354 | "PAUSED": "On Hold", |
| 355 | "PLANNING": "Plan to Watch", |
| 356 | "COMPLETED": "Completed", |
| 357 | "DROPPED": "Dropped", |
| 358 | } |
| 359 | |
| 360 | // Select category using DynamicSelect |
| 361 | var err error |
| 362 | categorySelection, err = DynamicSelect(categories, false) |
| 363 | if err != nil { |
| 364 | Log(fmt.Sprintf("Failed to select category: %v", err), logFile) |
| 365 | ExitCurd(fmt.Errorf("Failed to select category")) |
| 366 | } |
| 367 | |
| 368 | if categorySelection.Key == "-1" { |
| 369 | ExitCurd(nil) |
| 370 | } |
| 371 | ClearScreen() |
| 372 | } |
| 407 | 373 | |
| 408 | | // find anime in animeList |
| 409 | | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
| 410 | | if err != nil { |
| 411 | | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile) |
| 412 | | } |
| 374 | // Filter anime list based on selected category |
| 375 | var animeListMap map[string]string |
| 376 | var animeListMapPreview map[string]RofiSelectPreview |
| 377 | |
| 378 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 379 | animeListMapPreview = make(map[string]RofiSelectPreview) |
| 380 | for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { |
| 381 | title := entry.Media.Title.English |
| 382 | if title == "" { |
| 383 | title = entry.Media.Title.Romaji |
| 384 | } |
| 385 | animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ |
| 386 | Title: title, |
| 387 | CoverImage: entry.CoverImage, |
| 388 | } |
| 389 | } |
| 390 | } else { |
| 391 | animeListMap = make(map[string]string) |
| 392 | for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { |
| 393 | title := entry.Media.Title.English |
| 394 | if title == "" { |
| 395 | title = entry.Media.Title.Romaji |
| 396 | } |
| 397 | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 398 | } |
| 399 | } |
| 413 | 400 | |
| 414 | | // If unable to get Allanime id automatically get manually |
| 415 | | if anime.AllanimeId == "" { |
| 416 | | CurdOut("Failed to automatically select anime") |
| 417 | | selectedAllanimeAnime, err := DynamicSelect(animeList, false) |
| 401 | var anilistSelectedOption SelectionOption |
| 402 | var selectedAllanimeAnime SelectionOption |
| 403 | var userQuery string |
| 404 | |
| 405 | if anime.Ep.ContinueLast { |
| 406 | // Get the last watched anime ID from the curd_id file |
| 407 | curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id") |
| 408 | curdIDBytes, err := os.ReadFile(curdIDPath) |
| 409 | if err != nil { |
| 410 | Log(fmt.Sprintf("Error reading curd_id file: %v", err), logFile) |
| 411 | ExitCurd(fmt.Errorf("Error reading curd_id file")) |
| 412 | } |
| 413 | |
| 414 | lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) |
| 415 | if err != nil { |
| 416 | Log(fmt.Sprintf("Error converting curd_id to integer: %v", err), logFile) |
| 417 | ExitCurd(fmt.Errorf("Error converting curd_id to integer")) |
| 418 | } |
| 419 | |
| 420 | anime.AnilistId = lastWatchedID |
| 421 | anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) |
| 422 | } else { |
| 423 | // Select anime to watch (Anilist) |
| 424 | var err error |
| 425 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 426 | anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) |
| 427 | } else { |
| 428 | anilistSelectedOption, err = DynamicSelect(animeListMap, true) |
| 429 | } |
| 430 | if err != nil { |
| 431 | Log(fmt.Sprintf("Error selecting anime: %v", err), logFile) |
| 432 | ExitCurd(fmt.Errorf("Error selecting anime")) |
| 433 | } |
| 434 | |
| 435 | Log(anilistSelectedOption, logFile) |
| 436 | |
| 437 | if anilistSelectedOption.Key == "-1" { |
| 438 | ExitCurd(nil) |
| 439 | } |
| 440 | |
| 441 | if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { |
| 442 | anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes, logFile) |
| 443 | } |
| 444 | |
| 445 | anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) |
| 446 | if err != nil { |
| 447 | Log(fmt.Sprintf("Error converting Anilist ID: %v", err), logFile) |
| 448 | ExitCurd(fmt.Errorf("Error converting Anilist ID")) |
| 449 | } |
| 450 | } |
| 451 | // Find anime in Local history |
| 452 | animePointer := LocalFindAnime(*databaseAnimes, anime.AnilistId, "") |
| 453 | |
| 454 | // Get anime entry |
| 455 | selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, anilistSelectedOption.Key) |
| 456 | if err != nil { |
| 457 | Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err), logFile) |
| 458 | ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist")) |
| 459 | } |
| 418 | 460 | |
| 419 | | if err != nil { |
| 420 | | // fmt.Println("No anime available") |
| 421 | | ExitCurd(fmt.Errorf("No anime available")) |
| 422 | | } |
| 423 | | anime.AllanimeId = selectedAllanimeAnime.Key |
| 424 | | } |
| 425 | | } else { |
| 426 | | // if anime found in database, use it |
| 427 | | anime.AllanimeId = animePointer.AllanimeId |
| 428 | | anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime |
| 429 | | anime.Ep.Resume = true |
| 430 | | anime.Ep.Number = animePointer.Ep.Number |
| 431 | | } |
| 461 | // Set anime entry |
| 462 | anime.Title = selectedAnilistAnime.Media.Title |
| 463 | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes |
| 464 | anime.Ep.Number = selectedAnilistAnime.Progress+1 |
| 465 | var animeList map[string]string |
| 466 | userQuery = anime.Title.Romaji |
| 467 | |
| 468 | // if anime not found in database, find it in animeList |
| 469 | if animePointer == nil { |
| 470 | Log("Anime not found in database, searching in animeList...", logFile) |
| 471 | // Get Anime list (All anime) |
| 472 | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub), logFile) |
| 473 | |
| 474 | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 475 | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err), logFile) |
| 476 | if err != nil { |
| 477 | Log(fmt.Sprintf("Failed to select anime: %v", err), logFile) |
| 478 | ExitCurd(fmt.Errorf("Failed to select anime")) |
| 479 | } |
| 480 | if len(animeList) == 0 { |
| 481 | ExitCurd(fmt.Errorf("No results found.")) |
| 482 | } |
| 483 | |
| 484 | // find anime in animeList |
| 485 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
| 486 | if err != nil { |
| 487 | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err), logFile) |
| 488 | } |
| 489 | |
| 490 | // If unable to get Allanime id automatically get manually |
| 491 | if anime.AllanimeId == "" { |
| 492 | CurdOut("Failed to automatically select anime") |
| 493 | selectedAllanimeAnime, err := DynamicSelect(animeList, false) |
| 494 | |
| 495 | if err != nil { |
| 496 | // fmt.Println("No anime available") |
| 497 | ExitCurd(fmt.Errorf("No anime available")) |
| 498 | } |
| 499 | anime.AllanimeId = selectedAllanimeAnime.Key |
| 500 | } |
| 501 | } else { |
| 502 | // if anime found in database, use it |
| 503 | anime.AllanimeId = animePointer.AllanimeId |
| 504 | anime.Ep.Player.PlaybackTime = animePointer.Ep.Player.PlaybackTime |
| 505 | anime.Ep.Resume = true |
| 506 | anime.Ep.Number = animePointer.Ep.Number |
| 507 | } |
| 432 | 508 | |
| 433 | | if selectedAllanimeAnime.Key == "-1" { |
| 434 | | ExitCurd(nil) |
| 435 | | } |
| 509 | if selectedAllanimeAnime.Key == "-1" { |
| 510 | ExitCurd(nil) |
| 511 | } |
| 436 | 512 | |
| 437 | | // If upstream is ahead, update the episode number |
| 438 | | if temp_anime, err := FindAnimeByAnilistID(user.AnimeList, strconv.Itoa(anime.AnilistId)); err == nil { |
| 439 | | if temp_anime.Progress > anime.Ep.Number { |
| 440 | | anime.Ep.Number = temp_anime.Progress |
| 441 | | anime.Ep.Player.PlaybackTime = 0 |
| 442 | | anime.Ep.Resume = false |
| 443 | | } |
| 444 | | } |
| 513 | // If upstream is ahead, update the episode number |
| 514 | if temp_anime, err := FindAnimeByAnilistID(user.AnimeList, strconv.Itoa(anime.AnilistId)); err == nil { |
| 515 | if temp_anime.Progress > anime.Ep.Number { |
| 516 | anime.Ep.Number = temp_anime.Progress |
| 517 | anime.Ep.Player.PlaybackTime = 0 |
| 518 | anime.Ep.Resume = false |
| 519 | } |
| 520 | } |
| 445 | 521 | |
| 446 | | if anime.TotalEpisodes == 0 { |
| 447 | | // Get updated anime data |
| 448 | | Log(selectedAllanimeAnime, logFile) |
| 449 | | updatedAnime, err := GetAnimeDataByID(anime.AnilistId, user.Token) |
| 450 | | Log(updatedAnime, logFile) |
| 451 | | if err != nil { |
| 452 | | Log(fmt.Sprintf("Error getting updated anime data: %v", err), logFile) |
| 453 | | } else { |
| 454 | | anime.TotalEpisodes = updatedAnime.TotalEpisodes |
| 455 | | Log(fmt.Sprintf("Updated total episodes: %d", anime.TotalEpisodes), logFile) |
| 456 | | } |
| 457 | | } |
| 522 | if anime.TotalEpisodes == 0 { |
| 523 | // Get updated anime data |
| 524 | Log(selectedAllanimeAnime, logFile) |
| 525 | updatedAnime, err := GetAnimeDataByID(anime.AnilistId, user.Token) |
| 526 | Log(updatedAnime, logFile) |
| 527 | if err != nil { |
| 528 | Log(fmt.Sprintf("Error getting updated anime data: %v", err), logFile) |
| 529 | } else { |
| 530 | anime.TotalEpisodes = updatedAnime.TotalEpisodes |
| 531 | Log(fmt.Sprintf("Updated total episodes: %d", anime.TotalEpisodes), logFile) |
| 532 | } |
| 533 | } |
| 458 | 534 | |
| 459 | | if anime.TotalEpisodes == 0 { // If failed to get anime data |
| 460 | | CurdOut("Failed to get anime data. Attempting to retrieve from anime list.") |
| 461 | | animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 462 | | if err != nil { |
| 463 | | CurdOut(fmt.Sprintf("Failed to retrieve anime list: %v", err)) |
| 464 | | } else { |
| 465 | | for allanimeId, label := range animeList { |
| 466 | | if allanimeId == anime.AllanimeId { |
| 467 | | // Extract total episodes from the label |
| 468 | | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 { |
| 469 | | anime.TotalEpisodes, _ = strconv.Atoi(matches[1]) |
| 470 | | CurdOut(fmt.Sprintf("Retrieved total episodes: %d", anime.TotalEpisodes)) |
| 471 | | break |
| 472 | | } |
| 473 | | } |
| 474 | | } |
| 475 | | } |
| 476 | | |
| 477 | | if anime.TotalEpisodes == 0 { |
| 478 | | CurdOut("Still unable to determine total episodes.") |
| 479 | | CurdOut(fmt.Sprintf("Your AniList progress: %d", selectedAnilistAnime.Progress)) |
| 480 | | var episodeNumber int |
| 481 | | if userCurdConfig.RofiSelection { |
| 482 | | userInput, err := GetUserInputFromRofi("Enter the episode you want to start from") |
| 483 | | if err != nil { |
| 484 | | Log("Error getting user input: "+err.Error(), logFile) |
| 485 | | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 486 | | } |
| 487 | | episodeNumber, err = strconv.Atoi(userInput) |
| 488 | | } else { |
| 489 | | fmt.Print("Enter the episode you want to start from: ") |
| 490 | | fmt.Scanln(&episodeNumber) |
| 491 | | } |
| 492 | | anime.Ep.Number = episodeNumber |
| 493 | | } else { |
| 494 | | anime.Ep.Number = selectedAnilistAnime.Progress + 1 |
| 495 | | } |
| 496 | | } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases |
| 497 | | Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile) |
| 498 | | var answer string |
| 499 | | if userCurdConfig.RofiSelection { |
| 500 | | userInput, err := GetUserInputFromRofi("Would like to start the anime from beginning? (y/n)") |
| 501 | | if err != nil { |
| 502 | | Log("Error getting user input: "+err.Error(), logFile) |
| 503 | | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 504 | | } |
| 505 | | answer = userInput |
| 506 | | } else { |
| 507 | | fmt.Printf("Would like to start the anime from beginning? (y/n)\n") |
| 508 | | fmt.Scanln(&answer) |
| 509 | | } |
| 510 | | if answer == "y" { |
| 511 | | anime.Ep.Number = 1 |
| 512 | | } else { |
| 513 | | anime.Ep.Number = anime.TotalEpisodes |
| 514 | | } |
| 515 | | } |
| 535 | if anime.TotalEpisodes == 0 { // If failed to get anime data |
| 536 | CurdOut("Failed to get anime data. Attempting to retrieve from anime list.") |
| 537 | animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 538 | if err != nil { |
| 539 | CurdOut(fmt.Sprintf("Failed to retrieve anime list: %v", err)) |
| 540 | } else { |
| 541 | for allanimeId, label := range animeList { |
| 542 | if allanimeId == anime.AllanimeId { |
| 543 | // Extract total episodes from the label |
| 544 | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 { |
| 545 | anime.TotalEpisodes, _ = strconv.Atoi(matches[1]) |
| 546 | CurdOut(fmt.Sprintf("Retrieved total episodes: %d", anime.TotalEpisodes)) |
| 547 | break |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if anime.TotalEpisodes == 0 { |
| 554 | CurdOut("Still unable to determine total episodes.") |
| 555 | CurdOut(fmt.Sprintf("Your AniList progress: %d", selectedAnilistAnime.Progress)) |
| 556 | var episodeNumber int |
| 557 | if userCurdConfig.RofiSelection { |
| 558 | userInput, err := GetUserInputFromRofi("Enter the episode you want to start from") |
| 559 | if err != nil { |
| 560 | Log("Error getting user input: "+err.Error(), logFile) |
| 561 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 562 | } |
| 563 | episodeNumber, err = strconv.Atoi(userInput) |
| 564 | } else { |
| 565 | fmt.Print("Enter the episode you want to start from: ") |
| 566 | fmt.Scanln(&episodeNumber) |
| 567 | } |
| 568 | anime.Ep.Number = episodeNumber |
| 569 | } else { |
| 570 | anime.Ep.Number = selectedAnilistAnime.Progress + 1 |
| 571 | } |
| 572 | } else if anime.TotalEpisodes < anime.Ep.Number { // Handle weird cases |
| 573 | Log(fmt.Sprintf("Weird case: anime.TotalEpisodes < anime.Ep.Number: %v < %v", anime.TotalEpisodes, anime.Ep.Number), logFile) |
| 574 | var answer string |
| 575 | if userCurdConfig.RofiSelection { |
| 576 | userInput, err := GetUserInputFromRofi("Would like to start the anime from beginning? (y/n)") |
| 577 | if err != nil { |
| 578 | Log("Error getting user input: "+err.Error(), logFile) |
| 579 | ExitCurd(fmt.Errorf("Error getting user input: "+err.Error())) |
| 580 | } |
| 581 | answer = userInput |
| 582 | } else { |
| 583 | fmt.Printf("Would like to start the anime from beginning? (y/n)\n") |
| 584 | fmt.Scanln(&answer) |
| 585 | } |
| 586 | if answer == "y" { |
| 587 | anime.Ep.Number = 1 |
| 588 | } else { |
| 589 | anime.Ep.Number = anime.TotalEpisodes |
| 590 | } |
| 591 | } |
| 516 | 592 | |
| 517 | 593 | } |
| 518 | 594 | |
| @@ -585,6 +661,30 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string |
| 585 | 661 | } |
| 586 | 662 | } |
| 587 | 663 | |
| 664 | // Display starting message with cover image and episode info |
| 665 | if anime.CoverImage != "" && userCurdConfig.ImagePreview && userCurdConfig.RofiSelection { |
| 666 | // Get the cached image path |
| 667 | cacheDir := os.ExpandEnv("${HOME}/.cache/curd/images") |
| 668 | filename := fmt.Sprintf("%x.jpg", md5.Sum([]byte(anime.CoverImage))) |
| 669 | cachePath := filepath.Join(cacheDir, filename) |
| 670 | |
| 671 | // Display the image if it exists in cache |
| 672 | _, err := os.Stat(cachePath) |
| 673 | if err == nil { |
| 674 | // File exists |
| 675 | Log(fmt.Sprintf("Image found at %s", cachePath), logFile) |
| 676 | CurdOut(fmt.Sprintf("-i %s \"%s - Episode %d\"", cachePath, GetAnimeName(*anime), anime.Ep.Number)) |
| 677 | } else { |
| 678 | // File does not exist |
| 679 | Log(fmt.Sprintf("Image does not exist at %s", cachePath), logFile) |
| 680 | CurdOut(fmt.Sprintf("%s - Episode %d", |
| 681 | GetAnimeName(*anime), |
| 682 | anime.Ep.Number)) |
| 683 | |
| 684 | } |
| 685 | } else { |
| 686 | CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number)) |
| 687 | } |
| 588 | 688 | mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}) |
| 589 | 689 | |
| 590 | 690 | if err != nil { |
| @@ -637,3 +737,20 @@ func CheckAndDownloadFiles(storagePath string, filesToCheck []string) error { |
| 637 | 737 | return nil |
| 638 | 738 | } |
| 639 | 739 | |
| 740 | func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 741 | switch category { |
| 742 | case "CURRENT": |
| 743 | return list.Watching |
| 744 | case "COMPLETED": |
| 745 | return list.Completed |
| 746 | case "PAUSED": |
| 747 | return list.Paused |
| 748 | case "DROPPED": |
| 749 | return list.Dropped |
| 750 | case "PLANNING": |
| 751 | return list.Planning |
| 752 | default: |
| 753 | return []Entry{} |
| 754 | } |
| 755 | } |
| 756 | |