Commit 47abfdfd5b
Unsigned
Layout: unified · split
internal/config.go +8 −8
| @@ -120,7 +120,7 @@ func defaultConfigMap() map[string]string { | ||
| 120 | 120 | "DiscordClientId": "1287457464148820089", |
| 121 | 121 | "Provider": "stacked", |
| 122 | 122 | "DisabledProviders": "[]", |
| 123 | "ManualProviderSearch": "false", | |
| 123 | "ManualProviderSearch": "false", | |
| 124 | 124 | "TrackingLocal": "true", |
| 125 | 125 | "TrackingRemote": "anilist", |
| 126 | 126 | "TrackingConfigured": "false", |
| @@ -739,13 +739,13 @@ func getOrderedCategories(userCurdConfig *CurdConfig) []SelectionOption { | ||
| 739 | 739 | "UPDATE": "Update (Episode, Status, Score)", |
| 740 | 740 | "REMAP_PROVIDER": "Remap Provider", |
| 741 | 741 | "CONTINUE_LAST": "Continue Last Session", |
| 742 | "PLANNING": "Plan to Watch", | |
| 743 | "COMPLETED": "Completed", | |
| 744 | "PAUSED": "Paused", | |
| 745 | "DROPPED": "Dropped", | |
| 746 | "REWATCHING": "Rewatching", | |
| 747 | "TRACKER": "Change Tracker", | |
| 748 | "PROVIDER": "Change Provider", | |
| 742 | "PLANNING": "Plan to Watch", | |
| 743 | "COMPLETED": "Completed", | |
| 744 | "PAUSED": "Paused", | |
| 745 | "DROPPED": "Dropped", | |
| 746 | "REWATCHING": "Rewatching", | |
| 747 | "TRACKER": "Change Tracker", | |
| 748 | "PROVIDER": "Change Provider", | |
| 749 | 749 | } |
| 750 | 750 | |
| 751 | 751 | // Create ordered list to store final result |
internal/curd.go +8 −81
| @@ -1318,96 +1318,23 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime) string { | ||
| 1318 | 1318 | anime.ProviderId = anime.Ep.NextEpisode.ProviderId |
| 1319 | 1319 | } |
| 1320 | 1320 | } else { |
| 1321 | // Get episode link | |
| 1322 | episodeResult, err := ResolveEpisodeURLForPlayback(*userCurdConfig, anime, anime.Ep.Number) | |
| 1323 | link := episodeResult.Links | |
| 1324 | if len(link) > 0 { | |
| 1325 | Log(fmt.Sprintf("Links details from %s/%s: %+v", episodeResult.ProviderName, episodeResult.Mode, link)) | |
| 1326 | } | |
| 1327 | if err != nil { | |
| 1328 | linkErr := err | |
| 1329 | Log(fmt.Sprintf("ResolveEpisodeURL failed: %v", linkErr)) | |
| 1330 | if reselectProviderAnime(userCurdConfig, anime, linkErr) { | |
| 1331 | episodeResult, err = ResolveEpisodeURLForPlayback(*userCurdConfig, anime, anime.Ep.Number) | |
| 1332 | link = episodeResult.Links | |
| 1333 | if err == nil { | |
| 1334 | Log(fmt.Sprintf("Successfully retrieved %s/%s episode link after provider reselect. Links count: %d", episodeResult.ProviderName, episodeResult.Mode, len(link))) | |
| 1335 | anime.Ep.Links = link | |
| 1336 | applyStreamPlaybackHints(anime, anime.Ep.Links, episodeResult.LinkHints) | |
| 1337 | goto episodeLinksReady | |
| 1338 | } | |
| 1339 | linkErr = err | |
| 1340 | Log(fmt.Sprintf("ResolveEpisodeURL still failed after provider reselect: %v", linkErr)) | |
| 1341 | } | |
| 1342 | for { | |
| 1343 | switch promptEpisodeLinkFailureRecovery(userCurdConfig) { | |
| 1344 | case "remap": | |
| 1345 | if RemapAnimeProviderOnEpisodeFailure(userCurdConfig, anime, nil) { | |
| 1346 | episodeResult, err = ResolveEpisodeURLForPlayback(*userCurdConfig, anime, anime.Ep.Number) | |
| 1347 | link = episodeResult.Links | |
| 1348 | if err == nil && len(link) > 0 { | |
| 1349 | anime.Ep.Links = link | |
| 1350 | applyStreamPlaybackHints(anime, anime.Ep.Links, episodeResult.LinkHints) | |
| 1351 | goto episodeLinksReady | |
| 1352 | } | |
| 1353 | if err != nil { | |
| 1354 | linkErr = err | |
| 1355 | Log(fmt.Sprintf("ResolveEpisodeURL failed after provider remap: %v", linkErr)) | |
| 1356 | } | |
| 1357 | } | |
| 1358 | case "episode": | |
| 1359 | episodeProviderName, episodeProviderID := AnimeProviderID(anime) | |
| 1360 | episodeList, listErr := EpisodesList(QualifyProviderID(episodeProviderName, episodeProviderID), userCurdConfig.SubOrDub) | |
| 1361 | if listErr != nil { | |
| 1362 | CurdOut("No episode list found: " + listErr.Error()) | |
| 1363 | Log(fmt.Sprintf("EpisodesList failed: %v", listErr)) | |
| 1364 | continue | |
| 1365 | } | |
| 1366 | if len(episodeList) == 0 { | |
| 1367 | CurdOut("No episodes were returned by the current provider for this anime.") | |
| 1368 | Log(fmt.Sprintf("EpisodesList returned no episodes for provider %s and id %s after ResolveEpisodeURL error: %v", episodeProviderName, episodeProviderID, linkErr)) | |
| 1369 | continue | |
| 1370 | } | |
| 1371 | episodeNumber, promptErr := promptPositiveEpisodeNumber(userCurdConfig, fmt.Sprintf("Enter the episode (%v episodes)", episodeList[len(episodeList)-1])) | |
| 1372 | if promptErr != nil { | |
| 1373 | Log("Invalid episode input: " + promptErr.Error()) | |
| 1374 | CurdOut("Invalid episode number") | |
| 1375 | continue | |
| 1376 | } | |
| 1377 | anime.Ep.Number = episodeNumber | |
| 1378 | episodeResult, err = ResolveEpisodeURLForPlayback(*userCurdConfig, anime, anime.Ep.Number) | |
| 1379 | link = episodeResult.Links | |
| 1380 | if err == nil && len(link) > 0 { | |
| 1381 | anime.Ep.Links = link | |
| 1382 | applyStreamPlaybackHints(anime, anime.Ep.Links, episodeResult.LinkHints) | |
| 1383 | goto episodeLinksReady | |
| 1384 | } | |
| 1385 | if err != nil { | |
| 1386 | linkErr = err | |
| 1387 | Log(fmt.Sprintf("ResolveEpisodeURL failed for episode %d: %v", anime.Ep.Number, linkErr)) | |
| 1388 | } else { | |
| 1389 | CurdOut("Failed to get episode link") | |
| 1390 | } | |
| 1391 | default: | |
| 1392 | RestoreScreen() | |
| 1393 | return "" | |
| 1394 | } | |
| 1395 | } | |
| 1396 | } else { | |
| 1397 | Log(fmt.Sprintf("Successfully retrieved %s/%s episode link on first try. Links count: %d", episodeResult.ProviderName, episodeResult.Mode, len(link))) | |
| 1321 | // Preferred-first resolve; diagnosed recovery only after that fails. | |
| 1322 | episodeResult, ok := resolveEpisodeLinksWithRecovery(userCurdConfig, anime, nil, true) | |
| 1323 | if !ok { | |
| 1324 | RestoreScreen() | |
| 1325 | return "" | |
| 1398 | 1326 | } |
| 1399 | anime.Ep.Links = link | |
| 1327 | Log(fmt.Sprintf("Successfully retrieved %s/%s episode link. Links count: %d", episodeResult.ProviderName, episodeResult.Mode, len(episodeResult.Links))) | |
| 1328 | anime.Ep.Links = episodeResult.Links | |
| 1400 | 1329 | applyStreamPlaybackHints(anime, anime.Ep.Links, episodeResult.LinkHints) |
| 1401 | 1330 | } |
| 1402 | 1331 | |
| 1403 | episodeLinksReady: | |
| 1404 | 1332 | if len(anime.Ep.Links) == 0 { |
| 1405 | 1333 | CurdOut("No episode links found") |
| 1406 | 1334 | RestoreScreen() |
| 1407 | 1335 | return "" |
| 1408 | } else { | |
| 1409 | Log(fmt.Sprintf("Episode links validation passed. Found %d links", len(anime.Ep.Links))) | |
| 1410 | 1336 | } |
| 1337 | Log(fmt.Sprintf("Episode links validation passed. Found %d links", len(anime.Ep.Links))) | |
| 1411 | 1338 | |
| 1412 | 1339 | // Modify the goroutine in main.go where next episode links are fetched |
| 1413 | 1340 | // Get next episode link in parallel |
internal/episode_link_recovery_test.go added +289
| @@ -0,0 +1,289 @@ | ||
| 1 | package internal | |
| 2 | ||
| 3 | import ( | |
| 4 | "errors" | |
| 5 | "strings" | |
| 6 | "testing" | |
| 7 | ) | |
| 8 | ||
| 9 | func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) { | |
| 10 | withAudio := episodeLinkFailureRecoveryOptions("sub", true) | |
| 11 | if len(withAudio) != 3 { | |
| 12 | t.Fatalf("expected 3 options with audio, got %#v", withAudio) | |
| 13 | } | |
| 14 | if withAudio[0].Key != "remap" || !strings.HasPrefix(withAudio[0].Label, "1.") { | |
| 15 | t.Fatalf("remap should be first: %#v", withAudio[0]) | |
| 16 | } | |
| 17 | if withAudio[1].Key != "audio" || !strings.Contains(withAudio[1].Label, "dub") { | |
| 18 | t.Fatalf("audio should be second and mention dub: %#v", withAudio[1]) | |
| 19 | } | |
| 20 | if withAudio[2].Key != "episode" || !strings.Contains(strings.ToLower(withAudio[2].Label), "wrong") { | |
| 21 | t.Fatalf("episode correction should be last ranked action: %#v", withAudio[2]) | |
| 22 | } | |
| 23 | ||
| 24 | // Numbered labels must sort in the same priority order as DynamicSelect's alpha sort. | |
| 25 | labels := []string{withAudio[0].Label, withAudio[1].Label, withAudio[2].Label} | |
| 26 | if !(labels[0] < labels[1] && labels[1] < labels[2]) { | |
| 27 | t.Fatalf("numbered labels should sort by priority, got %v", labels) | |
| 28 | } | |
| 29 | ||
| 30 | withoutAudio := episodeLinkFailureRecoveryOptions("dub", false) | |
| 31 | if len(withoutAudio) != 2 { | |
| 32 | t.Fatalf("expected 2 options without audio, got %#v", withoutAudio) | |
| 33 | } | |
| 34 | if withoutAudio[0].Key != "remap" || withoutAudio[1].Key != "episode" { | |
| 35 | t.Fatalf("unexpected options without audio: %#v", withoutAudio) | |
| 36 | } | |
| 37 | if !strings.HasPrefix(withoutAudio[1].Label, "2.") { | |
| 38 | t.Fatalf("episode option should renumber when audio omitted: %#v", withoutAudio[1]) | |
| 39 | } | |
| 40 | // No cancel/quit/back in our option list — DynamicSelect injects a single exit pair. | |
| 41 | for _, opt := range withAudio { | |
| 42 | switch opt.Key { | |
| 43 | case "quit", "back", "-1", "-2", "cancel": | |
| 44 | t.Fatalf("recovery options should not include exit keys: %#v", opt) | |
| 45 | } | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | func TestEpisodeLinkFailureDiagnosisIncludesEpisodeModeProviders(t *testing.T) { | |
| 50 | withAllProvidersEnabledForTest(t) | |
| 51 | cfg := &CurdConfig{Provider: `["senshi","allanime"]`, SubOrDub: "sub"} | |
| 52 | anime := &Anime{ | |
| 53 | Title: AnimeTitle{English: "Frieren: Beyond Journey's End"}, | |
| 54 | Ep: Episode{Number: 12}, | |
| 55 | ProviderName: "senshi", | |
| 56 | } | |
| 57 | msg := episodeLinkFailureDiagnosis(cfg, anime, errors.New("no sub episode links found across providers")) | |
| 58 | for _, want := range []string{ | |
| 59 | `Couldn't find Episode 12 (sub)`, | |
| 60 | `Frieren: Beyond Journey's End`, | |
| 61 | "Tried:", | |
| 62 | "senshi", | |
| 63 | "allanime", | |
| 64 | "Reason:", | |
| 65 | "no sub episode links", | |
| 66 | } { | |
| 67 | if !strings.Contains(msg, want) { | |
| 68 | t.Fatalf("diagnosis missing %q in:\n%s", want, msg) | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | func TestPromptEpisodeLinkFailureRecoveryMapsExitKeysToBack(t *testing.T) { | |
| 74 | cfg := &CurdConfig{SubOrDub: "sub", Provider: `["allanime"]`} | |
| 75 | anime := &Anime{Title: AnimeTitle{Romaji: "Example"}, Ep: Episode{Number: 1}} | |
| 76 | ||
| 77 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 78 | return SelectionOption{Key: "-2", Label: "Back"}, nil | |
| 79 | }) | |
| 80 | if got := promptEpisodeLinkFailureRecovery(cfg, anime, errors.New("fail"), true); got != "back" { | |
| 81 | t.Fatalf("expected back for -2, got %q", got) | |
| 82 | } | |
| 83 | ||
| 84 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 85 | return SelectionOption{Key: "-1", Label: "Quit"}, nil | |
| 86 | }) | |
| 87 | if got := promptEpisodeLinkFailureRecovery(cfg, anime, errors.New("fail"), true); got != "back" { | |
| 88 | t.Fatalf("expected back for -1, got %q", got) | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | func TestResolveEpisodeLinksWithRecoverySucceedsWithoutRecoveryMenu(t *testing.T) { | |
| 93 | provider := &stackStubProvider{ | |
| 94 | name: "allanime", | |
| 95 | episodeResults: map[string]map[string][]string{ | |
| 96 | "allanime-id": {"sub": {"sub-url"}}, | |
| 97 | }, | |
| 98 | searchResults: map[string][]SelectionOption{ | |
| 99 | "sub": {{Title: "Example", Key: "allanime-id"}}, | |
| 100 | }, | |
| 101 | } | |
| 102 | withProviderFactories(t, provider) | |
| 103 | ||
| 104 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 105 | t.Fatalf("recovery menu should not open when preferred resolve succeeds: %#v", options) | |
| 106 | return SelectionOption{}, nil | |
| 107 | }) | |
| 108 | ||
| 109 | cfg := &CurdConfig{Provider: `["allanime"]`, SubOrDub: "sub"} | |
| 110 | anime := &Anime{ | |
| 111 | Title: AnimeTitle{Romaji: "Example"}, | |
| 112 | ProviderName: "allanime", | |
| 113 | ProviderId: "allanime-id", | |
| 114 | Ep: Episode{Number: 1}, | |
| 115 | } | |
| 116 | ||
| 117 | result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false) | |
| 118 | if !ok { | |
| 119 | t.Fatal("expected success without recovery") | |
| 120 | } | |
| 121 | if result.Links[0] != "sub-url" || result.Mode != "sub" { | |
| 122 | t.Fatalf("unexpected result: %#v", result) | |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 126 | func TestResolveEpisodeLinksWithRecoveryOnlyAfterPreferredAndAlternateFail(t *testing.T) { | |
| 127 | // Both modes have zero streams → no alternate prompt (nothing to offer), then recovery. | |
| 128 | providerBothDead := &stackStubProvider{ | |
| 129 | name: "allanime", | |
| 130 | episodeErrors: map[string]map[string]error{ | |
| 131 | "allanime-id": { | |
| 132 | "sub": errors.New("no sub"), | |
| 133 | "dub": errors.New("no dub"), | |
| 134 | }, | |
| 135 | }, | |
| 136 | searchResults: map[string][]SelectionOption{ | |
| 137 | "sub": {{Title: "Example", Key: "allanime-id"}}, | |
| 138 | "dub": {{Title: "Example", Key: "allanime-id"}}, | |
| 139 | }, | |
| 140 | } | |
| 141 | withProviderFactories(t, providerBothDead) | |
| 142 | ||
| 143 | var promptPhases []string | |
| 144 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 145 | if len(options) >= 1 && options[0].Key == "play" { | |
| 146 | promptPhases = append(promptPhases, "alternate-audio") | |
| 147 | return SelectionOption{Key: "cancel"}, nil | |
| 148 | } | |
| 149 | if len(options) >= 1 && options[0].Key == "remap" { | |
| 150 | promptPhases = append(promptPhases, "recovery") | |
| 151 | keys := make([]string, 0, len(options)) | |
| 152 | for _, opt := range options { | |
| 153 | keys = append(keys, opt.Key) | |
| 154 | } | |
| 155 | joined := strings.Join(keys, ",") | |
| 156 | if !strings.Contains(joined, "remap") || !strings.Contains(joined, "episode") || !strings.Contains(joined, "audio") { | |
| 157 | t.Fatalf("recovery menu missing expected actions: %s", joined) | |
| 158 | } | |
| 159 | if strings.Contains(joined, "quit") || strings.Contains(joined, "cancel") { | |
| 160 | t.Fatalf("recovery menu should not embed cancel/quit keys: %s", joined) | |
| 161 | } | |
| 162 | return SelectionOption{Key: "back"}, nil | |
| 163 | } | |
| 164 | t.Fatalf("unexpected prompt options: %#v", options) | |
| 165 | return SelectionOption{}, nil | |
| 166 | }) | |
| 167 | ||
| 168 | cfg := &CurdConfig{Provider: `["allanime","no-animepahe"]`, SubOrDub: "sub"} | |
| 169 | anime := &Anime{ | |
| 170 | Title: AnimeTitle{Romaji: "Example"}, | |
| 171 | ProviderName: "allanime", | |
| 172 | ProviderId: "allanime-id", | |
| 173 | Ep: Episode{Number: 3}, | |
| 174 | } | |
| 175 | ||
| 176 | result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false) | |
| 177 | if ok || len(result.Links) > 0 { | |
| 178 | t.Fatalf("expected user back-out after recovery, got ok=%v result=%#v", ok, result) | |
| 179 | } | |
| 180 | // No playable alternate streams ⇒ skip audio prompt, go straight to recovery. | |
| 181 | if got := strings.Join(promptPhases, ","); got != "recovery" { | |
| 182 | t.Fatalf("expected recovery only when alternate has no streams, got %q", got) | |
| 183 | } | |
| 184 | if len(providerBothDead.calls) < 1 || providerBothDead.calls[0] != "allanime:sub" { | |
| 185 | t.Fatalf("expected preferred sub first, got %#v", providerBothDead.calls) | |
| 186 | } | |
| 187 | ||
| 188 | // When alternate streams exist, user is asked before recovery. | |
| 189 | providerAltOK := &stackStubProvider{ | |
| 190 | name: "allanime", | |
| 191 | episodeResults: map[string]map[string][]string{ | |
| 192 | "allanime-id": {"dub": {"dub-url"}}, | |
| 193 | }, | |
| 194 | episodeErrors: map[string]map[string]error{ | |
| 195 | "allanime-id": {"sub": errors.New("no sub")}, | |
| 196 | }, | |
| 197 | searchResults: map[string][]SelectionOption{ | |
| 198 | "sub": {{Title: "Example", Key: "allanime-id"}}, | |
| 199 | "dub": {{Title: "Example", Key: "allanime-id"}}, | |
| 200 | }, | |
| 201 | } | |
| 202 | withProviderFactories(t, providerAltOK) | |
| 203 | promptPhases = nil | |
| 204 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 205 | if len(options) >= 1 && options[0].Key == "play" { | |
| 206 | promptPhases = append(promptPhases, "alternate-audio") | |
| 207 | return SelectionOption{Key: "cancel"}, nil | |
| 208 | } | |
| 209 | if len(options) >= 1 && options[0].Key == "remap" { | |
| 210 | promptPhases = append(promptPhases, "recovery") | |
| 211 | return SelectionOption{Key: "back"}, nil | |
| 212 | } | |
| 213 | t.Fatalf("unexpected prompt options: %#v", options) | |
| 214 | return SelectionOption{}, nil | |
| 215 | }) | |
| 216 | anime.Ep.Number = 4 | |
| 217 | if _, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false); ok { | |
| 218 | t.Fatal("expected back-out after declining alternate") | |
| 219 | } | |
| 220 | if got := strings.Join(promptPhases, ","); got != "alternate-audio,recovery" { | |
| 221 | t.Fatalf("expected alternate audio prompt before recovery when dub exists, got %q", got) | |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | func TestResolveEpisodeLinksWithRecoveryRemapThenSucceeds(t *testing.T) { | |
| 226 | // First mapping fails; after user remaps via ResolveAnimeProviderMapping stubs are hard. | |
| 227 | // Simulate remap by having recovery choose "episode" change that unlocks links? Better: | |
| 228 | // recovery "audio" after preferred-only failure with dub available without going through | |
| 229 | // the built-in alternate prompt (user declined it). | |
| 230 | ||
| 231 | provider := &stackStubProvider{ | |
| 232 | name: "allanime", | |
| 233 | episodeResults: map[string]map[string][]string{ | |
| 234 | "allanime-id": {"dub": {"dub-url"}}, | |
| 235 | }, | |
| 236 | episodeErrors: map[string]map[string]error{ | |
| 237 | "allanime-id": {"sub": errors.New("no sub")}, | |
| 238 | }, | |
| 239 | searchResults: map[string][]SelectionOption{ | |
| 240 | "sub": {{Title: "Example", Key: "allanime-id"}}, | |
| 241 | "dub": {{Title: "Example", Key: "allanime-id"}}, | |
| 242 | }, | |
| 243 | } | |
| 244 | withProviderFactories(t, provider) | |
| 245 | ||
| 246 | phase := 0 | |
| 247 | withPromptSelect(t, func(options []SelectionOption) (SelectionOption, error) { | |
| 248 | phase++ | |
| 249 | switch phase { | |
| 250 | case 1: | |
| 251 | // Decline alternate during ResolveEpisodeURLForPlayback. | |
| 252 | if options[0].Key != "play" { | |
| 253 | t.Fatalf("expected alternate audio prompt first, got %#v", options) | |
| 254 | } | |
| 255 | return SelectionOption{Key: "cancel"}, nil | |
| 256 | case 2: | |
| 257 | // Recovery menu — pick explicit audio retry. | |
| 258 | if options[0].Key != "remap" { | |
| 259 | t.Fatalf("expected recovery menu, got %#v", options) | |
| 260 | } | |
| 261 | return SelectionOption{Key: "audio"}, nil | |
| 262 | case 3: | |
| 263 | // Approve alternate from recovery path. | |
| 264 | if options[0].Key != "play" { | |
| 265 | t.Fatalf("expected alternate audio approval, got %#v", options) | |
| 266 | } | |
| 267 | return SelectionOption{Key: "play"}, nil | |
| 268 | default: | |
| 269 | t.Fatalf("unexpected extra prompt phase %d: %#v", phase, options) | |
| 270 | return SelectionOption{}, nil | |
| 271 | } | |
| 272 | }) | |
| 273 | ||
| 274 | cfg := &CurdConfig{Provider: `["allanime","no-animepahe"]`, SubOrDub: "sub"} | |
| 275 | anime := &Anime{ | |
| 276 | Title: AnimeTitle{Romaji: "Example"}, | |
| 277 | ProviderName: "allanime", | |
| 278 | ProviderId: "allanime-id", | |
| 279 | Ep: Episode{Number: 1}, | |
| 280 | } | |
| 281 | ||
| 282 | result, ok := resolveEpisodeLinksWithRecovery(cfg, anime, nil, false) | |
| 283 | if !ok { | |
| 284 | t.Fatal("expected recovery audio path to succeed") | |
| 285 | } | |
| 286 | if result.Mode != "dub" || result.Links[0] != "dub-url" { | |
| 287 | t.Fatalf("expected dub result after recovery audio, got %#v", result) | |
| 288 | } | |
| 289 | } | |
internal/localTracking.go +5 −34
| @@ -473,41 +473,12 @@ func WatchUntracked(userCurdConfig *CurdConfig) { | ||
| 473 | 473 | } |
| 474 | 474 | anime.Ep.NextEpisode = NextEpisode{} |
| 475 | 475 | } else { |
| 476 | // Resolve links: preferred SubOrDub is exhausted before any audio-mode prompt. | |
| 477 | resolvedLink, err := ResolveEpisodeURLForPlayback(*userCurdConfig, &anime, anime.Ep.Number) | |
| 478 | if err != nil { | |
| 479 | Log(fmt.Sprintf("Failed to get episode link: %v", err)) | |
| 480 | switch promptEpisodeLinkFailureRecovery(userCurdConfig) { | |
| 481 | case "remap": | |
| 482 | if RemapAnimeProviderOnEpisodeFailure(userCurdConfig, &anime, nil) { | |
| 483 | continue | |
| 484 | } | |
| 485 | case "episode": | |
| 486 | episodePrompt := "Enter the episode number" | |
| 487 | providerName, providerID := AnimeProviderID(&anime) | |
| 488 | if providerID != "" { | |
| 489 | if episodeList, listErr := EpisodesList(QualifyProviderID(providerName, providerID), userCurdConfig.SubOrDub); listErr == nil && len(episodeList) > 0 { | |
| 490 | episodePrompt = fmt.Sprintf("Enter the episode (%v episodes)", episodeList[len(episodeList)-1]) | |
| 491 | } | |
| 492 | } | |
| 493 | episodeNumber, promptErr := promptPositiveEpisodeNumber(userCurdConfig, episodePrompt) | |
| 494 | if promptErr != nil { | |
| 495 | Log(fmt.Sprintf("Invalid episode number: %v", promptErr)) | |
| 496 | CurdOut("Invalid episode number") | |
| 497 | continue | |
| 498 | } | |
| 499 | anime.Ep.Number = episodeNumber | |
| 500 | continue | |
| 501 | default: | |
| 502 | ExitCurd(nil) | |
| 503 | } | |
| 504 | continue | |
| 505 | } | |
| 506 | link := resolvedLink.Links | |
| 507 | if len(link) == 0 { | |
| 508 | ExitCurd(fmt.Errorf("No episode links found")) | |
| 476 | // Preferred-first resolve; diagnosed recovery only after that fails. | |
| 477 | resolvedLink, ok := resolveEpisodeLinksWithRecovery(userCurdConfig, &anime, nil, true) | |
| 478 | if !ok { | |
| 479 | ExitCurd(nil) | |
| 509 | 480 | } |
| 510 | anime.Ep.Links = link | |
| 481 | anime.Ep.Links = resolvedLink.Links | |
| 511 | 482 | applyStreamPlaybackHints(&anime, anime.Ep.Links, resolvedLink.LinkHints) |
| 512 | 483 | } |
| 513 | 484 | |
internal/provider_mapping.go +174 −10
| @@ -780,7 +780,7 @@ func RemapAnimeProviderOnEpisodeFailure(config *CurdConfig, anime *Anime, anilis | ||
| 780 | 780 | return false |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | CurdOut("Could not get an episode link with the current provider mapping.") | |
| 783 | CurdOut("Wrong match is common — search again and pick the right title.") | |
| 784 | 784 | anime.ProviderId = "" |
| 785 | 785 | anime.ProviderName = "" |
| 786 | 786 | anime.Ep.NextEpisode = NextEpisode{} |
| @@ -793,19 +793,183 @@ func RemapAnimeProviderOnEpisodeFailure(config *CurdConfig, anime *Anime, anilis | ||
| 793 | 793 | return outcome == ProviderMappingOK |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | func promptEpisodeLinkFailureRecovery(config *CurdConfig) string { | |
| 797 | selected, err := promptSelect([]SelectionOption{ | |
| 798 | {Key: "remap", Label: "Search providers again"}, | |
| 799 | {Key: "episode", Label: "Try a different episode number"}, | |
| 800 | {Key: "quit", Label: "Cancel playback"}, | |
| 796 | // episodeLinkFailureRecoveryOptions builds ranked recovery actions for a dead-end | |
| 797 | // after preferred (and optional alternate) resolve already failed. | |
| 798 | // Labels are numbered so DynamicSelect's alphabetical sort preserves priority. | |
| 799 | func episodeLinkFailureRecoveryOptions(preferredMode string, includeAudio bool) []SelectionOption { | |
| 800 | preferredMode = normalizeTranslationType(preferredMode) | |
| 801 | alternateMode := alternateTranslationType(preferredMode) | |
| 802 | ||
| 803 | options := []SelectionOption{ | |
| 804 | {Key: "remap", Label: "1. Search for this anime again"}, | |
| 805 | } | |
| 806 | next := 2 | |
| 807 | if includeAudio { | |
| 808 | options = append(options, SelectionOption{ | |
| 809 | Key: "audio", | |
| 810 | Label: fmt.Sprintf("%d. Try other audio (%s)", next, alternateMode), | |
| 811 | }) | |
| 812 | next++ | |
| 813 | } | |
| 814 | options = append(options, SelectionOption{ | |
| 815 | Key: "episode", | |
| 816 | Label: fmt.Sprintf("%d. Change episode number (if this one is wrong)", next), | |
| 801 | 817 | }) |
| 818 | return options | |
| 819 | } | |
| 820 | ||
| 821 | func episodeLinkFailureDiagnosis(config *CurdConfig, anime *Anime, lastErr error) string { | |
| 822 | title := "this anime" | |
| 823 | if anime != nil { | |
| 824 | if name := strings.TrimSpace(GetAnimeName(*anime)); name != "" { | |
| 825 | title = name | |
| 826 | } | |
| 827 | } | |
| 828 | ||
| 829 | epNo := 0 | |
| 830 | if anime != nil { | |
| 831 | epNo = anime.Ep.Number | |
| 832 | } | |
| 833 | ||
| 834 | mode := "sub" | |
| 835 | if config != nil { | |
| 836 | mode = normalizeTranslationType(config.SubOrDub) | |
| 837 | } | |
| 838 | ||
| 839 | providers := configuredProviderNames(config) | |
| 840 | tried := "none" | |
| 841 | if len(providers) > 0 { | |
| 842 | tried = strings.Join(providers, ", ") | |
| 843 | } | |
| 844 | ||
| 845 | var b strings.Builder | |
| 846 | fmt.Fprintf(&b, "Couldn't find Episode %d (%s) for %q\nTried: %s", epNo, mode, title, tried) | |
| 847 | if lastErr != nil { | |
| 848 | reason := strings.TrimSpace(lastErr.Error()) | |
| 849 | if reason != "" { | |
| 850 | // Keep diagnosis readable — one short reason line. | |
| 851 | if len(reason) > 160 { | |
| 852 | reason = reason[:157] + "..." | |
| 853 | } | |
| 854 | fmt.Fprintf(&b, "\nReason: %s", reason) | |
| 855 | } | |
| 856 | } | |
| 857 | return b.String() | |
| 858 | } | |
| 859 | ||
| 860 | // promptEpisodeLinkFailureRecovery shows a diagnosed dead-end after full resolve | |
| 861 | // failure. Returns: remap | audio | episode | back | |
| 862 | // DynamicSelect still injects Back/Quit; both map to "back" (single exit path). | |
| 863 | func promptEpisodeLinkFailureRecovery(config *CurdConfig, anime *Anime, lastErr error, includeAudio bool) string { | |
| 864 | preferredMode := "sub" | |
| 865 | if config != nil { | |
| 866 | preferredMode = normalizeTranslationType(config.SubOrDub) | |
| 867 | } | |
| 868 | ||
| 869 | CurdOut(episodeLinkFailureDiagnosis(config, anime, lastErr)) | |
| 870 | ||
| 871 | selected, err := promptSelect(episodeLinkFailureRecoveryOptions(preferredMode, includeAudio)) | |
| 802 | 872 | if err != nil { |
| 803 | return "quit" | |
| 873 | return "back" | |
| 804 | 874 | } |
| 805 | if selected.Key == "-1" || selected.Key == "-2" { | |
| 806 | return "quit" | |
| 875 | switch selected.Key { | |
| 876 | case "remap", "audio", "episode": | |
| 877 | return selected.Key | |
| 878 | case "-1", "-2", "quit", "back", "": | |
| 879 | return "back" | |
| 880 | default: | |
| 881 | return "back" | |
| 882 | } | |
| 883 | } | |
| 884 | ||
| 885 | // resolveEpisodeLinksWithRecovery runs preferred-first playback resolve and only | |
| 886 | // after that fails presents the diagnosed recovery menu. Returns ok=false when | |
| 887 | // the user backs out. | |
| 888 | func resolveEpisodeLinksWithRecovery(config *CurdConfig, anime *Anime, anilistEntry *Entry, allowAnimepaheReselect bool) (ProviderEpisodeResult, bool) { | |
| 889 | if config == nil || anime == nil { | |
| 890 | return ProviderEpisodeResult{}, false | |
| 891 | } | |
| 892 | ||
| 893 | var lastErr error | |
| 894 | includeAudio := true | |
| 895 | attemptedAnimepaheReselect := false | |
| 896 | ||
| 897 | for { | |
| 898 | result, err := ResolveEpisodeURLForPlayback(*config, anime, anime.Ep.Number) | |
| 899 | if err == nil && len(result.Links) > 0 { | |
| 900 | return result, true | |
| 901 | } | |
| 902 | if err != nil { | |
| 903 | lastErr = err | |
| 904 | } else { | |
| 905 | lastErr = fmt.Errorf("no episode links found") | |
| 906 | } | |
| 907 | Log(fmt.Sprintf("ResolveEpisodeURLForPlayback failed for ep %d: %v", anime.Ep.Number, lastErr)) | |
| 908 | ||
| 909 | // One-shot Animepahe stale-mapping reselect (tracked path helper). | |
| 910 | if allowAnimepaheReselect && !attemptedAnimepaheReselect { | |
| 911 | attemptedAnimepaheReselect = true | |
| 912 | if reselectProviderAnime(config, anime, lastErr) { | |
| 913 | result, err = ResolveEpisodeURLForPlayback(*config, anime, anime.Ep.Number) | |
| 914 | if err == nil && len(result.Links) > 0 { | |
| 915 | Log(fmt.Sprintf("Successfully retrieved %s/%s episode link after provider reselect", result.ProviderName, result.Mode)) | |
| 916 | return result, true | |
| 917 | } | |
| 918 | if err != nil { | |
| 919 | lastErr = err | |
| 920 | } else { | |
| 921 | lastErr = fmt.Errorf("no episode links found after provider reselect") | |
| 922 | } | |
| 923 | Log(fmt.Sprintf("ResolveEpisodeURL still failed after provider reselect: %v", lastErr)) | |
| 924 | } | |
| 925 | } | |
| 926 | ||
| 927 | switch promptEpisodeLinkFailureRecovery(config, anime, lastErr, includeAudio) { | |
| 928 | case "remap": | |
| 929 | if RemapAnimeProviderOnEpisodeFailure(config, anime, anilistEntry) { | |
| 930 | // Full preferred-first resolve again after remap. | |
| 931 | continue | |
| 932 | } | |
| 933 | lastErr = fmt.Errorf("provider remap did not produce a new mapping") | |
| 934 | case "audio": | |
| 935 | // Explicit re-offer of alternate mode (user may have declined earlier). | |
| 936 | altResult, altErr := ResolveEpisodeURLAlternateModeWithPrompt(*config, anime, anime.Ep.Number, nil) | |
| 937 | if altErr == nil && len(altResult.Links) > 0 { | |
| 938 | return altResult, true | |
| 939 | } | |
| 940 | if altErr != nil { | |
| 941 | lastErr = altErr | |
| 942 | if strings.Contains(strings.ToLower(altErr.Error()), "no ") && | |
| 943 | strings.Contains(strings.ToLower(altErr.Error()), "streams available") { | |
| 944 | includeAudio = false | |
| 945 | } | |
| 946 | } else { | |
| 947 | lastErr = fmt.Errorf("no %s streams available", alternateTranslationType(config.SubOrDub)) | |
| 948 | includeAudio = false | |
| 949 | } | |
| 950 | CurdOut("Still no playable stream after trying other audio.") | |
| 951 | case "episode": | |
| 952 | episodePrompt := "Change episode number (if this one is wrong)" | |
| 953 | providerName, providerID := AnimeProviderID(anime) | |
| 954 | if providerID != "" { | |
| 955 | if episodeList, listErr := EpisodesList(QualifyProviderID(providerName, providerID), config.SubOrDub); listErr == nil && len(episodeList) > 0 { | |
| 956 | episodePrompt = fmt.Sprintf("Change episode number (provider lists up to %v)", episodeList[len(episodeList)-1]) | |
| 957 | } | |
| 958 | } | |
| 959 | episodeNumber, promptErr := promptPositiveEpisodeNumber(config, episodePrompt) | |
| 960 | if promptErr != nil { | |
| 961 | Log("Invalid episode input: " + promptErr.Error()) | |
| 962 | CurdOut("Invalid episode number") | |
| 963 | continue | |
| 964 | } | |
| 965 | anime.Ep.Number = episodeNumber | |
| 966 | anime.Ep.NextEpisode = NextEpisode{} | |
| 967 | // Re-run full preferred-first resolve for the new episode. | |
| 968 | continue | |
| 969 | default: | |
| 970 | return ProviderEpisodeResult{}, false | |
| 971 | } | |
| 807 | 972 | } |
| 808 | return selected.Key | |
| 809 | 973 | } |
| 810 | 974 | |
| 811 | 975 | func ResolveUntrackedProviderSearch(config *CurdConfig, initialQuery string) (providerID, providerName string, back bool, err error) { |