| @@ -985,26 +985,85 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni |
| 985 | ExitCurd(fmt.Errorf("No results found.")) |
985 | ExitCurd(fmt.Errorf("No results found.")) |
| 986 | } |
986 | } |
| 987 | |
987 | |
| 988 | // find anime in animeList by searching through the options |
988 | // Automatic mapping using Thumbnail clues |
| 989 | targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes) |
989 | // 1. Try AniList and MAL thumbnail matching |
| 990 | found := false |
990 | found := false |
| |
991 | anilistIDStr := strconv.Itoa(anime.AnilistId) |
| |
992 | var jikanUrls []string |
| |
993 | fetchedJikan := false |
| |
994 | |
| |
995 | // Helper regex |
| |
996 | // AniList regex extracts ID from strings like "bx155348-" or "/155348.jpg" |
| |
997 | anilistRegex := regexp.MustCompile(`anilistcdn/media/anime/cover/(?:large|medium)/(?:bx)?(\d+)`) |
| |
998 | // MyAnimeList regex extracts the filename like "120128.jpg" |
| |
999 | malRegex := regexp.MustCompile(`myanimelist\.net/images/anime/[^/]+/([^/]+\.jpg)`) |
| |
1000 | |
| 991 | for i, option := range animeList { |
1001 | for i, option := range animeList { |
| 992 | Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s'", i, option.Key, option.Label)) |
1002 | Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s', Thumbnail='%s'", i, option.Key, option.Label, option.Thumbnail)) |
| 993 | if option.Label == targetLabel { |
1003 | |
| 994 | anime.AllanimeId = option.Key |
1004 | if strings.Contains(option.Thumbnail, "anilist.co") { |
| 995 | Log(fmt.Sprintf("Found exact match! Setting AllanimeId to: %s", anime.AllanimeId)) |
1005 | matches := anilistRegex.FindStringSubmatch(option.Thumbnail) |
| 996 | found = true |
1006 | if len(matches) > 1 && matches[1] == anilistIDStr { |
| 997 | break |
1007 | anime.AllanimeId = option.Key |
| |
1008 | Log(fmt.Sprintf("Found Anilist Thumbnail match! Setting AllanimeId to: %s", anime.AllanimeId)) |
| |
1009 | found = true |
| |
1010 | break |
| |
1011 | } |
| |
1012 | } else if strings.Contains(option.Thumbnail, "myanimelist.net") { |
| |
1013 | matches := malRegex.FindStringSubmatch(option.Thumbnail) |
| |
1014 | if len(matches) > 1 { |
| |
1015 | fileName := matches[1] |
| |
1016 | |
| |
1017 | // Fetch Jikan pictures lazily (only once) |
| |
1018 | if !fetchedJikan { |
| |
1019 | if anime.MalId == 0 { |
| |
1020 | anime.MalId, _ = GetAnimeMalID(anime.AnilistId) |
| |
1021 | } |
| |
1022 | if anime.MalId != 0 { |
| |
1023 | urls, err := FetchJikanPictures(anime.MalId) |
| |
1024 | if err != nil { |
| |
1025 | Log(fmt.Sprintf("Failed to fetch Jikan pictures: %v", err)) |
| |
1026 | } else { |
| |
1027 | jikanUrls = urls |
| |
1028 | } |
| |
1029 | } |
| |
1030 | fetchedJikan = true |
| |
1031 | } |
| |
1032 | |
| |
1033 | for _, url := range jikanUrls { |
| |
1034 | if strings.HasSuffix(url, "/"+fileName) || strings.Contains(url, fileName) { |
| |
1035 | anime.AllanimeId = option.Key |
| |
1036 | Log(fmt.Sprintf("Found MyAnimeList Thumbnail match (%s)! Setting AllanimeId to: %s", fileName, anime.AllanimeId)) |
| |
1037 | found = true |
| |
1038 | break |
| |
1039 | } |
| |
1040 | } |
| |
1041 | } |
| |
1042 | if found { |
| |
1043 | break |
| |
1044 | } |
| 998 | } |
1045 | } |
| 999 | } |
1046 | } |
| 1000 | |
1047 | |
| |
1048 | // 2. Fallback to naive title/episode match |
| 1001 | if !found { |
1049 | if !found { |
| 1002 | Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel)) |
1050 | targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes) |
| |
1051 | for _, option := range animeList { |
| |
1052 | if option.Label == targetLabel { |
| |
1053 | anime.AllanimeId = option.Key |
| |
1054 | Log(fmt.Sprintf("Found exact text match! Setting AllanimeId to: %s", anime.AllanimeId)) |
| |
1055 | found = true |
| |
1056 | break |
| |
1057 | } |
| |
1058 | } |
| |
1059 | if !found { |
| |
1060 | Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel)) |
| |
1061 | } |
| 1003 | } |
1062 | } |
| 1004 | |
1063 | |
| 1005 | // If unable to get Allanime id automatically get manually |
1064 | // If unable to get Allanime id automatically get manually |
| 1006 | if anime.AllanimeId == "" { |
1065 | if anime.AllanimeId == "" { |
| 1007 | CurdOut("Failed to automatically select anime") |
1066 | CurdOut("We didn't find any matches. Please select manually.") |
| 1008 | selectedAllanimeAnime, err := DynamicSelect(animeList) |
1067 | selectedAllanimeAnime, err := DynamicSelect(animeList) |
| 1009 | |
1068 | |
| 1010 | if selectedAllanimeAnime.Key == "-1" { |
1069 | if selectedAllanimeAnime.Key == "-1" { |