Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/anime_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type anime struct {
ID string `json:"_id"`
Name string `json:"name"`
EnglishName string `json:"englishName"`
Thumbnail string `json:"thumbnail"`
AvailableEpisodes interface{} `json:"availableEpisodes"`
}

Expand Down Expand Up @@ -64,6 +65,7 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
_id
name
englishName
thumbnail
availableEpisodes
__typename
}
Expand Down Expand Up @@ -147,8 +149,9 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
}

animeList = append(animeList, SelectionOption{
Key: anime.ID,
Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
Key: anime.ID,
Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
Thumbnail: anime.Thumbnail,
})
}
return animeList, nil
Expand Down
79 changes: 69 additions & 10 deletions internal/curd.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,26 +985,85 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
ExitCurd(fmt.Errorf("No results found."))
}

// find anime in animeList by searching through the options
targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)
// Automatic mapping using Thumbnail clues
// 1. Try AniList and MAL thumbnail matching
found := false
anilistIDStr := strconv.Itoa(anime.AnilistId)
var jikanUrls []string
fetchedJikan := false

// Helper regex
// AniList regex extracts ID from strings like "bx155348-" or "/155348.jpg"
anilistRegex := regexp.MustCompile(`anilistcdn/media/anime/cover/(?:large|medium)/(?:bx)?(\d+)`)
// MyAnimeList regex extracts the filename like "120128.jpg"
malRegex := regexp.MustCompile(`myanimelist\.net/images/anime/[^/]+/([^/]+\.jpg)`)

for i, option := range animeList {
Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s'", i, option.Key, option.Label))
if option.Label == targetLabel {
anime.AllanimeId = option.Key
Log(fmt.Sprintf("Found exact match! Setting AllanimeId to: %s", anime.AllanimeId))
found = true
break
Log(fmt.Sprintf("Checking option %d: Key='%s', Label='%s', Thumbnail='%s'", i, option.Key, option.Label, option.Thumbnail))

if strings.Contains(option.Thumbnail, "anilist.co") {
matches := anilistRegex.FindStringSubmatch(option.Thumbnail)
if len(matches) > 1 && matches[1] == anilistIDStr {
anime.AllanimeId = option.Key
Log(fmt.Sprintf("Found Anilist Thumbnail match! Setting AllanimeId to: %s", anime.AllanimeId))
found = true
break
}
} else if strings.Contains(option.Thumbnail, "myanimelist.net") {
matches := malRegex.FindStringSubmatch(option.Thumbnail)
if len(matches) > 1 {
fileName := matches[1]

// Fetch Jikan pictures lazily (only once)
if !fetchedJikan {
if anime.MalId == 0 {
anime.MalId, _ = GetAnimeMalID(anime.AnilistId)
}
if anime.MalId != 0 {
urls, err := FetchJikanPictures(anime.MalId)
if err != nil {
Log(fmt.Sprintf("Failed to fetch Jikan pictures: %v", err))
} else {
jikanUrls = urls
}
}
fetchedJikan = true
}

for _, url := range jikanUrls {
if strings.HasSuffix(url, "/"+fileName) || strings.Contains(url, fileName) {
anime.AllanimeId = option.Key
Log(fmt.Sprintf("Found MyAnimeList Thumbnail match (%s)! Setting AllanimeId to: %s", fileName, anime.AllanimeId))
found = true
break
}
}
}
if found {
break
}
}
}

// 2. Fallback to naive title/episode match
if !found {
Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel))
targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)
for _, option := range animeList {
if option.Label == targetLabel {
anime.AllanimeId = option.Key
Log(fmt.Sprintf("Found exact text match! Setting AllanimeId to: %s", anime.AllanimeId))
found = true
break
}
}
if !found {
Log(fmt.Sprintf("No exact match found for label '%s'. Will require manual selection.", targetLabel))
}
}

// If unable to get Allanime id automatically get manually
if anime.AllanimeId == "" {
CurdOut("Failed to automatically select anime")
CurdOut("We didn't find any matches. Please select manually.")
selectedAllanimeAnime, err := DynamicSelect(animeList)

if selectedAllanimeAnime.Key == "-1" {
Expand Down
37 changes: 37 additions & 0 deletions internal/jikan.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,40 @@ func makeGetRequest(url string, headers map[string]string) (map[string]interface

return responseData, nil
}

// FetchJikanPictures fetches the pictures for an anime using the Jikan API.
// It returns a list of all raw image URLs.
func FetchJikanPictures(malID int) ([]string, error) {
url := fmt.Sprintf("https://api.jikan.moe/v4/anime/%d/pictures", malID)

response, err := makeGetRequest(url, nil)
if err != nil {
return nil, fmt.Errorf("Jikan API request failed: %v", err)
}

dataList, ok := response["data"].([]interface{})
if !ok {
return nil, fmt.Errorf("invalid Jikan API response format")
}

var urls []string
for _, item := range dataList {
if mapItem, ok := item.(map[string]interface{}); ok {
for _, format := range []string{"jpg", "webp"} {
if formatData, ok := mapItem[format].(map[string]interface{}); ok {
if imgURL, ok := formatData["image_url"].(string); ok && imgURL != "" {
urls = append(urls, imgURL)
}
if smallURL, ok := formatData["small_image_url"].(string); ok && smallURL != "" {
urls = append(urls, smallURL)
}
if largeURL, ok := formatData["large_image_url"].(string); ok && largeURL != "" {
urls = append(urls, largeURL)
}
}
}
}
}

return urls, nil
}
2 changes: 1 addition & 1 deletion internal/selection_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
if m.filteredKeys[m.selected].Key == "add_new" {
CurdOut("Adding a new anime...")
m.filteredKeys[m.selected] = SelectionOption{"add_new", "0"}
m.filteredKeys[m.selected] = SelectionOption{Label: "add_new", Key: "0"}
return m, tea.Quit
}
return m, tea.Quit
Expand Down
5 changes: 3 additions & 2 deletions internal/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type SelectionOptionImage struct {

// SelectionOption holds the label and the internal key
type SelectionOption struct {
Label string
Key string
Label string
Key string
Thumbnail string
}
Loading