internal/providers/senshi/search.go

116 lines · 2804 bytes

  1package senshi
  2
  3import (
  4	"fmt"
  5	"net/http"
  6	"strconv"
  7	"strings"
  8
  9	"github.com/wraient/curd/internal/providers"
 10)
 11
 12func searchAnime(query, mode string) ([]providers.SelectionOption, error) {
 13	query = strings.TrimSpace(query)
 14	if query == "" {
 15		return nil, fmt.Errorf("empty search query")
 16	}
 17	_ = mode
 18
 19	var payload filterResponse
 20	err := fetchJSON(http.MethodPost, baseURL+"/anime/filter", map[string]any{
 21		"searchTerm":         query,
 22		"types":              []string{},
 23		"genres":             []string{},
 24		"status":             []string{},
 25		"seasons":            []string{},
 26		"year":               "",
 27		"studios":            []string{},
 28		"producers":          []string{},
 29		"languages":          []string{},
 30		"page":               1,
 31		"limit":              30,
 32		"sortBy":             "score_desc",
 33		"languagePreference": "EN",
 34	}, &payload)
 35	if err != nil {
 36		return nil, err
 37	}
 38	if len(payload.Data) == 0 {
 39		return nil, fmt.Errorf("no results for %q", query)
 40	}
 41
 42	options := make([]providers.SelectionOption, 0, len(payload.Data))
 43	for _, item := range payload.Data {
 44		malID := item.ID
 45		if malID <= 0 {
 46			continue
 47		}
 48		title := strings.TrimSpace(item.TitleEnglish)
 49		if title == "" {
 50			title = strings.TrimSpace(item.Title)
 51		}
 52		label := formatSearchLabel(item)
 53		options = append(options, providers.SelectionOption{
 54			Key:       strconv.Itoa(malID),
 55			Label:     label,
 56			Title:     title,
 57			Thumbnail: searchThumbnail(item),
 58			ExtraData: toSearchItem(item),
 59		})
 60	}
 61	if len(options) == 0 {
 62		return nil, fmt.Errorf("no results for %q", query)
 63	}
 64	return options, nil
 65}
 66
 67func searchThumbnail(item animeItem) string {
 68	if thumbnail := absoluteURL(item.AnimePicture); thumbnail != "" {
 69		return thumbnail
 70	}
 71	return posterURL(item.ID)
 72}
 73
 74func formatSearchLabel(item animeItem) string {
 75	title := strings.TrimSpace(item.TitleEnglish)
 76	if title == "" {
 77		title = strings.TrimSpace(item.Title)
 78	}
 79	parts := []string{title}
 80	if item.Type != "" {
 81		parts = append(parts, item.Type)
 82	}
 83	if item.AniYear > 0 {
 84		parts = append(parts, strconv.Itoa(item.AniYear))
 85	}
 86	if episodes, ok := parseEpisodeCount(item.AniEpisodes); ok {
 87		parts = append(parts, fmt.Sprintf("%d eps", episodes))
 88	}
 89	return strings.Join(parts, " · ")
 90}
 91
 92func toSearchItem(item animeItem) SearchItem {
 93	episodes, _ := parseEpisodeCount(item.AniEpisodes)
 94	return SearchItem{
 95		MalID:    item.ID,
 96		PublicID: item.PublicID,
 97		Title:    item.Title,
 98		Type:     item.Type,
 99		Episodes: episodes,
100		Year:     item.AniYear,
101		Score:    item.Score,
102		Status:   item.AniStatus,
103	}
104}
105
106func parseEpisodeCount(raw string) (int, bool) {
107	raw = strings.TrimSpace(raw)
108	if raw == "" || raw == "?" {
109		return 0, false
110	}
111	count, err := strconv.Atoi(raw)
112	if err != nil || count <= 0 {
113		return 0, false
114	}
115	return count, true
116}