Commit 3bfcdea64b

3bfcdea64b8acddbdbf1a787f92d060ff219dff3

parent: f28ee237f3

Unregistered key

Rushikesh Gaikwad <81632222+Wraient@users.noreply.github.com> · 2026-04-01 01:20 UTC
committer: <noreply@github.com>

Add: Search anime in multiple languages (#88)

Layout: unified · split

Build/curd-windows-build.iss +1 −1
@@ -1,6 +1,6 @@
11[Setup]
22AppName=Curd Installer
3AppVersion=1.0.8
3AppVersion=1.2.0
44DefaultDirName={userappdata}\Curd
55PrivilegesRequired=lowest
66AllowNoIcons=yes
internal/anime_list.go +66 −7
@@ -8,7 +8,7 @@ import (
88 "net/url"
99 "os"
1010 "path/filepath"
11 // "strings"
11 "strings"
1212)
1313
1414type anime struct {
@@ -27,6 +27,20 @@ type response struct {
2727 } `json:"data"`
2828}
2929
30func normalizeTranslationType(mode string) string {
31 if strings.EqualFold(strings.TrimSpace(mode), "dub") {
32 return "dub"
33 }
34 return "sub"
35}
36
37func alternateTranslationType(mode string) string {
38 if normalizeTranslationType(mode) == "dub" {
39 return "sub"
40 }
41 return "dub"
42}
43
3044// func main() {
3145// // Get environment variables
3246// mode := "sub"
@@ -43,6 +57,42 @@ type response struct {
4357// }
4458
4559func SearchAnime(query, mode string) ([]SelectionOption, error) {
60 preferredMode := normalizeTranslationType(mode)
61 alternateMode := alternateTranslationType(preferredMode)
62
63 preferredResults, preferredErr := searchAnimeByMode(query, preferredMode, preferredMode)
64 alternateResults, alternateErr := searchAnimeByMode(query, alternateMode, preferredMode)
65
66 if preferredErr != nil {
67 Log(fmt.Sprintf("Failed searching %s results for %q: %v", preferredMode, query, preferredErr))
68 }
69 if alternateErr != nil {
70 Log(fmt.Sprintf("Failed searching %s results for %q: %v", alternateMode, query, alternateErr))
71 }
72
73 if preferredErr != nil && alternateErr != nil {
74 return nil, preferredErr
75 }
76
77 animeList := make([]SelectionOption, 0, len(preferredResults)+len(alternateResults))
78 seen := make(map[string]struct{}, len(preferredResults)+len(alternateResults))
79
80 for _, option := range preferredResults {
81 animeList = append(animeList, option)
82 seen[option.Key] = struct{}{}
83 }
84
85 for _, option := range alternateResults {
86 if _, exists := seen[option.Key]; exists {
87 continue
88 }
89 animeList = append(animeList, option)
90 }
91
92 return animeList, nil
93}
94
95func searchAnimeByMode(query, mode, preferredMode string) ([]SelectionOption, error) {
4696 userCurdConfig := GetGlobalConfig()
4797 if userCurdConfig == nil {
4898 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
@@ -56,7 +106,9 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
56106 allanimeAPI = "https://api." + allanimeBase + "/api"
57107 )
58108
59 // Prepare the anime list
109 mode = normalizeTranslationType(mode)
110 preferredMode = normalizeTranslationType(preferredMode)
111
60112 animeList := make([]SelectionOption, 0)
61113
62114 searchGql := `query($search: SearchInput, $limit: Int, $page: Int, $translationType: VaildTranslationTypeEnumType, $countryOrigin: VaildCountryOriginEnumType) {
@@ -134,23 +186,30 @@ func SearchAnime(query, mode string) ([]SelectionOption, error) {
134186 for _, anime := range response.Data.Shows.Edges {
135187 var episodesStr string
136188 if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok {
137 if subEpisodes, ok := episodes["sub"].(float64); ok {
138 episodesStr = fmt.Sprintf("%d", int(subEpisodes))
189 if modeEpisodes, ok := episodes[mode].(float64); ok {
190 episodesStr = fmt.Sprintf("%d", int(modeEpisodes))
139191 } else {
140 Log(subEpisodes)
141192 episodesStr = "Unknown"
142193 }
194 } else {
195 episodesStr = "Unknown"
143196 }
144197
145198 // Use English name if available and configured, otherwise use default name
146199 displayName := anime.Name
147 if anime.EnglishName != "" && userCurdConfig.AnimeNameLanguage == "english" {
200 if anime.EnglishName != "" && userCurdConfig != nil && userCurdConfig.AnimeNameLanguage == "english" {
148201 displayName = anime.EnglishName
149202 }
150203
204 label := fmt.Sprintf("%s (%s episodes)", displayName, episodesStr)
205 if mode != preferredMode {
206 label = fmt.Sprintf("%s [%s]", label, mode)
207 }
208
151209 animeList = append(animeList, SelectionOption{
210 Title: displayName,
152211 Key: anime.ID,
153 Label: fmt.Sprintf("%s (%s episodes)", displayName, episodesStr),
212 Label: label,
154213 Thumbnail: anime.Thumbnail,
155214 })
156215 }
internal/curd.go +1 −1
@@ -1089,7 +1089,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
10891089 if !found {
10901090 targetLabel := fmt.Sprintf("%v (%d episodes)", userQuery, selectedAnilistAnime.Media.Episodes)
10911091 for _, option := range animeList {
1092 if option.Label == targetLabel {
1092 if fmt.Sprintf("%s (%d episodes)", option.Title, selectedAnilistAnime.Media.Episodes) == targetLabel {
10931093 anime.AllanimeId = option.Key
10941094 Log(fmt.Sprintf("Found exact text match! Setting AllanimeId to: %s", anime.AllanimeId))
10951095 found = true
internal/episode_list.go +12 −1
@@ -32,6 +32,7 @@ type episodesResponse struct {
3232
3333// episodesList performs the API call and fetches the episodes list
3434func EpisodesList(showID, mode string) ([]string, error) {
35 preferredMode := normalizeTranslationType(mode)
3536 const (
3637 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
3738 allanimeRef = "https://allanime.to"
@@ -77,7 +78,17 @@ func EpisodesList(showID, mode string) ([]string, error) {
7778 }
7879
7980 // Extract and sort the episodes
80 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, mode)
81 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode)
82 if len(episodes) == 0 {
83 fallbackMode := alternateTranslationType(preferredMode)
84 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, fallbackMode)
85 if len(episodes) > 0 {
86 Log(fmt.Sprintf("Falling back to %s episode list for anime %s", fallbackMode, showID))
87 }
88 }
89 if len(episodes) == 0 {
90 return episodes, fmt.Errorf("no episodes found for anime %s", showID)
91 }
8192 return episodes, nil
8293}
8394
internal/episode_url.go +18 −1
@@ -151,11 +151,28 @@ func extractLinks(provider_id string) map[string]interface{} {
151151// - []string: a list of links for specified episode.
152152// - error: an error if the episode is not found or if there is an issue during the search.
153153func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
154 preferredMode := normalizeTranslationType(config.SubOrDub)
155 links, err := getEpisodeURLForMode(id, preferredMode, epNo)
156 if err == nil {
157 return links, nil
158 }
159
160 fallbackMode := alternateTranslationType(preferredMode)
161 fallbackLinks, fallbackErr := getEpisodeURLForMode(id, fallbackMode, epNo)
162 if fallbackErr == nil {
163 Log(fmt.Sprintf("Falling back to %s for anime %s episode %d", fallbackMode, id, epNo))
164 return fallbackLinks, nil
165 }
166
167 return nil, err
168}
169
170func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
154171 query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}`
155172
156173 variables := map[string]string{
157174 "showId": id,
158 "translationType": config.SubOrDub,
175 "translationType": normalizeTranslationType(mode),
159176 "episodeString": fmt.Sprintf("%d", epNo),
160177 }
161178
internal/localTracking.go +7 −2
@@ -297,7 +297,13 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
297297 }
298298
299299 anime.AllanimeId = selectedAnime.Key
300 anime.Title.English = selectedAnime.Label
300 if selectedAnime.Title != "" {
301 anime.Title.English = selectedAnime.Title
302 anime.Title.Romaji = selectedAnime.Title
303 } else {
304 anime.Title.English = selectedAnime.Label
305 anime.Title.Romaji = selectedAnime.Label
306 }
301307 break
302308 }
303309
@@ -422,4 +428,3 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
422428 }
423429
424430}
425
internal/structs.go +1
@@ -140,6 +140,7 @@ type SelectionOptionImage struct {
140140
141141// SelectionOption holds the label and the internal key
142142type SelectionOption struct {
143 Title string
143144 Label string
144145 Key string
145146 Thumbnail string