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 {
1515 ID string `json:"_id"`
1616 Name string `json:"name"`
1717 EnglishName string `json:"englishName"`
18 Thumbnail string `json:"thumbnail"`
1819 AvailableEpisodes interface{} `json:"availableEpisodes"`
1920}
2021
@@ -64,6 +65,7 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
6465 _id
6566 name
6667 englishName
68 thumbnail
6769 availableEpisodes
6870 __typename
6971 }
@@ -147,8 +149,9 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
147149 }
148150
149151 animeList = append(animeList, SelectionOption{
150 Key: anime.ID,
151 Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
152 Key: anime.ID,
153 Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
154 Thumbnail: anime.Thumbnail,
152155 })
153156 }
154157 return animeList, nil
internal/curd.go +69 −10
@@ -985,26 +985,85 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
985985 ExitCurd(fmt.Errorf("No results found."))
986986 }
987987
988 // find anime in animeList by searching through the options
989 targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)
988 // Automatic mapping using Thumbnail clues
989 // 1. Try AniList and MAL thumbnail matching
990990 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
9911001 for i, option := range animeList {
992 Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s'", i, option.Key, option.Label))
993 if option.Label == targetLabel {
994 anime.AllanimeId = option.Key
995 Log(fmt.Sprintf("Found exact match! Setting AllanimeId to: %s", anime.AllanimeId))
996 found = true
997 break
1002 Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s', Thumbnail='%s'", i, option.Key, option.Label, option.Thumbnail))
1003
1004 if strings.Contains(option.Thumbnail, "anilist.co") {
1005 matches := anilistRegex.FindStringSubmatch(option.Thumbnail)
1006 if len(matches) > 1 && matches[1] == anilistIDStr {
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 }
9981045 }
9991046 }
10001047
1048 // 2. Fallback to naive title/episode match
10011049 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 }
10031062 }
10041063
10051064 // If unable to get Allanime id automatically get manually
10061065 if anime.AllanimeId == "" {
1007 CurdOut("Failed to automatically select anime")
1066 CurdOut("We didn't find any matches. Please select manually.")
10081067 selectedAllanimeAnime, err := DynamicSelect(animeList)
10091068
10101069 if selectedAllanimeAnime.Key == "-1" {
internal/jikan.go +37
@@ -104,3 +104,40 @@ func makeGetRequest(url string, headers map[string]string) (map[string]interface
104104
105105 return responseData, nil
106106}
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) {
124124 case "enter":
125125 if m.filteredKeys[m.selected].Key == "add_new" {
126126 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"}
128128 return m, tea.Quit
129129 }
130130 return m, tea.Quit
internal/structs.go +3 −2
@@ -128,6 +128,7 @@ type SelectionOptionImage struct {
128128
129129// SelectionOption holds the label and the internal key
130130type SelectionOption struct {
131 Label string
132 Key string
131 Label string
132 Key string
133 Thumbnail string
133134}