Commit f3686059ea
Unsigned
Layout: unified · split
internal/curd.go +76 −15
| @@ -298,6 +298,16 @@ 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 | 312 | animeListOptions = make([]SelectionOption, 0) |
| 303 | 313 | // Include anime from all categories |
| @@ -351,6 +361,16 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { | ||
| 351 | 361 | Label: title, |
| 352 | 362 | }) |
| 353 | 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 | }) | |
| 373 | } | |
| 354 | 374 | } |
| 355 | 375 | |
| 356 | 376 | // Select anime to update |
| @@ -395,7 +415,13 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) { | ||
| 395 | 415 | |
| 396 | 416 | currentStatus := "None" |
| 397 | 417 | if selectedAnilistAnime.Status != "" { |
| 398 | 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 | } | |
| 399 | 425 | } |
| 400 | 426 | CurdOut(fmt.Sprintf("Current category: %s", currentStatus)) |
| 401 | 427 | |
| @@ -585,7 +611,19 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA | ||
| 585 | 611 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 586 | 612 | animeMapPreview, err = SearchAnimeAnilistPreview(query, user.Token) |
| 587 | 613 | } else { |
| 588 | animeMap, err = SearchAnimeAnilist(query, user.Token) | |
| 614 | animeMap, err := SearchAnimeAnilist(query, user.Token) | |
| 615 | if err != nil { | |
| 616 | Log(fmt.Sprintf("Failed to search anime: %v", err)) | |
| 617 | ExitCurd(fmt.Errorf("Failed to search anime")) | |
| 618 | } | |
| 619 | // Convert map to []SelectionOption | |
| 620 | animeOptions = make([]SelectionOption, 0, len(animeMap)) | |
| 621 | for key, label := range animeMap { | |
| 622 | animeOptions = append(animeOptions, SelectionOption{ | |
| 623 | Key: key, | |
| 624 | Label: label, | |
| 625 | }) | |
| 626 | } | |
| 589 | 627 | } |
| 590 | 628 | if err != nil { |
| 591 | 629 | Log(fmt.Sprintf("Failed to search anime: %v", err)) |
| @@ -870,7 +908,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | ||
| 870 | 908 | Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub)) |
| 871 | 909 | |
| 872 | 910 | animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub) |
| 873 | Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err)) | |
| 874 | 911 | if err != nil { |
| 875 | 912 | Log(fmt.Sprintf("Failed to select anime: %v", err)) |
| 876 | 913 | ExitCurd(fmt.Errorf("Failed to select anime")) |
| @@ -879,10 +916,21 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | ||
| 879 | 916 | ExitCurd(fmt.Errorf("No results found.")) |
| 880 | 917 | } |
| 881 | 918 | |
| 882 | // find anime in animeList | |
| 883 | anime.AllanimeId, err = FindKeyByValue(animeList, fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)) | |
| 884 | if err != nil { | |
| 885 | Log(fmt.Sprintf("Failed to find anime in animeList: %v", err)) | |
| 919 | // find anime in animeList by searching through the options | |
| 920 | targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes) | |
| 921 | found := false | |
| 922 | for i, option := range animeList { | |
| 923 | Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s'", i, option.Key, option.Label)) | |
| 924 | if option.Label == targetLabel { | |
| 925 | anime.AllanimeId = option.Key | |
| 926 | Log(fmt.Sprintf("Found exact match! Setting AllanimeId to: %s", anime.AllanimeId)) | |
| 927 | found = true | |
| 928 | break | |
| 929 | } | |
| 930 | } | |
| 931 | ||
| 932 | if !found { | |
| 933 | Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel)) | |
| 886 | 934 | } |
| 887 | 935 | |
| 888 | 936 | // If unable to get Allanime id automatically get manually |
| @@ -895,7 +943,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni | ||
| 895 | 943 | } |
| 896 | 944 | |
| 897 | 945 | if err != nil { |
| 898 | // fmt.Println("No anime available") | |
| 899 | 946 | ExitCurd(fmt.Errorf("No anime available")) |
| 900 | 947 | } |
| 901 | 948 | anime.AllanimeId = selectedAllanimeAnime.Key |
| @@ -1101,12 +1148,24 @@ func WriteTokenToFile(token string, filePath string) error { | ||
| 1101 | 1148 | |
| 1102 | 1149 | func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { |
| 1103 | 1150 | |
| 1104 | if anime.Ep.NextEpisode.Number == anime.Ep.Number { | |
| 1151 | // Validate inputs | |
| 1152 | if anime.AllanimeId == "" { | |
| 1153 | CurdOut("Error: No anime ID found") | |
| 1154 | os.Exit(1) | |
| 1155 | } | |
| 1156 | if anime.Ep.Number <= 0 { | |
| 1157 | CurdOut("Error: Invalid episode number") | |
| 1158 | os.Exit(1) | |
| 1159 | } | |
| 1160 | ||
| 1161 | if (anime.Ep.NextEpisode.Number == anime.Ep.Number) && (len(anime.Ep.NextEpisode.Links) > 0) { | |
| 1105 | 1162 | anime.Ep.Links = anime.Ep.NextEpisode.Links |
| 1106 | Log("using prefetched next episode link") | |
| 1107 | 1163 | } else { |
| 1108 | 1164 | // Get episode link |
| 1109 | 1165 | link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number) |
| 1166 | if len(link) > 0 { | |
| 1167 | Log(fmt.Sprintf("Links details: %+v", link)) | |
| 1168 | } | |
| 1110 | 1169 | if err != nil { |
| 1111 | 1170 | // If unable to get episode link automatically get manually |
| 1112 | 1171 | episodeList, err := EpisodesList(anime.AllanimeId, userCurdConfig.SubOrDub) |
| @@ -1131,7 +1190,8 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1131 | 1190 | CurdOut("Failed to get episode link") |
| 1132 | 1191 | os.Exit(1) |
| 1133 | 1192 | } |
| 1134 | // anime.Ep.Links = link | |
| 1193 | } else { | |
| 1194 | Log(fmt.Sprintf("Successfully retrieved episode link on first try. Links count: %d", len(link))) | |
| 1135 | 1195 | } |
| 1136 | 1196 | anime.Ep.Links = link |
| 1137 | 1197 | } |
| @@ -1139,10 +1199,10 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1139 | 1199 | if len(anime.Ep.Links) == 0 { |
| 1140 | 1200 | CurdOut("No episode links found") |
| 1141 | 1201 | os.Exit(1) |
| 1202 | } else { | |
| 1203 | Log(fmt.Sprintf("Episode links validation passed. Found %d links", len(anime.Ep.Links))) | |
| 1142 | 1204 | } |
| 1143 | 1205 | |
| 1144 | Log(anime) | |
| 1145 | ||
| 1146 | 1206 | // Modify the goroutine in main.go where next episode links are fetched |
| 1147 | 1207 | // Get next episode link in parallel |
| 1148 | 1208 | go func() { |
| @@ -1154,14 +1214,15 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1154 | 1214 | } |
| 1155 | 1215 | nextLinks, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, nextEpNum) |
| 1156 | 1216 | if err != nil { |
| 1157 | Log("Error getting next episode link: " + err.Error()) | |
| 1217 | Log(fmt.Sprintf("Error getting next episode link for ep %d: %v", nextEpNum, err)) | |
| 1158 | 1218 | } else { |
| 1159 | 1219 | anime.Ep.NextEpisode = NextEpisode{ |
| 1160 | 1220 | Number: nextEpNum, |
| 1161 | 1221 | Links: nextLinks, |
| 1162 | 1222 | } |
| 1163 | Log(fmt.Sprintf("Next episode link fetched for ep %d", nextEpNum)) | |
| 1164 | 1223 | } |
| 1224 | } else { | |
| 1225 | Log(fmt.Sprintf("Next episode %d exceeds total episodes %d, skipping prefetch", nextEpNum, anime.TotalEpisodes)) | |
| 1165 | 1226 | } |
| 1166 | 1227 | }() |
| 1167 | 1228 | |