Commit b08c42f0d4

b08c42f0d4684fc182d76ada24a6f9e37b71b7f5

parent: adcafea6e3

Unregistered key

Rushikesh Gaikwad <81632222+Wraient@users.noreply.github.com> · 2026-03-31 15:19 UTC
committer: <noreply@github.com>

Feat: Add thumbnail support for anime selection and improve matching logic (#83)

Layout: unified · split

internal/anime_list.go +5 −2
@@ -15,6 +15,7 @@ type anime struct {
15 ID string `json:"_id"` 15 ID string `json:"_id"`
16 Name string `json:"name"` 16 Name string `json:"name"`
17 EnglishName string `json:"englishName"` 17 EnglishName string `json:"englishName"`
18 Thumbnail string `json:"thumbnail"`
18 AvailableEpisodes interface{} `json:"availableEpisodes"` 19 AvailableEpisodes interface{} `json:"availableEpisodes"`
19} 20}
20 21
@@ -64,6 +65,7 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
64 _id 65 _id
65 name 66 name
66 englishName 67 englishName
68 thumbnail
67 availableEpisodes 69 availableEpisodes
68 __typename 70 __typename
69 } 71 }
@@ -147,8 +149,9 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
147 } 149 }
148 150
149 animeList = append(animeList, SelectionOption{ 151 animeList = append(animeList, SelectionOption{
150 Key: anime.ID, 152 Key: anime.ID,
151 Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr), 153 Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
154 Thumbnail: anime.Thumbnail,
152 }) 155 })
153 } 156 }
154 return animeList, nil 157 return animeList, nil
internal/curd.go +69 −10
@@ -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" {
internal/jikan.go +37
@@ -104,3 +104,40 @@ func makeGetRequest(url string, headers map[string]string) (map[string]interface
104 104
105 return responseData, nil 105 return responseData, nil
106} 106}
107
108// FetchJikanPictures fetches the pictures for an anime using the Jikan API.
109// It returns a list of all raw image URLs.
110func FetchJikanPictures(malID int) ([]string, error) {
111 url := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/pictures", malID)
112
113 response, err := makeGetRequest(url, nil)
114 if err != nil {
115 return nil, fmt.Errorf("Jikan API request failed: %v", err)
116 }
117
118 dataList, ok := response["data"].([]interface{})
119 if !ok {
120 return nil, fmt.Errorf("invalid Jikan API response format")
121 }
122
123 var urls []string
124 for _, item := range dataList {
125 if mapItem, ok := item.(map[string]interface{}); ok {
126 for _, format := range []string{"jpg", "webp"} {
127 if formatData, ok := mapItem[format].(map[string]interface{}); ok {
128 if imgURL, ok := formatData["image_url"].(string); ok && imgURL != "" {
129 urls = append(urls, imgURL)
130 }
131 if smallURL, ok := formatData["small_image_url"].(string); ok && smallURL != "" {
132 urls = append(urls, smallURL)
133 }
134 if largeURL, ok := formatData["large_image_url"].(string); ok && largeURL != "" {
135 urls = append(urls, largeURL)
136 }
137 }
138 }
139 }
140 }
141
142 return urls, nil
143}
internal/selection_menu.go +1 −1
@@ -124,7 +124,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
124 case "enter": 124 case "enter":
125 if m.filteredKeys[m.selected].Key == "add_new" { 125 if m.filteredKeys[m.selected].Key == "add_new" {
126 CurdOut("Adding a new anime...") 126 CurdOut("Adding a new anime...")
127 m.filteredKeys[m.selected] = SelectionOption{"add_new", "0"} 127 m.filteredKeys[m.selected] = SelectionOption{Label: "add_new", Key: "0"}
128 return m, tea.Quit 128 return m, tea.Quit
129 } 129 }
130 return m, tea.Quit 130 return m, tea.Quit
internal/structs.go +3 −2
@@ -128,6 +128,7 @@ type SelectionOptionImage struct {
128 128
129// SelectionOption holds the label and the internal key 129// SelectionOption holds the label and the internal key
130type SelectionOption struct { 130type SelectionOption struct {
131 Label string 131 Label string
132 Key string 132 Key string
133 Thumbnail string
133} 134}