internal/anime_list.go
112 lines · 3264 bytes
1package internal
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8 // "strings"
9)
10
11type anime struct {
12 ID string `json:"_id"`
13 Name string `json:"name"`
14 AvailableEpisodes interface{} `json:"availableEpisodes"`
15}
16
17type response struct {
18 Data struct {
19 Shows struct {
20 Edges []anime `json:"edges"`
21 } `json:"shows"`
22 } `json:"data"`
23}
24
25// func main() {
26// // Get environment variables
27// mode := "sub"
28
29// // Query for the anime (from a file in this example)
30// query := "one piece"
31
32// // Search anime
33// animeList, err := SearchAnime(string(query), mode)
34// if err != nil {
35
36// }
37// fmt.Println(animeList)
38// }
39
40// searchAnime performs the API call and fetches anime information
41func SearchAnime(query, mode string) (map[string]string, error) {
42 const (
43 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
44 allanimeRef = "https://allanime.to"
45 allanimeBase = "allanime.day"
46 allanimeAPI = "https://api." + allanimeBase + "/api"
47 )
48
49 // Format and return the anime list
50 animeList := make(map[string]string)
51
52 searchGql := `query( $search: SearchInput $limit: Int $page: Int $translationType: VaildTranslationTypeEnumType $countryOrigin: VaildCountryOriginEnumType ) { shows( search: $search limit: $limit page: $page translationType: $translationType countryOrigin: $countryOrigin ) { edges { _id name availableEpisodes __typename } } }`
53
54 // Build the request URL
55 url := fmt.Sprintf("%s?variables={\"search\":{\"allowAdult\":false,\"allowUnknown\":false,\"query\":\"%s\"},\"limit\":40,\"page\":1,\"translationType\":\"%s\",\"countryOrigin\":\"ALL\"}&query=%s", allanimeAPI, query, mode, searchGql)
56
57 // Make the HTTP request
58 req, err := http.NewRequest("GET", url, nil)
59 if err != nil {
60 fmt.Println("Error creating HTTP request:", err)
61 return animeList, err
62 }
63 req.Header.Set("User-Agent", agent)
64 req.Header.Set("Referer", allanimeRef)
65
66 client := &http.Client{}
67 resp, err := client.Do(req)
68 if err != nil {
69 fmt.Println("Error making HTTP request:", err)
70 return animeList, err
71 }
72 defer resp.Body.Close()
73
74 body, err := ioutil.ReadAll(resp.Body)
75 if err != nil {
76 fmt.Println("Error reading response body:", err)
77 return animeList, err
78 }
79
80 // Parse the JSON response
81 var response response
82 err = json.Unmarshal(body, &response)
83 if err != nil {
84 fmt.Println("Error parsing JSON:", err)
85 return animeList, err
86 }
87
88 for _, anime := range response.Data.Shows.Edges {
89 // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64
90
91 var episodesStr string
92
93 // Log(anime.AvailableEpisodes, logFile)
94 // episodesStr = anime.AvailableEpisodes.sub
95
96 //anime is = {"dub":0,"raw":0,"sub":4}
97
98 if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok {
99 // Log(episodes, logFile)
100 if subEpisodes, ok := episodes["sub"].(float64); ok {
101 episodesStr = fmt.Sprintf("%d", int(subEpisodes))
102 } else {
103 Log(subEpisodes, logFile)
104 episodesStr = "Unknown"
105 }
106 }
107
108 animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr)
109 // animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr))
110 }
111 return animeList, nil
112}