internal/providers/anipub/episodes.go
62 lines · 1455 bytes
4 symbols in this file
1package anipub
2
3import (
4 "fmt"
5 "strconv"
6 "strings"
7)
8
9func episodesList(showID, mode string) ([]string, error) {
10 showID, err := parseShowID(showID)
11 if err != nil {
12 return nil, err
13 }
14 _ = mode
15
16 var details detailsResponse
17 if err := fetchJSON(detailsURL(showID), baseURL+"/", &details); err != nil {
18 return nil, err
19 }
20
21 count := len(details.Local.Ep) + 1
22 if count <= 0 {
23 return nil, fmt.Errorf("no episodes found for show %s", showID)
24 }
25
26 episodes := make([]string, 0, count)
27 for epNo := 1; epNo <= count; epNo++ {
28 episodes = append(episodes, strconv.Itoa(epNo))
29 }
30 return episodes, nil
31}
32
33func episodeVideoLink(details detailsResponse, epNo int) (string, error) {
34 if epNo <= 0 {
35 return "", fmt.Errorf("invalid episode number %d", epNo)
36 }
37 if epNo == 1 {
38 return normalizeEpisodeLink(details.Local.Link), nil
39 }
40 idx := epNo - 2
41 if idx < 0 || idx >= len(details.Local.Ep) {
42 return "", fmt.Errorf("episode %d not found", epNo)
43 }
44 return normalizeEpisodeLink(details.Local.Ep[idx].Link), nil
45}
46
47func normalizeEpisodeLink(raw string) string {
48 raw = strings.TrimSpace(raw)
49 raw = strings.TrimPrefix(raw, "src=")
50 return strings.TrimSpace(raw)
51}
52
53func parseShowID(showID string) (string, error) {
54 showID = strings.TrimSpace(showID)
55 if showID == "" {
56 return "", fmt.Errorf("empty show id")
57 }
58 if _, err := strconv.Atoi(showID); err != nil {
59 return "", fmt.Errorf("invalid anipub show id %q", showID)
60 }
61 return showID, nil
62}