internal/selection_labels.go
207 lines · 5514 bytes
1package internal
2
3import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "strings"
8
9 "github.com/wraient/curd/internal/providers/animepahe"
10 "github.com/wraient/curd/internal/providers/anipub"
11 "github.com/wraient/curd/internal/providers/senshi"
12)
13
14var selectionEpisodeCountRE = regexp.MustCompile(`\((\d+)\s+episodes?\)`)
15
16func FormatAnimeSearchLabel(title string, episodes int) string {
17 title = strings.TrimSpace(title)
18 if title == "" {
19 return ""
20 }
21 if episodes > 0 {
22 return fmt.Sprintf("%s (%d episodes)", title, episodes)
23 }
24 return title
25}
26
27func TrackerEpisodeHint(config *CurdConfig, entry *Entry) string {
28 if entry == nil || entry.Media.Episodes <= 0 {
29 return ""
30 }
31 tracker := RemoteTrackingDisplayName(config)
32 title := mediaDisplayTitle(entry.Media, config)
33 if title == "" {
34 return fmt.Sprintf("Your %s entry has %d episodes — pick the matching title below.", tracker, entry.Media.Episodes)
35 }
36 return fmt.Sprintf("Your %s entry for %q has %d episodes — pick the matching title below.", tracker, title, entry.Media.Episodes)
37}
38
39func episodeCountFromSelectionOption(option SelectionOption) (int, bool) {
40 for _, source := range []string{option.Label, option.Title} {
41 if source == "" {
42 continue
43 }
44 if matches := selectionEpisodeCountRE.FindStringSubmatch(source); len(matches) > 1 {
45 if count, err := strconv.Atoi(matches[1]); err == nil && count > 0 {
46 return count, true
47 }
48 }
49 }
50
51 if option.ExtraData != nil {
52 if item, ok := option.ExtraData.(animepahe.SearchItem); ok && item.Episodes > 0 {
53 return item.Episodes, true
54 }
55 if item, ok := option.ExtraData.(senshi.SearchItem); ok && item.Episodes > 0 {
56 return item.Episodes, true
57 }
58 if item, ok := option.ExtraData.(anipub.SearchItem); ok && item.Episodes > 0 {
59 return item.Episodes, true
60 }
61 }
62
63 return 0, false
64}
65
66func selectionOptionBaseTitle(option SelectionOption) string {
67 if option.Title != "" {
68 return strings.TrimSpace(option.Title)
69 }
70
71 label := strings.TrimSpace(option.Label)
72 if label == "" {
73 return ""
74 }
75 if idx := strings.Index(label, " ("); idx > 0 {
76 return strings.TrimSpace(label[:idx])
77 }
78 if idx := strings.Index(label, " — "); idx > 0 {
79 return strings.TrimSpace(label[:idx])
80 }
81 if idx := strings.Index(label, " ["); idx > 0 {
82 return strings.TrimSpace(label[:idx])
83 }
84 return label
85}
86
87func ensureEpisodeCountInLabel(option SelectionOption) SelectionOption {
88 if selectionEpisodeCountRE.MatchString(option.Label) {
89 return option
90 }
91
92 count, ok := episodeCountFromSelectionOption(option)
93 if !ok || count <= 0 {
94 return option
95 }
96
97 baseTitle := selectionOptionBaseTitle(option)
98 if baseTitle == "" {
99 return option
100 }
101
102 providerSuffix := ""
103 if idx := strings.LastIndex(option.Label, " ["); idx > 0 && strings.HasSuffix(option.Label, "]") {
104 providerSuffix = option.Label[idx:]
105 }
106
107 option.Label = FormatAnimeSearchLabel(baseTitle, count) + providerSuffix
108 if option.Title == "" {
109 option.Title = baseTitle
110 }
111 return option
112}
113
114func ProviderSearchOptionsForDisplay(options []SelectionOption, entry *Entry) []SelectionOption {
115 if len(options) == 0 {
116 return options
117 }
118
119 targetEpisodes := 0
120 if entry != nil {
121 targetEpisodes = entry.Media.Episodes
122 }
123
124 display := make([]SelectionOption, len(options))
125 for i, option := range options {
126 display[i] = ensureEpisodeCountInLabel(option)
127 if targetEpisodes <= 0 {
128 continue
129 }
130 if count, ok := episodeCountFromSelectionOption(display[i]); ok && count == targetEpisodes {
131 display[i].Label = display[i].Label + " ✓"
132 }
133 }
134 return display
135}
136
137func ManualProviderSearchEnabled(config *CurdConfig) bool {
138 return config != nil && config.ManualProviderSearch
139}
140
141func displayMediaFormat(format string) string {
142 format = strings.TrimSpace(strings.ToUpper(format))
143 switch format {
144 case "TV":
145 return "TV"
146 case "TV_SHORT":
147 return "TV Short"
148 case "MOVIE":
149 return "Movie"
150 case "OVA":
151 return "OVA"
152 case "ONA":
153 return "ONA"
154 case "SPECIAL":
155 return "Special"
156 case "MUSIC":
157 return "Music"
158 default:
159 return strings.ReplaceAll(format, "_", " ")
160 }
161}
162
163func formatEpisodeCountHint(episodes int) string {
164 if episodes > 0 {
165 return fmt.Sprintf("%d episodes", episodes)
166 }
167 return "unknown episode count"
168}
169
170func ManualProviderSearchHint(config *CurdConfig, entry *Entry, query, subOrDub string) string {
171 title := strings.TrimSpace(query)
172 mediaType := ""
173 episodes := 0
174
175 if entry != nil {
176 if trackerTitle := mediaDisplayTitle(entry.Media, config); trackerTitle != "" {
177 title = trackerTitle
178 }
179 mediaType = displayMediaFormat(entry.Media.Format)
180 episodes = entry.Media.Episodes
181 }
182 if title == "" {
183 title = "unknown title"
184 }
185
186 mode := strings.ToLower(normalizeTranslationType(subOrDub))
187 parts := []string{fmt.Sprintf("Searching for %q", title)}
188 if mediaType != "" {
189 parts = append(parts, mediaType)
190 }
191 parts = append(parts, formatEpisodeCountHint(episodes))
192 if mode != "" {
193 parts = append(parts, mode)
194 }
195 return strings.Join(parts, " · ") + " — select the matching provider entry below."
196}
197
198func promptProviderSearchSelectionWithHint(config *CurdConfig, options []SelectionOption, entry *Entry, hint string) (SelectionOption, error) {
199 if strings.TrimSpace(hint) != "" {
200 CurdOut(hint)
201 }
202 return DynamicSelect(ProviderSearchOptionsForDisplay(options, entry))
203}
204
205func promptProviderSearchSelection(config *CurdConfig, options []SelectionOption, entry *Entry) (SelectionOption, error) {
206 return promptProviderSearchSelectionWithHint(config, options, entry, TrackerEpisodeHint(config, entry))
207}