internal/providers/anineko/substyle.go
138 lines · 2832 bytes
1package anineko
2
3import (
4 "fmt"
5 "strings"
6 "sync"
7
8 "github.com/wraient/curd/internal/curdhost"
9)
10
11var (
12 subStyleChoiceMu sync.Mutex
13 subStyleChosen string
14)
15
16func 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
27func 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
42func 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
57func chooseSubStyle(groups map[string][]string, preference string) (string, error) {
58 preference = resolveSubStylePreference(preference)
59 hasSoft := len(groups["sub"]) > 0
60 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")
109}
110
111func embedURLsForSubStyle(groups map[string][]string, style string) []string {
112 switch style {
113 case "soft":
114 return append([]string{}, groups["sub"]...)
115 case "hard":
116 return append([]string{}, groups["hsub"]...)
117 default:
118 return nil
119 }
120}
121
122func embedURLsForMode(groups map[string][]string, mode, subStyle string) ([]string, error) {
123 if strings.EqualFold(mode, "dub") {
124 return groups["dub"], nil
125 }
126
127 chosen, err := chooseSubStyle(groups, subStyle)
128 if err != nil {
129 return nil, err
130 }
131 return embedURLsForSubStyle(groups, chosen), nil
132}
133
134func resetSubStyleForTest() {
135 subStyleChoiceMu.Lock()
136 subStyleChosen = ""
137 subStyleChoiceMu.Unlock()
138}