internal/aniskip.go

103 lines · 3026 bytes

  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 := http.Get(url)
 34	if err != nil {
 35		Log(fmt.Sprintf("error fetching data from AniSkip API: %w", err), logFile)
 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), logFile)
 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.Sprintf("failed to read response body: %w", err), logFile)
 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}