| @@ -143,7 +143,7 @@ func ExitCurd(err error) { |
| 143 | 143 | RestoreScreen() |
| 144 | 144 | |
| 145 | 145 | anime := GetGlobalAnime() |
| 146 | | if anime != nil && anime.Ep.Player.SocketPath != "" { |
| 146 | if (anime != nil) && (anime.Ep.Player.SocketPath != "") { |
| 147 | 147 | _, err = MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{"quit"}) |
| 148 | 148 | if err != nil { |
| 149 | 149 | Log("Error closing MPV: " + err.Error()) |
| @@ -223,11 +223,11 @@ func CurdOut(data interface{}) { |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { |
| 226 | | // Create update options map |
| 227 | | updateOptions := map[string]string{ |
| 228 | | "CATEGORY": "Change Anime Category", |
| 229 | | "PROGRESS": "Change Progress", |
| 230 | | "SCORE": "Add/Change Score", |
| 226 | // Create update options |
| 227 | updateOptions := []SelectionOption{ |
| 228 | {Key: "CATEGORY", Label: "Change Anime Category"}, |
| 229 | {Key: "PROGRESS", Label: "Change Progress"}, |
| 230 | {Key: "SCORE", Label: "Add/Change Score"}, |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | // Select update option |
| @@ -242,7 +242,7 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | // Get user's anime list |
| 245 | | var animeListMap map[string]string |
| 245 | var animeListOptions []SelectionOption |
| 246 | 246 | var animeListMapPreview map[string]RofiSelectPreview |
| 247 | 247 | |
| 248 | 248 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| @@ -298,43 +298,78 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { |
| 298 | 298 | CoverImage: entry.CoverImage, |
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | for _, entry := range user.AnimeList.Rewatching { |
| 302 | title := entry.Media.Title.English |
| 303 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 304 | title = entry.Media.Title.Romaji |
| 305 | } |
| 306 | animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ |
| 307 | Title: title, |
| 308 | CoverImage: entry.CoverImage, |
| 309 | } |
| 310 | } |
| 301 | 311 | } else { |
| 302 | | animeListMap = make(map[string]string) |
| 312 | animeListOptions = make([]SelectionOption, 0) |
| 303 | 313 | // Include anime from all categories |
| 304 | 314 | for _, entry := range user.AnimeList.Watching { |
| 305 | 315 | title := entry.Media.Title.English |
| 306 | 316 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 307 | 317 | title = entry.Media.Title.Romaji |
| 308 | 318 | } |
| 309 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 319 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 320 | Key: strconv.Itoa(entry.Media.ID), |
| 321 | Label: title, |
| 322 | }) |
| 310 | 323 | } |
| 311 | 324 | for _, entry := range user.AnimeList.Completed { |
| 312 | 325 | title := entry.Media.Title.English |
| 313 | 326 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 314 | 327 | title = entry.Media.Title.Romaji |
| 315 | 328 | } |
| 316 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 329 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 330 | Key: strconv.Itoa(entry.Media.ID), |
| 331 | Label: title, |
| 332 | }) |
| 317 | 333 | } |
| 318 | 334 | for _, entry := range user.AnimeList.Paused { |
| 319 | 335 | title := entry.Media.Title.English |
| 320 | 336 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 321 | 337 | title = entry.Media.Title.Romaji |
| 322 | 338 | } |
| 323 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 339 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 340 | Key: strconv.Itoa(entry.Media.ID), |
| 341 | Label: title, |
| 342 | }) |
| 324 | 343 | } |
| 325 | 344 | for _, entry := range user.AnimeList.Dropped { |
| 326 | 345 | title := entry.Media.Title.English |
| 327 | 346 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 328 | 347 | title = entry.Media.Title.Romaji |
| 329 | 348 | } |
| 330 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 349 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 350 | Key: strconv.Itoa(entry.Media.ID), |
| 351 | Label: title, |
| 352 | }) |
| 331 | 353 | } |
| 332 | 354 | for _, entry := range user.AnimeList.Planning { |
| 333 | 355 | title := entry.Media.Title.English |
| 334 | 356 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 335 | 357 | title = entry.Media.Title.Romaji |
| 336 | 358 | } |
| 337 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 359 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 360 | Key: strconv.Itoa(entry.Media.ID), |
| 361 | Label: title, |
| 362 | }) |
| 363 | } |
| 364 | for _, entry := range user.AnimeList.Rewatching { |
| 365 | title := entry.Media.Title.English |
| 366 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 367 | title = entry.Media.Title.Romaji |
| 368 | } |
| 369 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 370 | Key: strconv.Itoa(entry.Media.ID), |
| 371 | Label: title, |
| 372 | }) |
| 338 | 373 | } |
| 339 | 374 | } |
| 340 | 375 | |
| @@ -343,7 +378,7 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { |
| 343 | 378 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 344 | 379 | selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false) |
| 345 | 380 | } else { |
| 346 | | selectedAnime, err = DynamicSelect(animeListMap) |
| 381 | selectedAnime, err = DynamicSelect(animeListOptions) |
| 347 | 382 | } |
| 348 | 383 | if err != nil { |
| 349 | 384 | Log(fmt.Sprintf("Failed to select anime: %v", err)) |
| @@ -369,17 +404,24 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { |
| 369 | 404 | ClearScreen() |
| 370 | 405 | switch updateSelection.Key { |
| 371 | 406 | case "CATEGORY": |
| 372 | | categories := map[string]string{ |
| 373 | | "CURRENT": "Currently Watching", |
| 374 | | "COMPLETED": "Completed", |
| 375 | | "PAUSED": "On Hold", |
| 376 | | "DROPPED": "Dropped", |
| 377 | | "PLANNING": "Plan to Watch", |
| 407 | categories := []SelectionOption{ |
| 408 | {Key: "CURRENT", Label: "Currently Watching"}, |
| 409 | {Key: "COMPLETED", Label: "Completed"}, |
| 410 | {Key: "PAUSED", Label: "On Hold"}, |
| 411 | {Key: "DROPPED", Label: "Dropped"}, |
| 412 | {Key: "PLANNING", Label: "Plan to Watch"}, |
| 413 | {Key: "REPEATING", Label: "Rewatching"}, // Anilist uses REPEATING for rewatching |
| 378 | 414 | } |
| 379 | 415 | |
| 380 | 416 | currentStatus := "None" |
| 381 | 417 | if selectedAnilistAnime.Status != "" { |
| 382 | | currentStatus = categories[selectedAnilistAnime.Status] |
| 418 | // Find the label for the current status |
| 419 | for _, cat := range categories { |
| 420 | if cat.Key == selectedAnilistAnime.Status { |
| 421 | currentStatus = cat.Label |
| 422 | break |
| 423 | } |
| 424 | } |
| 383 | 425 | } |
| 384 | 426 | CurdOut(fmt.Sprintf("Current category: %s", currentStatus)) |
| 385 | 427 | |
| @@ -550,6 +592,7 @@ func UpdateCurd(repo, fileName string) error { |
| 550 | 592 | |
| 551 | 593 | func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime) SelectionOption { |
| 552 | 594 | var query string |
| 595 | var animeOptions []SelectionOption |
| 553 | 596 | var animeMapPreview map[string]RofiSelectPreview |
| 554 | 597 | var animeOptions []SelectionOption |
| 555 | 598 | var err error |
| @@ -571,7 +614,19 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 571 | 614 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 572 | 615 | animeMapPreview, err = SearchAnimeAnilistPreview(query, user.Token) |
| 573 | 616 | } else { |
| 574 | | animeOptions, err = SearchAnimeAnilist(query, user.Token) |
| 617 | animeMap, err := SearchAnimeAnilist(query, user.Token) |
| 618 | if err != nil { |
| 619 | Log(fmt.Sprintf("Failed to search anime: %v", err)) |
| 620 | ExitCurd(fmt.Errorf("Failed to search anime")) |
| 621 | } |
| 622 | // Convert map to []SelectionOption |
| 623 | animeOptions = make([]SelectionOption, 0, len(animeMap)) |
| 624 | for key, label := range animeMap { |
| 625 | animeOptions = append(animeOptions, SelectionOption{ |
| 626 | Key: key, |
| 627 | Label: label, |
| 628 | }) |
| 629 | } |
| 575 | 630 | } |
| 576 | 631 | if err != nil { |
| 577 | 632 | Log(fmt.Sprintf("Failed to search anime: %v", err)) |
| @@ -580,7 +635,7 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 580 | 635 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 581 | 636 | anilistSelectedOption, err = DynamicSelectPreview(animeMapPreview, false) |
| 582 | 637 | } else { |
| 583 | | anilistSelectedOption, err = DynamicSelectFromSlice(animeOptions) |
| 638 | anilistSelectedOption, err = DynamicSelect(animeOptions) |
| 584 | 639 | } |
| 585 | 640 | |
| 586 | 641 | if anilistSelectedOption.Key == "-1" { |
| @@ -598,12 +653,13 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA |
| 598 | 653 | } |
| 599 | 654 | |
| 600 | 655 | // Add category selection before adding to list |
| 601 | | categories := map[string]string{ |
| 602 | | "CURRENT": "Currently Watching", |
| 603 | | "COMPLETED": "Completed", |
| 604 | | "PAUSED": "On Hold", |
| 605 | | "DROPPED": "Dropped", |
| 606 | | "PLANNING": "Plan to Watch", |
| 656 | categories := []SelectionOption{ |
| 657 | {Key: "CURRENT", Label: "Currently Watching"}, |
| 658 | {Key: "COMPLETED", Label: "Completed"}, |
| 659 | {Key: "PAUSED", Label: "On Hold"}, |
| 660 | {Key: "DROPPED", Label: "Dropped"}, |
| 661 | {Key: "PLANNING", Label: "Plan to Watch"}, |
| 662 | {Key: "REPEATING", Label: "Rewatching"}, // Anilist uses REPEATING for rewatching |
| 607 | 663 | } |
| 608 | 664 | |
| 609 | 665 | ClearScreen() |
| @@ -651,7 +707,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 651 | 707 | var anilistUserDataPreview map[string]interface{} |
| 652 | 708 | |
| 653 | 709 | // Filter anime list based on selected category |
| 654 | | var animeListMap map[string]string |
| 710 | var animeListOptions []SelectionOption |
| 655 | 711 | var animeListMapPreview map[string]RofiSelectPreview |
| 656 | 712 | |
| 657 | 713 | // Get user id, username and Anime list |
| @@ -718,11 +774,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 718 | 774 | } |
| 719 | 775 | } else { |
| 720 | 776 | // Create category selection map |
| 721 | | categories := getOrderedCategories(userCurdConfig) |
| 777 | // Get ordered categories |
| 778 | orderedCategories := getOrderedCategories(userCurdConfig) |
| 779 | |
| 780 | // Use DynamicSelect with ordered categories directly |
| 781 | categorySelection, err = DynamicSelect(orderedCategories) |
| 722 | 782 | |
| 723 | | // Select category using DynamicSelect |
| 724 | | var err error |
| 725 | | categorySelection, err = DynamicSelect(categories) |
| 726 | 783 | if err != nil { |
| 727 | 784 | Log(fmt.Sprintf("Failed to select category: %v", err)) |
| 728 | 785 | ExitCurd(fmt.Errorf("Failed to select category")) |
| @@ -760,13 +817,16 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 760 | 817 | } |
| 761 | 818 | } |
| 762 | 819 | } else { |
| 763 | | animeListMap = make(map[string]string) |
| 820 | animeListOptions = make([]SelectionOption, 0) |
| 764 | 821 | for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { |
| 765 | 822 | title := entry.Media.Title.English |
| 766 | 823 | if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { |
| 767 | 824 | title = entry.Media.Title.Romaji |
| 768 | 825 | } |
| 769 | | animeListMap[strconv.Itoa(entry.Media.ID)] = title |
| 826 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 827 | Key: strconv.Itoa(entry.Media.ID), |
| 828 | Label: title, |
| 829 | }) |
| 770 | 830 | } |
| 771 | 831 | } |
| 772 | 832 | } |
| @@ -798,10 +858,13 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 798 | 858 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 799 | 859 | anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) |
| 800 | 860 | } else { |
| 801 | | // Add "Add new anime" option to the map |
| 802 | | animeListMap["add_new"] = "Add new anime" |
| 861 | // Add "Add new anime" option to the slice |
| 862 | animeListOptions = append(animeListOptions, SelectionOption{ |
| 863 | Key: "add_new", |
| 864 | Label: "Add new anime", |
| 865 | }) |
| 803 | 866 | |
| 804 | | anilistSelectedOption, err = DynamicSelect(animeListMap) |
| 867 | anilistSelectedOption, err = DynamicSelect(animeListOptions) |
| 805 | 868 | } |
| 806 | 869 | if err != nil { |
| 807 | 870 | Log(fmt.Sprintf("Error selecting anime: %v", err)) |
| @@ -838,7 +901,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 838 | 901 | anime.Title = selectedAnilistAnime.Media.Title |
| 839 | 902 | anime.TotalEpisodes = selectedAnilistAnime.Media.Episodes |
| 840 | 903 | anime.Ep.Number = selectedAnilistAnime.Progress + 1 |
| 841 | | var animeList map[string]string |
| 904 | var animeList []SelectionOption |
| 842 | 905 | userQuery = anime.Title.Romaji |
| 843 | 906 | |
| 844 | 907 | // if anime not found in database, find it in animeList |
| @@ -848,7 +911,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 848 | 911 | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub)) |
| 849 | 912 | |
| 850 | 913 | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 851 | | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err)) |
| 852 | 914 | if err != nil { |
| 853 | 915 | Log(fmt.Sprintf("Failed to select anime: %v", err)) |
| 854 | 916 | ExitCurd(fmt.Errorf("Failed to select anime")) |
| @@ -857,10 +919,21 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 857 | 919 | ExitCurd(fmt.Errorf("No results found.")) |
| 858 | 920 | } |
| 859 | 921 | |
| 860 | | // find anime in animeList |
| 861 | | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) |
| 862 | | if err != nil { |
| 863 | | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err)) |
| 922 | // find anime in animeList by searching through the options |
| 923 | targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes) |
| 924 | found := false |
| 925 | for i, option := range animeList { |
| 926 | Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s'", i, option.Key, option.Label)) |
| 927 | if option.Label == targetLabel { |
| 928 | anime.AllanimeId = option.Key |
| 929 | Log(fmt.Sprintf("Found exact match! Setting AllanimeId to: %s", anime.AllanimeId)) |
| 930 | found = true |
| 931 | break |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | if !found { |
| 936 | Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel)) |
| 864 | 937 | } |
| 865 | 938 | |
| 866 | 939 | // If unable to get Allanime id automatically get manually |
| @@ -873,7 +946,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 873 | 946 | } |
| 874 | 947 | |
| 875 | 948 | if err != nil { |
| 876 | | // fmt.Println("No anime available") |
| 877 | 949 | ExitCurd(fmt.Errorf("No anime available")) |
| 878 | 950 | } |
| 879 | 951 | anime.AllanimeId = selectedAllanimeAnime.Key |
| @@ -902,9 +974,9 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 902 | 974 | |
| 903 | 975 | if !isInWatchingList { |
| 904 | 976 | // Create options for the prompt |
| 905 | | options := map[string]string{ |
| 906 | | "yes": "Add to watching list", |
| 907 | | "no": "Continue without adding", |
| 977 | options := []SelectionOption{ |
| 978 | {Key: "yes", Label: "Add to watching list"}, |
| 979 | {Key: "no", Label: "Continue without adding"}, |
| 908 | 980 | } |
| 909 | 981 | |
| 910 | 982 | var selectedOption SelectionOption |
| @@ -972,10 +1044,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 972 | 1044 | if err != nil { |
| 973 | 1045 | CurdOut(fmt.Sprintf("Failed to retrieve anime list: %v", err)) |
| 974 | 1046 | } else { |
| 975 | | for allanimeId, label := range animeList { |
| 976 | | if allanimeId == anime.AllanimeId { |
| 1047 | for _, option := range animeList { |
| 1048 | if option.Key == anime.AllanimeId { |
| 977 | 1049 | // Extract total episodes from the label |
| 978 | | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(label); len(matches) > 1 { |
| 1050 | if matches := regexp.MustCompile(`\((\d+) episodes\)`).FindStringSubmatch(option.Label); len(matches) > 1 { |
| 979 | 1051 | anime.TotalEpisodes, _ = strconv.Atoi(matches[1]) |
| 980 | 1052 | CurdOut(fmt.Sprintf("Retrieved total episodes: %d", anime.TotalEpisodes)) |
| 981 | 1053 | break |
| @@ -1028,6 +1100,33 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 1028 | 1100 | if userCurdConfig.NextEpisodePrompt && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes && !userCurdConfig.RofiSelection { |
| 1029 | 1101 | // Ensure the terminal state is clean for the prompt |
| 1030 | 1102 | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1103 | episodeNum := anime.Ep.Number |
| 1104 | if !userCurdConfig.RofiSelection { |
| 1105 | episodeNum++ |
| 1106 | } |
| 1107 | |
| 1108 | CurdOut(fmt.Sprintf("Start next episode (%d)?", episodeNum)) |
| 1109 | |
| 1110 | // Create options for the selection |
| 1111 | options := []SelectionOption{ |
| 1112 | {Key: "yes", Label: "Yes, start next episode"}, |
| 1113 | // "no": "No, quit", |
| 1114 | } |
| 1115 | |
| 1116 | // Use DynamicSelect for both CLI and Rofi modes |
| 1117 | selectedOption, err := DynamicSelect(options) |
| 1118 | if err != nil { |
| 1119 | ExitCurd(err) |
| 1120 | } |
| 1121 | |
| 1122 | if selectedOption.Key == "-1" { |
| 1123 | // User selected to quit |
| 1124 | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1125 | ExitMPV(anime.Ep.Player.SocketPath) |
| 1126 | CurdOut("Exiting without starting next episode") |
| 1127 | ExitCurd(nil) |
| 1128 | return |
| 1129 | } |
| 1031 | 1130 | } |
| 1032 | 1131 | } |
| 1033 | 1132 | |
| @@ -1052,12 +1151,24 @@ func WriteTokenToFile(token string, filePath string) error { |
| 1052 | 1151 | |
| 1053 | 1152 | func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1054 | 1153 | |
| 1055 | | if anime.Ep.NextEpisode.Number == anime.Ep.Number { |
| 1154 | // Validate inputs |
| 1155 | if anime.AllanimeId == "" { |
| 1156 | CurdOut("Error: No anime ID found") |
| 1157 | os.Exit(1) |
| 1158 | } |
| 1159 | if anime.Ep.Number <= 0 { |
| 1160 | CurdOut("Error: Invalid episode number") |
| 1161 | os.Exit(1) |
| 1162 | } |
| 1163 | |
| 1164 | if (anime.Ep.NextEpisode.Number == anime.Ep.Number) && (len(anime.Ep.NextEpisode.Links) > 0) { |
| 1056 | 1165 | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1057 | | Log("using prefetched next episode link") |
| 1058 | 1166 | } else { |
| 1059 | 1167 | // Get episode link |
| 1060 | 1168 | link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) |
| 1169 | if len(link) > 0 { |
| 1170 | Log(fmt.Sprintf("Links details: %+v", link)) |
| 1171 | } |
| 1061 | 1172 | if err != nil { |
| 1062 | 1173 | // If unable to get episode link automatically get manually |
| 1063 | 1174 | episodeList, err := EpisodesList(anime.AllanimeId, userCurdConfig.SubOrDub) |
| @@ -1082,7 +1193,8 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1082 | 1193 | CurdOut("Failed to get episode link") |
| 1083 | 1194 | os.Exit(1) |
| 1084 | 1195 | } |
| 1085 | | // anime.Ep.Links = link |
| 1196 | } else { |
| 1197 | Log(fmt.Sprintf("Successfully retrieved episode link on first try. Links count: %d", len(link))) |
| 1086 | 1198 | } |
| 1087 | 1199 | anime.Ep.Links = link |
| 1088 | 1200 | } |
| @@ -1090,10 +1202,10 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1090 | 1202 | if len(anime.Ep.Links) == 0 { |
| 1091 | 1203 | CurdOut("No episode links found") |
| 1092 | 1204 | os.Exit(1) |
| 1205 | } else { |
| 1206 | Log(fmt.Sprintf("Episode links validation passed. Found %d links", len(anime.Ep.Links))) |
| 1093 | 1207 | } |
| 1094 | 1208 | |
| 1095 | | Log(anime) |
| 1096 | | |
| 1097 | 1209 | // Modify the goroutine in main.go where next episode links are fetched |
| 1098 | 1210 | // Get next episode link in parallel |
| 1099 | 1211 | go func() { |
| @@ -1105,14 +1217,15 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1105 | 1217 | } |
| 1106 | 1218 | nextLinks, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, nextEpNum) |
| 1107 | 1219 | if err != nil { |
| 1108 | | Log("Error getting next episode link: " + err.Error()) |
| 1220 | Log(fmt.Sprintf("Error getting next episode link for ep %d: %v", nextEpNum, err)) |
| 1109 | 1221 | } else { |
| 1110 | 1222 | anime.Ep.NextEpisode = NextEpisode{ |
| 1111 | 1223 | Number: nextEpNum, |
| 1112 | 1224 | Links: nextLinks, |
| 1113 | 1225 | } |
| 1114 | | Log(fmt.Sprintf("Next episode link fetched for ep %d", nextEpNum)) |
| 1115 | 1226 | } |
| 1227 | } else { |
| 1228 | Log(fmt.Sprintf("Next episode %d exceeds total episodes %d, skipping prefetch", nextEpNum, anime.TotalEpisodes)) |
| 1116 | 1229 | } |
| 1117 | 1230 | }() |
| 1118 | 1231 | |
| @@ -1213,6 +1326,7 @@ func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 1213 | 1326 | allEntries = append(allEntries, list.Paused...) |
| 1214 | 1327 | allEntries = append(allEntries, list.Dropped...) |
| 1215 | 1328 | allEntries = append(allEntries, list.Planning...) |
| 1329 | allEntries = append(allEntries, list.Rewatching...) |
| 1216 | 1330 | return allEntries |
| 1217 | 1331 | case "CURRENT": |
| 1218 | 1332 | return list.Watching |
| @@ -1224,6 +1338,8 @@ func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 1224 | 1338 | return list.Dropped |
| 1225 | 1339 | case "PLANNING": |
| 1226 | 1340 | return list.Planning |
| 1341 | case "REWATCHING": // Added for completeness, though "ALL" covers it. |
| 1342 | return list.Rewatching |
| 1227 | 1343 | default: |
| 1228 | 1344 | return []Entry{} |
| 1229 | 1345 | } |
| @@ -1231,6 +1347,20 @@ func getEntriesByCategory(list AnimeList, category string) []Entry { |
| 1231 | 1347 | |
| 1232 | 1348 | func NextEpisodePrompt(userCurdConfig *CurdConfig) { |
| 1233 | 1349 | anime := GetGlobalAnime() |
| 1350 | |
| 1351 | // Check if we just completed the last episode |
| 1352 | if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1353 | // Handle scoring and completion for the last episode |
| 1354 | HandleLastEpisodeCompletion(userCurdConfig, anime) |
| 1355 | |
| 1356 | // Exit after completing the series |
| 1357 | fmt.Print("\r\033[K") // Carriage return and clear line |
| 1358 | ExitMPV(anime.Ep.Player.SocketPath) |
| 1359 | CurdOut("Series completed!") |
| 1360 | ExitCurd(nil) |
| 1361 | return |
| 1362 | } |
| 1363 | |
| 1234 | 1364 | // If in CLI mode (not Rofi), we need to show the next episode number |
| 1235 | 1365 | episodeNum := anime.Ep.Number |
| 1236 | 1366 | if !userCurdConfig.RofiSelection { |
| @@ -1240,8 +1370,8 @@ func NextEpisodePrompt(userCurdConfig *CurdConfig) { |
| 1240 | 1370 | CurdOut(fmt.Sprintf("Start next episode (%d)?", episodeNum)) |
| 1241 | 1371 | |
| 1242 | 1372 | // Create options for the selection |
| 1243 | | options := map[string]string{ |
| 1244 | | "yes": "Yes, start next episode", |
| 1373 | options := []SelectionOption{ |
| 1374 | {Key: "yes", Label: "Yes, start next episode"}, |
| 1245 | 1375 | // "no": "No, quit", |
| 1246 | 1376 | } |
| 1247 | 1377 | |
| @@ -1364,6 +1494,35 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1364 | 1494 | // Save previous episode number for progress update |
| 1365 | 1495 | prevEpisode := anime.Ep.Number |
| 1366 | 1496 | |
| 1497 | // Check if we just completed the last episode |
| 1498 | if anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1499 | // Handle scoring and completion for the last episode |
| 1500 | HandleLastEpisodeCompletion(userCurdConfig, anime) |
| 1501 | |
| 1502 | // Update Anilist progress for the last episode if not rewatching |
| 1503 | if !anime.Rewatching { |
| 1504 | user := &User{} |
| 1505 | var err error |
| 1506 | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "token")) |
| 1507 | if err != nil { |
| 1508 | Log("Error reading token for progress update: " + err.Error()) |
| 1509 | } else { |
| 1510 | go func() { |
| 1511 | err = UpdateAnimeProgress(user.Token, anime.AnilistId, prevEpisode) |
| 1512 | if err != nil { |
| 1513 | Log("Error updating Anilist progress: " + err.Error()) |
| 1514 | } else { |
| 1515 | CurdOut(fmt.Sprintf("Anime progress updated! Latest watched episode: %d", prevEpisode)) |
| 1516 | } |
| 1517 | }() |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | CurdOut("Series completed!") |
| 1522 | ExitCurd(nil) |
| 1523 | return |
| 1524 | } |
| 1525 | |
| 1367 | 1526 | // Increment episode number |
| 1368 | 1527 | anime.Ep.Number++ |
| 1369 | 1528 | |
| @@ -1381,7 +1540,7 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1381 | 1540 | CurdOut(fmt.Sprintf("Last episode (%d). Are you sure you want to watch it?", anime.Ep.Number)) |
| 1382 | 1541 | |
| 1383 | 1542 | // Simple yes/no selection for the last episode |
| 1384 | | selectedOption, err := DynamicSelect(map[string]string{"yes": "Yes"}) |
| 1543 | selectedOption, err := DynamicSelect([]SelectionOption{{Key: "yes", Label: "Yes"}}) |
| 1385 | 1544 | if err != nil { |
| 1386 | 1545 | ExitCurd(err) |
| 1387 | 1546 | } |
| @@ -1432,3 +1591,68 @@ func StartNextEpisode(anime *Anime, userCurdConfig *CurdConfig, databaseFile str |
| 1432 | 1591 | // Output message to user |
| 1433 | 1592 | CurdOut(fmt.Sprint("Starting next episode: ", anime.Ep.Number)) |
| 1434 | 1593 | } |
| 1594 | |
| 1595 | // HandleLastEpisodeCompletion handles scoring and completion for the last episode |
| 1596 | func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime) { |
| 1597 | // Check if this is the last episode and scoring is enabled |
| 1598 | if userCurdConfig.ScoreOnCompletion && anime.TotalEpisodes > 0 && anime.Ep.Number == anime.TotalEpisodes { |
| 1599 | // Get user token for scoring |
| 1600 | user := &User{} |
| 1601 | |
| 1602 | // Get config for storage path |
| 1603 | homeDir := "" |
| 1604 | if runtime.GOOS == "windows" { |
| 1605 | homeDir = os.Getenv("USERPROFILE") |
| 1606 | } else { |
| 1607 | homeDir = os.Getenv("HOME") |
| 1608 | } |
| 1609 | configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf") |
| 1610 | config, err := LoadConfig(configFilePath) |
| 1611 | if err != nil { |
| 1612 | Log("Error loading config for scoring: " + err.Error()) |
| 1613 | return |
| 1614 | } |
| 1615 | |
| 1616 | user.Token, err = GetTokenFromFile(filepath.Join(os.ExpandEnv(config.StoragePath), "token")) |
| 1617 | if err != nil { |
| 1618 | Log("Error reading token for scoring: " + err.Error()) |
| 1619 | return |
| 1620 | } |
| 1621 | |
| 1622 | // Prompt user to score the anime |
| 1623 | CurdOut("You've completed this anime! Would you like to rate it?") |
| 1624 | |
| 1625 | scoreOptions := []SelectionOption{ |
| 1626 | {Key: "yes", Label: "Yes, rate this anime"}, |
| 1627 | {Key: "no", Label: "No, skip rating"}, |
| 1628 | } |
| 1629 | |
| 1630 | selectedOption, err := DynamicSelect(scoreOptions) |
| 1631 | if err != nil { |
| 1632 | Log(fmt.Sprintf("Error in score prompt selection: %v", err)) |
| 1633 | return |
| 1634 | } |
| 1635 | |
| 1636 | if selectedOption.Key == "yes" { |
| 1637 | err = RateAnime(user.Token, anime.AnilistId) |
| 1638 | if err != nil { |
| 1639 | Log(fmt.Sprintf("Error rating anime: %v", err)) |
| 1640 | CurdOut("Failed to rate anime") |
| 1641 | } else { |
| 1642 | CurdOut("Anime rated successfully!") |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | // Update anime status to completed on AniList |
| 1647 | if !anime.Rewatching { |
| 1648 | go func() { |
| 1649 | err = UpdateAnimeStatus(user.Token, anime.AnilistId, "COMPLETED") |
| 1650 | if err != nil { |
| 1651 | Log("Error updating anime status to completed: " + err.Error()) |
| 1652 | } else { |
| 1653 | CurdOut("Anime status updated to completed!") |
| 1654 | } |
| 1655 | }() |
| 1656 | } |
| 1657 | } |
| 1658 | } |