Commit 37a9025825
Unsigned
Layout: unified · split
internal/player.go +7
| @@ -327,6 +327,13 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, | ||
| 327 | 327 | // Wait a brief moment for the file to load |
| 328 | 328 | time.Sleep(100 * time.Millisecond) |
| 329 | 329 | |
| 330 | if subtitleURL := strings.TrimSpace(anime.Ep.SubtitleURL); subtitleURL != "" && !hasMPVSubtitleArg(args) { | |
| 331 | _, subErr := MPVSendCommand(mpvSocketPath, []interface{}{"sub-add", subtitleURL, "select"}) | |
| 332 | if subErr != nil { | |
| 333 | Log(fmt.Sprintf("Failed to load subtitle track: %v", subErr)) | |
| 334 | } | |
| 335 | } | |
| 336 | ||
| 330 | 337 | // Update the window title |
| 331 | 338 | titleCommand := []interface{}{"set_property", "force-media-title", title} |
| 332 | 339 | _, err = MPVSendCommand(mpvSocketPath, titleCommand) |
internal/providers/anineko/bibiemb.go +1 −1
| @@ -45,7 +45,7 @@ func resolveBibiemb(embedURL string) (resolvedStream, error) { | ||
| 45 | 45 | return resolvedStream{ |
| 46 | 46 | URL: variantURL, |
| 47 | 47 | Referrer: embedURL, |
| 48 | Subtitle: subtitleFromEmbedURL(embedURL), | |
| 48 | Subtitle: resolveSubtitle(embedURL, html), | |
| 49 | 49 | }, nil |
| 50 | 50 | } |
| 51 | 51 | |
internal/providers/anineko/provider_test.go +42
| @@ -117,6 +117,48 @@ func TestExtractLangEmbedURLs(t *testing.T) { | ||
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | func TestSubtitleFromEmbedHTML(t *testing.T) { | |
| 121 | html := `const subtitle = "https://cdn.example/page.vtt";` | |
| 122 | if got := subtitleFromEmbedHTML(html); got != "https://cdn.example/page.vtt" { | |
| 123 | t.Fatalf("unexpected subtitle %q", got) | |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | func TestResolveSubtitlePrefersEmbedURL(t *testing.T) { | |
| 128 | embedURL := "https://vibeplayer.site/soft?sub=https://cdn.example/url.vtt" | |
| 129 | html := `const subtitle = "https://cdn.example/page.vtt";` | |
| 130 | if got := resolveSubtitle(embedURL, html); got != "https://cdn.example/url.vtt" { | |
| 131 | t.Fatalf("unexpected subtitle %q", got) | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | func TestResolveEmbedHostDetectsVivibebe(t *testing.T) { | |
| 136 | if got := resolveEmbedHost("https://vivibebe.site/e6693c8de8202fbe"); got != "vibeplayer" { | |
| 137 | t.Fatalf("expected vibeplayer host, got %q", got) | |
| 138 | } | |
| 139 | } | |
| 140 | ||
| 141 | func TestChooseSubStyleHardPromptsForSoftFallback(t *testing.T) { | |
| 142 | resetSubStyleForTest() | |
| 143 | groups := map[string][]string{ | |
| 144 | "sub": {"https://vibeplayer.site/soft?sub=https://cdn.example/sub.vtt"}, | |
| 145 | } | |
| 146 | ||
| 147 | previousPrompt := curdhost.PromptSelect | |
| 148 | curdhost.PromptSelect = func([]curdhost.PromptOption) (curdhost.PromptOption, error) { | |
| 149 | return curdhost.PromptOption{Key: "soft", Label: "Use soft subs"}, nil | |
| 150 | } | |
| 151 | t.Cleanup(func() { | |
| 152 | curdhost.PromptSelect = previousPrompt | |
| 153 | resetSubStyleForTest() | |
| 154 | }) | |
| 155 | ||
| 156 | style, err := chooseSubStyle(groups, "hard") | |
| 157 | if err != nil || style != "soft" { | |
| 158 | t.Fatalf("expected soft after prompt, got style=%q err=%v", style, err) | |
| 159 | } | |
| 160 | } | |
| 161 | ||
| 120 | 162 | func TestChooseSubStyleRespectsPreference(t *testing.T) { |
| 121 | 163 | groups := map[string][]string{ |
| 122 | 164 | "hsub": {"https://vibeplayer.site/hard"}, |
internal/providers/anineko/scrape.go +46 −14
| @@ -7,8 +7,10 @@ import ( | ||
| 7 | 7 | ) |
| 8 | 8 | |
| 9 | 9 | var ( |
| 10 | dataVideoRE = regexp.MustCompile(`data-video="([^"]+)"`) | |
| 11 | subtitleRE = regexp.MustCompile(`[?&](?:sub|caption_1|c1_file)=([^&"]+)`) | |
| 10 | dataVideoRE = regexp.MustCompile(`data-video="([^"]+)"`) | |
| 11 | subtitleRE = regexp.MustCompile(`[?&](?:sub|caption_1|c1_file)=([^&"]+)`) | |
| 12 | embedSubtitleRE = regexp.MustCompile(`const subtitle = "([^"]+)"`) | |
| 13 | embedTrackFileRE = regexp.MustCompile(`file:\s*"([^"]+\.(?:vtt|ass|srt))"`) | |
| 12 | 14 | ) |
| 13 | 15 | |
| 14 | 16 | func extractLangEmbedURLs(html string) map[string][]string { |
| @@ -48,19 +50,26 @@ func extractLangEmbedURLs(html string) map[string][]string { | ||
| 48 | 50 | return groups |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | func partitionByHost(embedURLs []string) (bibiemb, vibeplayer, other []string) { | |
| 52 | for _, embedURL := range embedURLs { | |
| 53 | host := strings.ToLower(hostFromURL(embedURL)) | |
| 54 | switch { | |
| 55 | case strings.Contains(host, "bibiemb."): | |
| 56 | bibiemb = append(bibiemb, embedURL) | |
| 57 | case strings.Contains(host, "vibeplayer."): | |
| 58 | vibeplayer = append(vibeplayer, embedURL) | |
| 59 | default: | |
| 60 | other = append(other, embedURL) | |
| 61 | } | |
| 53 | func isVibeplayerHost(host string) bool { | |
| 54 | host = strings.ToLower(strings.TrimSpace(host)) | |
| 55 | if host == "" { | |
| 56 | return false | |
| 57 | } | |
| 58 | if strings.Contains(host, "vibeplayer.") || strings.Contains(host, "vivibebe.") { | |
| 59 | return true | |
| 60 | } | |
| 61 | return strings.HasPrefix(host, "vibe") && strings.Contains(host, "be.") | |
| 62 | } | |
| 63 | ||
| 64 | func resolveEmbedHost(embedURL string) string { | |
| 65 | switch { | |
| 66 | case strings.Contains(strings.ToLower(hostFromURL(embedURL)), "bibiemb."): | |
| 67 | return "bibiemb" | |
| 68 | case isVibeplayerHost(hostFromURL(embedURL)): | |
| 69 | return "vibeplayer" | |
| 70 | default: | |
| 71 | return "" | |
| 62 | 72 | } |
| 63 | return bibiemb, vibeplayer, other | |
| 64 | 73 | } |
| 65 | 74 | |
| 66 | 75 | func subtitleFromEmbedURL(embedURL string) string { |
| @@ -89,6 +98,29 @@ func subtitleFromEmbedURL(embedURL string) string { | ||
| 89 | 98 | return match[1] |
| 90 | 99 | } |
| 91 | 100 | |
| 101 | func subtitleFromEmbedHTML(html string) string { | |
| 102 | html = strings.TrimSpace(html) | |
| 103 | if html == "" { | |
| 104 | return "" | |
| 105 | } | |
| 106 | if match := embedSubtitleRE.FindStringSubmatch(html); len(match) >= 2 { | |
| 107 | if subtitle := strings.TrimSpace(match[1]); subtitle != "" { | |
| 108 | return subtitle | |
| 109 | } | |
| 110 | } | |
| 111 | if match := embedTrackFileRE.FindStringSubmatch(html); len(match) >= 2 { | |
| 112 | return strings.TrimSpace(match[1]) | |
| 113 | } | |
| 114 | return "" | |
| 115 | } | |
| 116 | ||
| 117 | func resolveSubtitle(embedURL, html string) string { | |
| 118 | if subtitle := subtitleFromEmbedURL(embedURL); subtitle != "" { | |
| 119 | return subtitle | |
| 120 | } | |
| 121 | return subtitleFromEmbedHTML(html) | |
| 122 | } | |
| 123 | ||
| 92 | 124 | func hostFromURL(rawURL string) string { |
| 93 | 125 | parsed, err := url.Parse(rawURL) |
| 94 | 126 | if err != nil { |
internal/providers/anineko/streams.go +11 −10
| @@ -31,18 +31,19 @@ func getEpisodeStreamsForMode(slug string, config providers.PlaybackConfig, epNo | ||
| 31 | 31 | return nil, nil, fmt.Errorf("no %s streams found for episode %d", mode, epNo) |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | bibiembURLs, vibeplayerURLs, _ := partitionByHost(embedURLs) | |
| 35 | ||
| 36 | for _, embedURL := range bibiembURLs { | |
| 37 | stream, err := resolveBibiemb(embedURL) | |
| 38 | if err != nil { | |
| 34 | for _, embedURL := range embedURLs { | |
| 35 | var ( | |
| 36 | stream resolvedStream | |
| 37 | err error | |
| 38 | ) | |
| 39 | switch resolveEmbedHost(embedURL) { | |
| 40 | case "bibiemb": | |
| 41 | stream, err = resolveBibiemb(embedURL) | |
| 42 | case "vibeplayer": | |
| 43 | stream, err = resolveVibeplayer(embedURL) | |
| 44 | default: | |
| 39 | 45 | continue |
| 40 | 46 | } |
| 41 | return singleStreamResult(stream) | |
| 42 | } | |
| 43 | ||
| 44 | for _, embedURL := range vibeplayerURLs { | |
| 45 | stream, err := resolveVibeplayer(embedURL) | |
| 46 | 47 | if err != nil { |
| 47 | 48 | continue |
| 48 | 49 | } |
internal/providers/anineko/substyle.go +3 −101
| @@ -1,111 +1,15 @@ | ||
| 1 | 1 | package anineko |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "fmt" | |
| 5 | 4 | "strings" |
| 6 | "sync" | |
| 7 | 5 | |
| 8 | "github.com/wraient/curd/internal/curdhost" | |
| 6 | "github.com/wraient/curd/internal/providers/substyle" | |
| 9 | 7 | ) |
| 10 | 8 | |
| 11 | var ( | |
| 12 | subStyleChoiceMu sync.Mutex | |
| 13 | subStyleChosen string | |
| 14 | ) | |
| 15 | ||
| 16 | func normalizeSubStyle(style string) string { | |
| 17 | switch strings.ToLower(strings.TrimSpace(style)) { | |
| 18 | case "soft": | |
| 19 | return "soft" | |
| 20 | case "hard": | |
| 21 | return "hard" | |
| 22 | default: | |
| 23 | return "ask" | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | func resolveSubStylePreference(fallback string) string { | |
| 28 | if curdhost.CurrentSubStyle != nil { | |
| 29 | if live := normalizeSubStyle(curdhost.CurrentSubStyle()); live != "ask" { | |
| 30 | return live | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | subStyleChoiceMu.Lock() | |
| 35 | defer subStyleChoiceMu.Unlock() | |
| 36 | if subStyleChosen != "" { | |
| 37 | return subStyleChosen | |
| 38 | } | |
| 39 | return normalizeSubStyle(fallback) | |
| 40 | } | |
| 41 | ||
| 42 | func rememberSubStyleChoice(style string) { | |
| 43 | style = normalizeSubStyle(style) | |
| 44 | if style != "soft" && style != "hard" { | |
| 45 | return | |
| 46 | } | |
| 47 | ||
| 48 | subStyleChoiceMu.Lock() | |
| 49 | subStyleChosen = style | |
| 50 | subStyleChoiceMu.Unlock() | |
| 51 | ||
| 52 | if curdhost.PersistSubStylePreference != nil { | |
| 53 | _ = curdhost.PersistSubStylePreference(style) | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | 9 | func chooseSubStyle(groups map[string][]string, preference string) (string, error) { |
| 58 | preference = resolveSubStylePreference(preference) | |
| 59 | 10 | hasSoft := len(groups["sub"]) > 0 |
| 60 | 11 | hasHard := len(groups["hsub"]) > 0 |
| 61 | ||
| 62 | switch preference { | |
| 63 | case "soft": | |
| 64 | if hasSoft { | |
| 65 | return "soft", nil | |
| 66 | } | |
| 67 | if hasHard { | |
| 68 | return "hard", nil | |
| 69 | } | |
| 70 | case "hard": | |
| 71 | if hasHard { | |
| 72 | return "hard", nil | |
| 73 | } | |
| 74 | if hasSoft { | |
| 75 | return "soft", nil | |
| 76 | } | |
| 77 | case "ask": | |
| 78 | if hasSoft && hasHard { | |
| 79 | if curdhost.PromptSelect == nil { | |
| 80 | rememberSubStyleChoice("soft") | |
| 81 | return "soft", nil | |
| 82 | } | |
| 83 | if curdhost.Out != nil { | |
| 84 | curdhost.Out("Both soft-sub and hard-sub streams are available.") | |
| 85 | } | |
| 86 | selected, err := curdhost.PromptSelect([]curdhost.PromptOption{ | |
| 87 | {Key: "soft", Label: "Soft sub (external subtitles)"}, | |
| 88 | {Key: "hard", Label: "Hard sub (burned-in subtitles)"}, | |
| 89 | }) | |
| 90 | if err != nil { | |
| 91 | return "", err | |
| 92 | } | |
| 93 | chosen := "soft" | |
| 94 | if selected.Key == "hard" { | |
| 95 | chosen = "hard" | |
| 96 | } | |
| 97 | rememberSubStyleChoice(chosen) | |
| 98 | return chosen, nil | |
| 99 | } | |
| 100 | if hasSoft { | |
| 101 | return "soft", nil | |
| 102 | } | |
| 103 | if hasHard { | |
| 104 | return "hard", nil | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | return "", fmt.Errorf("no sub streams found") | |
| 12 | return substyle.Choose(hasSoft, hasHard, preference) | |
| 109 | 13 | } |
| 110 | 14 | |
| 111 | 15 | func embedURLsForSubStyle(groups map[string][]string, style string) []string { |
| @@ -132,7 +36,5 @@ func embedURLsForMode(groups map[string][]string, mode, subStyle string) ([]stri | ||
| 132 | 36 | } |
| 133 | 37 | |
| 134 | 38 | func resetSubStyleForTest() { |
| 135 | subStyleChoiceMu.Lock() | |
| 136 | subStyleChosen = "" | |
| 137 | subStyleChoiceMu.Unlock() | |
| 39 | substyle.ResetForTest() | |
| 138 | 40 | } |
internal/providers/anineko/vibeplayer.go +2 −2
| @@ -6,7 +6,7 @@ import ( | ||
| 6 | 6 | "strings" |
| 7 | 7 | ) |
| 8 | 8 | |
| 9 | var vibeplayerMasterRE = regexp.MustCompile(`const src = "(https://vibeplayer\.site/public/stream/[^"]+/master\.m3u8)"`) | |
| 9 | var vibeplayerMasterRE = regexp.MustCompile(`const src = "(https?://[^"]+/public/stream/[^"]+/master\.m3u8)"`) | |
| 10 | 10 | |
| 11 | 11 | func resolveVibeplayer(embedURL string) (resolvedStream, error) { |
| 12 | 12 | embedURL = strings.TrimSpace(embedURL) |
| @@ -32,6 +32,6 @@ func resolveVibeplayer(embedURL string) (resolvedStream, error) { | ||
| 32 | 32 | return resolvedStream{ |
| 33 | 33 | URL: proxyURL, |
| 34 | 34 | Referrer: embedURL, |
| 35 | Subtitle: subtitleFromEmbedURL(embedURL), | |
| 35 | Subtitle: resolveSubtitle(embedURL, html), | |
| 36 | 36 | }, nil |
| 37 | 37 | } |
internal/providers/anipub/streams.go +6
| @@ -4,6 +4,7 @@ import ( | ||
| 4 | 4 | "fmt" |
| 5 | 5 | |
| 6 | 6 | "github.com/wraient/curd/internal/providers" |
| 7 | "github.com/wraient/curd/internal/providers/substyle" | |
| 7 | 8 | ) |
| 8 | 9 | |
| 9 | 10 | func getEpisodeStreamsForMode(showID string, config providers.PlaybackConfig, epNo int) ([]string, map[string]providers.StreamPlaybackHint, error) { |
| @@ -26,6 +27,11 @@ func getEpisodeStreamsForMode(showID string, config providers.PlaybackConfig, ep | ||
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | mode := providers.NormalizeTranslationType(config.SubOrDub) |
| 30 | if mode == "sub" { | |
| 31 | if _, err := substyle.Choose(true, false, config.SubStyle); err != nil { | |
| 32 | return nil, nil, err | |
| 33 | } | |
| 34 | } | |
| 29 | 35 | streamURL, subtitle, err := resolveMegaplayStream(videoLink, mode) |
| 30 | 36 | if err != nil { |
| 31 | 37 | return nil, nil, err |
internal/providers/senshi/provider_test.go +97
| @@ -139,4 +139,101 @@ func TestGetEpisodeStreamsForModeSelectsHardSub(t *testing.T) { | ||
| 139 | 139 | if hints[links[0]].Referrer != baseURL+"/" { |
| 140 | 140 | t.Fatalf("unexpected referrer %q", hints[links[0]].Referrer) |
| 141 | 141 | } |
| 142 | if hints[links[0]].Subtitle != "" { | |
| 143 | t.Fatalf("expected no external subtitle for hard sub, got %q", hints[links[0]].Subtitle) | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | func TestGetEpisodeStreamsForModeLoadsSoftSubtitles(t *testing.T) { | |
| 148 | const subtitleURL = "https://cdn.example/sub_2_eng.vtt" | |
| 149 | const manifestPath = "/sub_filemoon.json" | |
| 150 | ||
| 151 | var server *httptest.Server | |
| 152 | server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 153 | switch { | |
| 154 | case r.URL.Path == "/episode-embeds/62435/1": | |
| 155 | manifestURL := server.URL + manifestPath | |
| 156 | serverFM := "https://embed.example/e/abc/?sub.info=" + manifestURL | |
| 157 | _ = json.NewEncoder(w).Encode([]embedItem{{ | |
| 158 | URL: "https://cdn.example/soft.m3u8", | |
| 159 | ServerFM: strPtr(serverFM), | |
| 160 | Status: "HardSub", | |
| 161 | MaskedBaseURL: "https://ninstream.com/example/base", | |
| 162 | }}) | |
| 163 | case r.URL.Path == manifestPath: | |
| 164 | _ = json.NewEncoder(w).Encode([]senshiSubtitleTrack{{ | |
| 165 | Src: subtitleURL, | |
| 166 | Label: "ENG", | |
| 167 | Default: true, | |
| 168 | }}) | |
| 169 | default: | |
| 170 | http.NotFound(w, r) | |
| 171 | } | |
| 172 | })) | |
| 173 | defer server.Close() | |
| 174 | ||
| 175 | withSenshiTestClient(t, server.Client()) | |
| 176 | originalBase := baseURL | |
| 177 | t.Cleanup(func() { baseURL = originalBase }) | |
| 178 | baseURL = server.URL | |
| 179 | ||
| 180 | links, hints, err := getEpisodeStreamsForMode("62435", providers.PlaybackConfig{SubOrDub: "sub"}, 1) | |
| 181 | if err != nil { | |
| 182 | t.Fatalf("getEpisodeStreamsForMode: %v", err) | |
| 183 | } | |
| 184 | if len(links) != 1 || links[0] != "https://cdn.example/soft.m3u8" { | |
| 185 | t.Fatalf("unexpected links %#v", links) | |
| 186 | } | |
| 187 | if hints[links[0]].Subtitle != subtitleURL { | |
| 188 | t.Fatalf("unexpected subtitle %q", hints[links[0]].Subtitle) | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | func TestGetEpisodeStreamsForModeHardStillInjectsSenshiSubs(t *testing.T) { | |
| 193 | const subtitleURL = "https://cdn.example/sub_2_eng.vtt" | |
| 194 | const manifestPath = "/sub_filemoon.json" | |
| 195 | ||
| 196 | var server *httptest.Server | |
| 197 | server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 198 | switch { | |
| 199 | case r.URL.Path == "/episode-embeds/62435/1": | |
| 200 | manifestURL := server.URL + manifestPath | |
| 201 | serverFM := "https://embed.example/e/abc/?sub.info=" + manifestURL | |
| 202 | _ = json.NewEncoder(w).Encode([]embedItem{{ | |
| 203 | URL: "https://cdn.example/soft.m3u8", | |
| 204 | ServerFM: strPtr(serverFM), | |
| 205 | Status: "HardSub", | |
| 206 | MaskedBaseURL: "https://ninstream.com/example/base", | |
| 207 | }}) | |
| 208 | case r.URL.Path == manifestPath: | |
| 209 | _ = json.NewEncoder(w).Encode([]senshiSubtitleTrack{{ | |
| 210 | Src: subtitleURL, | |
| 211 | Label: "ENG", | |
| 212 | Default: true, | |
| 213 | }}) | |
| 214 | default: | |
| 215 | http.NotFound(w, r) | |
| 216 | } | |
| 217 | })) | |
| 218 | defer server.Close() | |
| 219 | ||
| 220 | withSenshiTestClient(t, server.Client()) | |
| 221 | originalBase := baseURL | |
| 222 | t.Cleanup(func() { baseURL = originalBase }) | |
| 223 | baseURL = server.URL | |
| 224 | ||
| 225 | links, hints, err := getEpisodeStreamsForMode("62435", providers.PlaybackConfig{SubOrDub: "sub", SubStyle: "hard"}, 1) | |
| 226 | if err != nil { | |
| 227 | t.Fatalf("getEpisodeStreamsForMode: %v", err) | |
| 228 | } | |
| 229 | if len(links) != 1 { | |
| 230 | t.Fatalf("unexpected links %#v", links) | |
| 231 | } | |
| 232 | if hints[links[0]].Subtitle != subtitleURL { | |
| 233 | t.Fatalf("expected senshi external subs even with hard preference, got %q", hints[links[0]].Subtitle) | |
| 234 | } | |
| 235 | } | |
| 236 | ||
| 237 | func strPtr(value string) *string { | |
| 238 | return &value | |
| 142 | 239 | } |
internal/providers/senshi/streams.go +13 −4
| @@ -18,10 +18,6 @@ func getEpisodeStreamsForMode(malIDStr string, config providers.PlaybackConfig, | ||
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | mode := providers.NormalizeTranslationType(config.SubOrDub) |
| 21 | wantStatus := "HardSub" | |
| 22 | if mode == "dub" { | |
| 23 | wantStatus = "Dub" | |
| 24 | } | |
| 25 | 21 | |
| 26 | 22 | var embeds []embedItem |
| 27 | 23 | url := fmt.Sprintf("%s/episode-embeds/%d/%d", baseURL, malID, epNo) |
| @@ -32,6 +28,11 @@ func getEpisodeStreamsForMode(malIDStr string, config providers.PlaybackConfig, | ||
| 32 | 28 | return nil, nil, fmt.Errorf("no streams found for episode %d", epNo) |
| 33 | 29 | } |
| 34 | 30 | |
| 31 | wantStatus := "HardSub" | |
| 32 | if mode == "dub" { | |
| 33 | wantStatus = "Dub" | |
| 34 | } | |
| 35 | ||
| 35 | 36 | for _, item := range embeds { |
| 36 | 37 | if !strings.EqualFold(strings.TrimSpace(item.Status), wantStatus) { |
| 37 | 38 | continue |
| @@ -40,9 +41,17 @@ func getEpisodeStreamsForMode(malIDStr string, config providers.PlaybackConfig, | ||
| 40 | 41 | if streamURL == "" { |
| 41 | 42 | continue |
| 42 | 43 | } |
| 44 | ||
| 45 | subtitle := "" | |
| 46 | if mode == "sub" { | |
| 47 | // Senshi labels streams HardSub even when subtitles are external. | |
| 48 | subtitle = resolveSenshiSubtitle(item) | |
| 49 | } | |
| 50 | ||
| 43 | 51 | hints := map[string]providers.StreamPlaybackHint{ |
| 44 | 52 | streamURL: { |
| 45 | 53 | Referrer: baseURL + "/", |
| 54 | Subtitle: subtitle, | |
| 46 | 55 | }, |
| 47 | 56 | } |
| 48 | 57 | return []string{streamURL}, hints, nil |
internal/providers/senshi/subtitles.go added +89
| @@ -0,0 +1,89 @@ | ||
| 1 | package senshi | |
| 2 | ||
| 3 | import ( | |
| 4 | "net/http" | |
| 5 | "net/url" | |
| 6 | "strings" | |
| 7 | ) | |
| 8 | ||
| 9 | type senshiSubtitleTrack struct { | |
| 10 | Src string `json:"src"` | |
| 11 | Label string `json:"label"` | |
| 12 | Default bool `json:"default"` | |
| 13 | } | |
| 14 | ||
| 15 | func senshiSubtitleManifestURL(item embedItem) string { | |
| 16 | if item.ServerFM != nil { | |
| 17 | if manifest := subtitleInfoFromURL(strings.TrimSpace(*item.ServerFM)); manifest != "" { | |
| 18 | return manifest | |
| 19 | } | |
| 20 | } | |
| 21 | base := strings.TrimSpace(item.MaskedBaseURL) | |
| 22 | if base == "" { | |
| 23 | return "" | |
| 24 | } | |
| 25 | return strings.TrimRight(base, "/") + "/sub_filemoon.json" | |
| 26 | } | |
| 27 | ||
| 28 | func subtitleInfoFromURL(rawURL string) string { | |
| 29 | if rawURL == "" { | |
| 30 | return "" | |
| 31 | } | |
| 32 | parsed, err := url.Parse(rawURL) | |
| 33 | if err != nil { | |
| 34 | return "" | |
| 35 | } | |
| 36 | return strings.TrimSpace(parsed.Query().Get("sub.info")) | |
| 37 | } | |
| 38 | ||
| 39 | func fetchSenshiSubtitle(manifestURL string) (string, error) { | |
| 40 | manifestURL = strings.TrimSpace(manifestURL) | |
| 41 | if manifestURL == "" { | |
| 42 | return "", nil | |
| 43 | } | |
| 44 | ||
| 45 | var tracks []senshiSubtitleTrack | |
| 46 | if err := fetchJSON(http.MethodGet, manifestURL, nil, &tracks); err != nil { | |
| 47 | return "", err | |
| 48 | } | |
| 49 | return pickSenshiSubtitleTrack(tracks), nil | |
| 50 | } | |
| 51 | ||
| 52 | func pickSenshiSubtitleTrack(tracks []senshiSubtitleTrack) string { | |
| 53 | var fallback string | |
| 54 | for _, track := range tracks { | |
| 55 | file := strings.TrimSpace(track.Src) | |
| 56 | if file == "" { | |
| 57 | continue | |
| 58 | } | |
| 59 | label := strings.ToLower(strings.TrimSpace(track.Label)) | |
| 60 | if track.Default || strings.Contains(label, "eng") { | |
| 61 | return file | |
| 62 | } | |
| 63 | if fallback == "" { | |
| 64 | fallback = file | |
| 65 | } | |
| 66 | } | |
| 67 | return fallback | |
| 68 | } | |
| 69 | ||
| 70 | func resolveSenshiSubtitle(item embedItem) string { | |
| 71 | manifestURL := senshiSubtitleManifestURL(item) | |
| 72 | if manifestURL == "" { | |
| 73 | return "" | |
| 74 | } | |
| 75 | subtitle, err := fetchSenshiSubtitle(manifestURL) | |
| 76 | if err != nil { | |
| 77 | return "" | |
| 78 | } | |
| 79 | return subtitle | |
| 80 | } | |
| 81 | ||
| 82 | func isSubEmbedStatus(status string) bool { | |
| 83 | switch strings.ToLower(strings.TrimSpace(status)) { | |
| 84 | case "hardsub", "softsub", "sub": | |
| 85 | return true | |
| 86 | default: | |
| 87 | return false | |
| 88 | } | |
| 89 | } | |
internal/providers/senshi/subtitles_test.go added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | package senshi | |
| 2 | ||
| 3 | import "testing" | |
| 4 | ||
| 5 | func TestSubtitleInfoFromURL(t *testing.T) { | |
| 6 | rawURL := "https://embed.example/e/abc/?sub.info=https%3A%2F%2Fninstream.com%2Fmanifest.json" | |
| 7 | if got := subtitleInfoFromURL(rawURL); got != "https://ninstream.com/manifest.json" { | |
| 8 | t.Fatalf("unexpected manifest url %q", got) | |
| 9 | } | |
| 10 | } | |
| 11 | ||
| 12 | func TestPickSenshiSubtitleTrackPrefersEnglishDefault(t *testing.T) { | |
| 13 | tracks := []senshiSubtitleTrack{ | |
| 14 | {Src: "https://cdn.example/jpn.vtt", Label: "JPN"}, | |
| 15 | {Src: "https://cdn.example/eng.vtt", Label: "ENG", Default: true}, | |
| 16 | } | |
| 17 | if got := pickSenshiSubtitleTrack(tracks); got != "https://cdn.example/eng.vtt" { | |
| 18 | t.Fatalf("unexpected subtitle %q", got) | |
| 19 | } | |
| 20 | } | |
| 21 | ||
| 22 | func TestSenshiSubtitleManifestURLUsesServerFM(t *testing.T) { | |
| 23 | serverFM := "https://embed.example/e/abc/?sub.info=https://ninstream.com/manifest.json" | |
| 24 | item := embedItem{ServerFM: &serverFM} | |
| 25 | if got := senshiSubtitleManifestURL(item); got != "https://ninstream.com/manifest.json" { | |
| 26 | t.Fatalf("unexpected manifest url %q", got) | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | func TestSenshiSubtitleManifestURLFallsBackToMaskedBase(t *testing.T) { | |
| 31 | item := embedItem{MaskedBaseURL: "https://ninstream.com/example/base"} | |
| 32 | if got := senshiSubtitleManifestURL(item); got != "https://ninstream.com/example/base/sub_filemoon.json" { | |
| 33 | t.Fatalf("unexpected manifest url %q", got) | |
| 34 | } | |
| 35 | } | |
internal/providers/senshi/types.go +5 −4
| @@ -40,8 +40,9 @@ type episodeItem struct { | ||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | type embedItem struct { |
| 43 | URL string `json:"url"` | |
| 44 | Server2 *string `json:"server2"` | |
| 45 | ServerFM *string `json:"serverFM"` | |
| 46 | Status string `json:"status"` | |
| 43 | URL string `json:"url"` | |
| 44 | Server2 *string `json:"server2"` | |
| 45 | ServerFM *string `json:"serverFM"` | |
| 46 | Status string `json:"status"` | |
| 47 | MaskedBaseURL string `json:"masked_base_url"` | |
| 47 | 48 | } |
internal/providers/substyle/substyle.go added +155
| @@ -0,0 +1,155 @@ | ||
| 1 | package substyle | |
| 2 | ||
| 3 | import ( | |
| 4 | "fmt" | |
| 5 | "strings" | |
| 6 | "sync" | |
| 7 | ||
| 8 | "github.com/wraient/curd/internal/curdhost" | |
| 9 | ) | |
| 10 | ||
| 11 | var ( | |
| 12 | choiceMu sync.Mutex | |
| 13 | chosen string | |
| 14 | ) | |
| 15 | ||
| 16 | func Normalize(style string) string { | |
| 17 | switch strings.ToLower(strings.TrimSpace(style)) { | |
| 18 | case "soft": | |
| 19 | return "soft" | |
| 20 | case "hard": | |
| 21 | return "hard" | |
| 22 | default: | |
| 23 | return "ask" | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | func resolvePreference(fallback string) string { | |
| 28 | if curdhost.CurrentSubStyle != nil { | |
| 29 | if live := Normalize(curdhost.CurrentSubStyle()); live != "ask" { | |
| 30 | return live | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | choiceMu.Lock() | |
| 35 | defer choiceMu.Unlock() | |
| 36 | if chosen != "" { | |
| 37 | return chosen | |
| 38 | } | |
| 39 | return Normalize(fallback) | |
| 40 | } | |
| 41 | ||
| 42 | func rememberChoice(style string) { | |
| 43 | style = Normalize(style) | |
| 44 | if style != "soft" && style != "hard" { | |
| 45 | return | |
| 46 | } | |
| 47 | ||
| 48 | choiceMu.Lock() | |
| 49 | chosen = style | |
| 50 | choiceMu.Unlock() | |
| 51 | ||
| 52 | if curdhost.PersistSubStylePreference != nil { | |
| 53 | _ = curdhost.PersistSubStylePreference(style) | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | // Choose picks soft or hard subtitle delivery for sub-mode playback. | |
| 58 | func Choose(hasSoft, hasHard bool, preference string) (string, error) { | |
| 59 | preference = resolvePreference(preference) | |
| 60 | ||
| 61 | switch preference { | |
| 62 | case "soft": | |
| 63 | if hasSoft { | |
| 64 | return "soft", nil | |
| 65 | } | |
| 66 | if hasHard { | |
| 67 | return "hard", nil | |
| 68 | } | |
| 69 | case "hard": | |
| 70 | if hasHard { | |
| 71 | return "hard", nil | |
| 72 | } | |
| 73 | if hasSoft { | |
| 74 | return promptSoftFallback() | |
| 75 | } | |
| 76 | case "ask": | |
| 77 | if hasSoft && hasHard { | |
| 78 | return promptBoth() | |
| 79 | } | |
| 80 | if hasSoft { | |
| 81 | if curdhost.Out != nil { | |
| 82 | curdhost.Out("Using soft subs with external subtitles.") | |
| 83 | } | |
| 84 | rememberChoice("soft") | |
| 85 | return "soft", nil | |
| 86 | } | |
| 87 | if hasHard { | |
| 88 | return "hard", nil | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | return "", fmt.Errorf("no sub streams found") | |
| 93 | } | |
| 94 | ||
| 95 | func promptBoth() (string, error) { | |
| 96 | if curdhost.PromptSelect == nil { | |
| 97 | rememberChoice("soft") | |
| 98 | return "soft", nil | |
| 99 | } | |
| 100 | if curdhost.Out != nil { | |
| 101 | curdhost.Out("Both soft-sub and hard-sub streams are available.") | |
| 102 | } | |
| 103 | selected, err := curdhost.PromptSelect([]curdhost.PromptOption{ | |
| 104 | {Key: "soft", Label: "Soft sub (external subtitles)"}, | |
| 105 | {Key: "hard", Label: "Hard sub (burned-in subtitles)"}, | |
| 106 | }) | |
| 107 | if err != nil { | |
| 108 | return "", err | |
| 109 | } | |
| 110 | chosenStyle := "soft" | |
| 111 | if selected.Key == "hard" { | |
| 112 | chosenStyle = "hard" | |
| 113 | } | |
| 114 | rememberChoice(chosenStyle) | |
| 115 | return chosenStyle, nil | |
| 116 | } | |
| 117 | ||
| 118 | func promptSoftFallback() (string, error) { | |
| 119 | if curdhost.PromptSelect == nil { | |
| 120 | rememberChoice("soft") | |
| 121 | return "soft", nil | |
| 122 | } | |
| 123 | if curdhost.Out != nil { | |
| 124 | curdhost.Out("Only soft subs with external subtitles are available.") | |
| 125 | } | |
| 126 | selected, err := curdhost.PromptSelect([]curdhost.PromptOption{ | |
| 127 | {Key: "soft", Label: "Use soft subs (external subtitles)"}, | |
| 128 | {Key: "cancel", Label: "Cancel"}, | |
| 129 | }) | |
| 130 | if err != nil { | |
| 131 | return "", err | |
| 132 | } | |
| 133 | if selected.Key != "soft" { | |
| 134 | return "", fmt.Errorf("no hard-sub streams found") | |
| 135 | } | |
| 136 | rememberChoice("soft") | |
| 137 | return "soft", nil | |
| 138 | } | |
| 139 | ||
| 140 | // HardOnly reports whether playback should restrict to burned-in subtitles. | |
| 141 | func HardOnly(preference string) bool { | |
| 142 | return resolvePreference(preference) == "hard" | |
| 143 | } | |
| 144 | ||
| 145 | // SoftOnly reports whether playback should restrict to external subtitles. | |
| 146 | func SoftOnly(preference string) bool { | |
| 147 | return resolvePreference(preference) == "soft" | |
| 148 | } | |
| 149 | ||
| 150 | // ResetForTest clears the in-session subtitle style choice. | |
| 151 | func ResetForTest() { | |
| 152 | choiceMu.Lock() | |
| 153 | chosen = "" | |
| 154 | choiceMu.Unlock() | |
| 155 | } | |
internal/providers/substyle/substyle_test.go added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | package substyle_test | |
| 2 | ||
| 3 | import ( | |
| 4 | "testing" | |
| 5 | ||
| 6 | "github.com/wraient/curd/internal/curdhost" | |
| 7 | "github.com/wraient/curd/internal/providers/substyle" | |
| 8 | ) | |
| 9 | ||
| 10 | func TestChooseHardPromptsForSoftFallback(t *testing.T) { | |
| 11 | substyle.ResetForTest() | |
| 12 | prompts := 0 | |
| 13 | previousPrompt := curdhost.PromptSelect | |
| 14 | curdhost.Out = func(string) {} | |
| 15 | curdhost.PromptSelect = func(options []curdhost.PromptOption) (curdhost.PromptOption, error) { | |
| 16 | prompts++ | |
| 17 | return curdhost.PromptOption{Key: "soft", Label: options[0].Label}, nil | |
| 18 | } | |
| 19 | t.Cleanup(func() { | |
| 20 | curdhost.PromptSelect = previousPrompt | |
| 21 | substyle.ResetForTest() | |
| 22 | }) | |
| 23 | ||
| 24 | style, err := substyle.Choose(true, false, "hard") | |
| 25 | if err != nil || style != "soft" { | |
| 26 | t.Fatalf("expected soft after prompt, got style=%q err=%v", style, err) | |
| 27 | } | |
| 28 | if prompts != 1 { | |
| 29 | t.Fatalf("expected 1 prompt, got %d", prompts) | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | func TestChooseHardDeclinedSoftFallback(t *testing.T) { | |
| 34 | substyle.ResetForTest() | |
| 35 | previousPrompt := curdhost.PromptSelect | |
| 36 | curdhost.PromptSelect = func([]curdhost.PromptOption) (curdhost.PromptOption, error) { | |
| 37 | return curdhost.PromptOption{Key: "cancel", Label: "Cancel"}, nil | |
| 38 | } | |
| 39 | t.Cleanup(func() { | |
| 40 | curdhost.PromptSelect = previousPrompt | |
| 41 | substyle.ResetForTest() | |
| 42 | }) | |
| 43 | ||
| 44 | _, err := substyle.Choose(true, false, "hard") | |
| 45 | if err == nil { | |
| 46 | t.Fatal("expected error when soft fallback is declined") | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | func TestChooseSoftFallsBackToHard(t *testing.T) { | |
| 51 | style, err := substyle.Choose(false, true, "soft") | |
| 52 | if err != nil || style != "hard" { | |
| 53 | t.Fatalf("expected hard fallback, got style=%q err=%v", style, err) | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | func TestChooseAskAutoUsesSoftOnly(t *testing.T) { | |
| 58 | substyle.ResetForTest() | |
| 59 | messages := 0 | |
| 60 | previousOut := curdhost.Out | |
| 61 | curdhost.Out = func(string) { messages++ } | |
| 62 | t.Cleanup(func() { | |
| 63 | curdhost.Out = previousOut | |
| 64 | substyle.ResetForTest() | |
| 65 | }) | |
| 66 | ||
| 67 | style, err := substyle.Choose(true, false, "ask") | |
| 68 | if err != nil || style != "soft" { | |
| 69 | t.Fatalf("expected soft, got style=%q err=%v", style, err) | |
| 70 | } | |
| 71 | if messages != 1 { | |
| 72 | t.Fatalf("expected status message, got %d", messages) | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | func TestChooseAskDeclinedSoftOnly(t *testing.T) { | |
| 77 | t.Skip("ask mode auto-accepts soft-only streams") | |
| 78 | } | |
internal/providers/types.go +1 −1
| @@ -12,7 +12,7 @@ type SelectionOption struct { | ||
| 12 | 12 | // PlaybackConfig carries playback preferences passed into providers. |
| 13 | 13 | type PlaybackConfig struct { |
| 14 | 14 | SubOrDub string |
| 15 | // SubStyle controls anineko subtitle stream tabs: ask, soft, or hard. | |
| 15 | // SubStyle controls subtitle delivery for sub-mode playback: ask, soft, or hard. | |
| 16 | 16 | SubStyle string |
| 17 | 17 | } |
| 18 | 18 | |