internal/aniskip.go
144 lines · 3939 bytes
8 symbols in this file
1package internal
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "math"
8 "net/http"
9)
10
11// skipTimesResponse struct to hold the response from the AniSkip API
12type skipTimesResponse struct {
13 Found bool `json:"found"`
14 Results []skipResult `json:"results"`
15}
16
17// skipResult struct to hold individual skip result data
18type skipResult struct {
19 Interval skipInterval `json:"interval"`
20}
21
22// skipInterval struct to hold the start and end times for skip intervals
23type skipInterval struct {
24 StartTime float64 `json:"start_time"`
25 EndTime float64 `json:"end_time"`
26}
27
28// GetAniSkipData fetches skip times data for a given anime ID and episode
29func GetAniSkipData(animeMalId int, episode int) (string, error) {
30 baseURL := "https://api.aniskip.com/v1/skip-times"
31 url := fmt.Sprintf("%s/%d/%d?types=op&types=ed", baseURL, animeMalId, episode)
32
33 resp, err := sharedHTTPClient.Get(url)
34 if err != nil {
35 Log(fmt.Errorf("error fetching data from AniSkip API: %w", err))
36 return "", fmt.Errorf("error fetching data from AniSkip API: %w", err)
37 }
38 defer resp.Body.Close()
39
40 if resp.StatusCode != http.StatusOK {
41 Log(fmt.Sprintf("failed with status %d", resp.StatusCode))
42 return "", fmt.Errorf("failed with status %d", resp.StatusCode)
43 }
44
45 body, err := io.ReadAll(resp.Body)
46 if err != nil {
47 Log(fmt.Errorf("failed to read response body %w", err))
48 return "", fmt.Errorf("failed to read response body %w", err)
49 }
50
51 return string(body), nil
52}
53
54// RoundTime rounds a time value to the specified precision
55func RoundTime(timeValue float64, precision int) float64 {
56 multiplier := math.Pow(10, float64(precision))
57 return math.Floor(timeValue*multiplier+0.5) / multiplier
58}
59
60// ParseAniSkipResponse parses the response text from the AniSkip API and updates the Anime struct
61func ParseAniSkipResponse(responseText string, anime *Anime, timePrecision int) error {
62 if responseText == "" {
63 return fmt.Errorf("response text is empty")
64 }
65
66 var data skipTimesResponse
67 err := json.Unmarshal([]byte(responseText), &data)
68 if err != nil {
69 return fmt.Errorf("error unmarshalling response: %w", err)
70 }
71
72 if !data.Found {
73 return fmt.Errorf("no skip times found")
74 }
75
76 // Populate skip times for the anime's episode
77 if len(data.Results) > 0 {
78 op := data.Results[0].Interval
79 anime.Ep.SkipTimes.Op = Skip{
80 Start: int(RoundTime(op.StartTime, timePrecision)),
81 End: int(RoundTime(op.EndTime, timePrecision)),
82 }
83 }
84
85 if len(data.Results) > 1 {
86 ed := data.Results[len(data.Results)-1].Interval
87 anime.Ep.SkipTimes.Ed = Skip{
88 Start: int(RoundTime(ed.StartTime, timePrecision)),
89 End: int(RoundTime(ed.EndTime, timePrecision)),
90 }
91 }
92
93 return nil
94}
95
96// GetAndParseAniSkipData fetches and parses skip times for a given anime ID and episode
97func GetAndParseAniSkipData(animeMalId int, episode int, timePrecision int, anime *Anime) error {
98 responseText, err := GetAniSkipData(animeMalId, episode)
99 if err != nil {
100 return err
101 }
102 return ParseAniSkipResponse(responseText, anime, timePrecision)
103}
104
105// Function to send OP and ED timings to MPV
106func SendSkipTimesToMPV(anime *Anime) error {
107 chapterList := []map[string]interface{}{
108 {
109 "title": "Pre-Opening",
110 "time": 0.0,
111 "end": float64(anime.Ep.SkipTimes.Op.Start),
112 },
113 {
114 "title": "Opening",
115 "time": float64(anime.Ep.SkipTimes.Op.Start),
116 "end": float64(anime.Ep.SkipTimes.Op.End),
117 },
118 {
119 "title": "Main",
120 "time": float64(anime.Ep.SkipTimes.Op.End),
121 "end": float64(anime.Ep.SkipTimes.Ed.Start),
122 },
123 {
124 "title": "Ending",
125 "time": float64(anime.Ep.SkipTimes.Ed.Start),
126 "end": float64(anime.Ep.SkipTimes.Ed.End),
127 },
128 {
129 "title": "Post-Credits",
130 "time": float64(anime.Ep.SkipTimes.Ed.End),
131 },
132 }
133
134 _, err := MPVSendCommand(anime.Ep.Player.SocketPath, []interface{}{
135 "set_property",
136 "chapter-list",
137 chapterList,
138 })
139 if err != nil {
140 return fmt.Errorf("error sending command to MPV: %w", err)
141 }
142
143 return nil
144}