internal/episode_list.go
106 lines · 2828 bytes
1package internal
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "net/http"
8 "sort"
9 "strconv"
10)
11
12type episodesResponse struct {
13 Data struct {
14 Show struct {
15 ID string `json:"_id"`
16 AvailableEpisodesDetail map[string]interface{} `json:"availableEpisodesDetail"`
17 } `json:"show"`
18 } `json:"data"`
19}
20
21// func main() {
22// // Get environment variables
23// // Read the ID from the file
24// id := "ReooPAxPMsHM4KPMY"
25
26// // Fetch episodes list
27// episodeList := episodesList(string(id), "sub")
28
29// // Write the episode list to a file
30// fmt.Println(episodeList)
31// }
32
33// episodesList performs the API call and fetches the episodes list
34func EpisodesList(showID, mode string) ([]string, error) {
35 const (
36 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
37 allanimeRef = "https://allanime.to"
38 allanimeBase = "allanime.day"
39 allanimeAPI = "https://api." + allanimeBase + "/api"
40 )
41
42 episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}`
43
44 // Build the request URL
45 url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql)
46 episodes := []string{}
47
48 // Make the HTTP request
49 req, err := http.NewRequest("GET", url, nil)
50 if err != nil {
51 Log(fmt.Sprint("Error creating HTTP request:", err))
52 return episodes, err
53 }
54 req.Header.Set("User-Agent", agent)
55 req.Header.Set("Referer", allanimeRef)
56
57 client := &http.Client{}
58 resp, err := client.Do(req)
59 if err != nil {
60 Log(fmt.Sprint("Error making HTTP request:", err))
61 return episodes, err
62 }
63 defer resp.Body.Close()
64
65 body, err := io.ReadAll(resp.Body)
66 if err != nil {
67 Log(fmt.Sprint("Error reading response body:", err))
68 return episodes, err
69 }
70
71 // Parse the JSON response
72 var response episodesResponse
73 err = json.Unmarshal(body, &response)
74 if err != nil {
75 Log(fmt.Sprint("Error parsing JSON:", err))
76 return episodes, err
77 }
78
79 // Extract and sort the episodes
80 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, mode)
81 return episodes, nil
82}
83
84// extractEpisodes extracts the episodes list from the availableEpisodesDetail field
85func extractEpisodes(availableEpisodesDetail map[string]interface{}, mode string) []string {
86 var episodes []float64
87
88 // Check if the mode (e.g., "sub") exists in the map
89 if eps, ok := availableEpisodesDetail[mode].([]interface{}); ok {
90 for _, ep := range eps {
91 if epNum, err := strconv.ParseFloat(fmt.Sprintf("%v", ep), 64); err == nil {
92 episodes = append(episodes, epNum)
93 }
94 }
95 }
96
97 // Sort episodes numerically
98 sort.Float64s(episodes)
99
100 // Convert to string and return
101 var episodesStr []string
102 for _, ep := range episodes {
103 episodesStr = append(episodesStr, fmt.Sprintf("%v", ep))
104 }
105 return episodesStr
106}