Commit f3686059ea

f3686059ea008ec62a7886024af57aafdbb299ce

parent: dab28c5413

Unsigned

Wraient <wraient.911@gmail.com> · 2025-07-23 18:18 UTC

Add: Logging and new implementation for selectionOption[]

Layout: unified · split

internal/curd.go +76 −15
@@ -298,6 +298,16 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
298298 CoverImage: entry.CoverImage,
299299 }
300300 }
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 }
301311 } else {
302312 animeListOptions = make([]SelectionOption, 0)
303313 // Include anime from all categories
@@ -351,6 +361,16 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
351361 Label: title,
352362 })
353363 }
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 }
354374 }
355375
356376 // Select anime to update
@@ -395,7 +415,13 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
395415
396416 currentStatus := "None"
397417 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 }
399425 }
400426 CurdOut(fmt.Sprintf("Current category: %s", currentStatus))
401427
@@ -585,7 +611,19 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA
585611 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
586612 animeMapPreview, err = SearchAnimeAnilistPreview(query, user.Token)
587613 } 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 }
589627 }
590628 if err != nil {
591629 Log(fmt.Sprintf("Failed to search anime: %v", err))
@@ -870,7 +908,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
870908 Log(fmt.Sprintf("Searching for anime with query: %s, SubOrDub: %s", userQuery, userCurdConfig.SubOrDub))
871909
872910 animeList, err = SearchAnime(string(userQuery), userCurdConfig.SubOrDub)
873 Log(fmt.Sprintf("SearchAnime result - animeList: %+v, err: %v", animeList, err))
874911 if err != nil {
875912 Log(fmt.Sprintf("Failed to select anime: %v", err))
876913 ExitCurd(fmt.Errorf("Failed to select anime"))
@@ -879,10 +916,21 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
879916 ExitCurd(fmt.Errorf("No results found."))
880917 }
881918
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))
886934 }
887935
888936 // If unable to get Allanime id automatically get manually
@@ -895,7 +943,6 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
895943 }
896944
897945 if err != nil {
898 // fmt.Println("No anime available")
899946 ExitCurd(fmt.Errorf("No anime available"))
900947 }
901948 anime.AllanimeId = selectedAllanimeAnime.Key
@@ -1101,12 +1148,24 @@ func WriteTokenToFile(token string, filePath string) error {
11011148
11021149func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string {
11031150
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) {
11051162 anime.Ep.Links = anime.Ep.NextEpisode.Links
1106 Log("using prefetched next episode link")
11071163 } else {
11081164 // Get episode link
11091165 link, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, anime.Ep.Number)
1166 if len(link) > 0 {
1167 Log(fmt.Sprintf("Links details: %+v", link))
1168 }
11101169 if err != nil {
11111170 // If unable to get episode link automatically get manually
11121171 episodeList, err := EpisodesList(anime.AllanimeId, userCurdConfig.SubOrDub)
@@ -1131,7 +1190,8 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string {
11311190 CurdOut("Failed to get episode link")
11321191 os.Exit(1)
11331192 }
1134 // anime.Ep.Links = link
1193 } else {
1194 Log(fmt.Sprintf("Successfully retrieved episode link on first try. Links count: %d", len(link)))
11351195 }
11361196 anime.Ep.Links = link
11371197 }
@@ -1139,10 +1199,10 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string {
11391199 if len(anime.Ep.Links) == 0 {
11401200 CurdOut("No episode links found")
11411201 os.Exit(1)
1202 } else {
1203 Log(fmt.Sprintf("Episode links validation passed. Found %d links", len(anime.Ep.Links)))
11421204 }
11431205
1144 Log(anime)
1145
11461206 // Modify the goroutine in main.go where next episode links are fetched
11471207 // Get next episode link in parallel
11481208 go func() {
@@ -1154,14 +1214,15 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string {
11541214 }
11551215 nextLinks, err := GetEpisodeURL(*userCurdConfig, anime.AllanimeId, nextEpNum)
11561216 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))
11581218 } else {
11591219 anime.Ep.NextEpisode = NextEpisode{
11601220 Number: nextEpNum,
11611221 Links: nextLinks,
11621222 }
1163 Log(fmt.Sprintf("Next episode link fetched for ep %d", nextEpNum))
11641223 }
1224 } else {
1225 Log(fmt.Sprintf("Next episode %d exceeds total episodes %d, skipping prefetch", nextEpNum, anime.TotalEpisodes))
11651226 }
11661227 }()
11671228