Commit 0a7afdf3e3

0a7afdf3e39dd3260afeffd8366aef516aacf0d7

parent: 963c0b2999

Unsigned

Wraient <wraient.911@gmail.com> · 2026-08-02 21:44 UTC

fix: determine total episodes across provider stack when a provider is down or rate-limited

Layout: unified · split

internal/curd.go +15
@@ -1144,6 +1144,21 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
1144 } 1144 }
1145 } 1145 }
1146 1146
1147 if anime.TotalEpisodes == 0 { // If the mapped provider alone couldn't provide a total
1148 CurdOut("Attempting to determine total episodes from the full provider stack.")
1149 totalQuery := animeSearchTitle(anime)
1150 if strings.TrimSpace(totalQuery) == "" {
1151 totalQuery = string(userQuery)
1152 }
1153 stackTotal, stackErr := determineProviderTotalEpisodes(userCurdConfig, totalQuery, anime, userCurdConfig.SubOrDub)
1154 if stackErr != nil {
1155 Log(fmt.Sprintf("Failed to determine total episodes from provider stack: %v", stackErr))
1156 } else {
1157 anime.TotalEpisodes = stackTotal
1158 CurdOut(fmt.Sprintf("Retrieved total episodes from provider stack: %d", anime.TotalEpisodes))
1159 }
1160 }
1161
1147 if anime.TotalEpisodes == 0 { // If provider episode list did not have a usable total 1162 if anime.TotalEpisodes == 0 { // If provider episode list did not have a usable total
1148 CurdOut("Attempting to retrieve total episodes from anime search results.") 1163 CurdOut("Attempting to retrieve total episodes from anime search results.")
1149 animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub) 1164 animeList, err := SearchAnime(string(userQuery), userCurdConfig.SubOrDub)
internal/live_determine_total_test.go added +65
@@ -0,0 +1,65 @@
1package internal
2
3import (
4 "os"
5 "testing"
6)
7
8// Exercises determineProviderTotalEpisodes against the real provider stack for
9// "One Piece" using the app's own Go HTTP client (no browser), to confirm the
10// total is resolved even when individual providers are down or rate-limited.
11func TestLiveDetermineProviderTotalEpisodes(t *testing.T) {
12 if os.Getenv("CURD_LIVE_DETERMINE_TOTAL") == "" {
13 t.Skip("set CURD_LIVE_DETERMINE_TOTAL=1 to run live")
14 }
15 cfg := &CurdConfig{
16 StoragePath: os.ExpandEnv("$HOME/.local/share/curd"),
17 SubOrDub: "sub",
18 Provider: "stacked",
19 TrackingRemote: "myanimelist",
20 }
21 withGlobalConfig(t, cfg)
22
23 anime := &Anime{
24 AnilistId: 21,
25 MalId: 21,
26 TotalEpisodes: 1100,
27 Title: AnimeTitle{English: "One Piece", Romaji: "One Piece", Japanese: "ONE PIECE"},
28 ProviderName: "",
29 ProviderId: "",
30 IsAiring: true,
31 }
32
33 for _, pn := range configuredProviderNames(cfg) {
34 p, err := ProviderByName(pn)
35 if err != nil {
36 t.Logf(" [%s] not configured: %v", pn, err)
37 continue
38 }
39 opts, derr := p.SearchAnime("One Piece", "sub")
40 if derr != nil {
41 t.Logf(" [%s] search ERROR: %v", pn, derr)
42 continue
43 }
44 if len(opts) == 0 {
45 t.Logf(" [%s] search returned no results", pn)
46 continue
47 }
48 best, _ := selectBestProviderSearchResult(opts, anime, "One Piece")
49 total, terr := getProviderTotalEpisodes(p, best.Key, "sub")
50 t.Logf(" [%s] matched Key=%q total=%d err=%v", pn, best.Key, total, terr)
51 }
52
53 total, err := determineProviderTotalEpisodes(cfg, "One Piece", anime, "sub")
54 t.Logf("determineProviderTotalEpisodes -> total=%d err=%v", total, err)
55 if err != nil {
56 t.Fatalf("could not determine total episodes from providers: %v", err)
57 }
58 if total <= 0 {
59 t.Fatalf("got non-positive total %d", total)
60 }
61 if total < 1000 {
62 t.Fatalf("expected a realistic One Piece total >1000, got %d (wrong match or broken ep list)", total)
63 }
64 t.Logf("OK: One Piece total episodes = %d", total)
65}\ No newline at end of file
internal/provider.go +61
@@ -534,6 +534,67 @@ func getProviderTotalEpisodes(provider Provider, showID, mode string) (int, erro
534 return 0, fmt.Errorf("provider returned no usable episode numbers") 534 return 0, fmt.Errorf("provider returned no usable episode numbers")
535} 535}
536 536
537// determineProviderTotalEpisodes finds the total episode count for an anime by
538// querying the configured provider stack. It first uses the anime's existing
539// provider mapping when one exists, then falls back to searching each provider
540// for the title and reading its episode list. This keeps the total findable
541// even when the preferred provider is down or the anime has no mapping yet
542// (e.g. an anime played straight from a MyAnimeList/AniList tracking list).
543func determineProviderTotalEpisodes(config *CurdConfig, query string, anime *Anime, mode string) (int, error) {
544 if anime == nil {
545 return 0, fmt.Errorf("missing anime")
546 }
547
548 // 1) Use the anime's existing provider mapping when it has one.
549 if providerName, providerID := AnimeProviderID(anime); providerID != "" {
550 if provider, err := ProviderByName(providerName); err == nil {
551 if total, err := getProviderTotalEpisodes(provider, providerID, mode); err == nil && total > 0 {
552 return total, nil
553 }
554 }
555 }
556
557 // 2) Otherwise search each configured provider and read its episode list so
558 // a single down provider can't block the total from being determined.
559 var lookupErrors []string
560 for _, providerName := range configuredProviderNames(config) {
561 provider, err := ProviderByName(providerName)
562 if err != nil {
563 continue
564 }
565 options, err := provider.SearchAnime(query, mode)
566 if err != nil || len(options) == 0 {
567 if err != nil {
568 lookupErrors = append(lookupErrors, fmt.Sprintf("%s search: %v", providerName, err))
569 }
570 continue
571 }
572 searchQuery := strings.TrimSpace(query)
573 if searchQuery == "" {
574 searchQuery = animeSearchTitle(anime)
575 }
576 option, ok := selectBestProviderSearchResult(options, anime, searchQuery)
577 if !ok {
578 continue
579 }
580 providerID := option.Key
581 if _, parsedID, ok := ParseProviderQualifiedID(option.Key); ok {
582 providerID = parsedID
583 }
584 if strings.TrimSpace(providerID) == "" {
585 continue
586 }
587 if total, err := getProviderTotalEpisodes(provider, providerID, mode); err == nil && total > 0 {
588 return total, nil
589 }
590 }
591
592 if len(lookupErrors) > 0 {
593 return 0, fmt.Errorf("could not determine total episodes from any provider: %s", strings.Join(lookupErrors, "; "))
594 }
595 return 0, fmt.Errorf("could not determine total episodes from any provider")
596}
597
537func inferProviderTotalEpisodes(providerName string, episodes []string) int { 598func inferProviderTotalEpisodes(providerName string, episodes []string) int {
538 if strings.EqualFold(providerName, "animepahe") { 599 if strings.EqualFold(providerName, "animepahe") {
539 return countUsableEpisodeEntries(episodes) 600 return countUsableEpisodeEntries(episodes)