Commit 56c911c0cd
Unsigned
Layout: unified · split
internal/anime_list.go added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | package main | |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/json" | |
| 5 | "fmt" | |
| 6 | "io/ioutil" | |
| 7 | "net/http" | |
| 8 | "os" | |
| 9 | "strings" | |
| 10 | ) | |
| 11 | ||
| 12 | type Anime struct { | |
| 13 | ID string `json:"_id"` | |
| 14 | Name string `json:"name"` | |
| 15 | AvailableEpisodes interface{} `json:"availableEpisodes"` | |
| 16 | } | |
| 17 | ||
| 18 | type Response struct { | |
| 19 | Data struct { | |
| 20 | Shows struct { | |
| 21 | Edges []Anime `json:"edges"` | |
| 22 | } `json:"shows"` | |
| 23 | } `json:"data"` | |
| 24 | } | |
| 25 | ||
| 26 | func main() { | |
| 27 | // Get environment variables | |
| 28 | mode := getEnv("ANI_CLI_MODE", "sub") | |
| 29 | ||
| 30 | // Query for the anime (from a file in this example) | |
| 31 | query := "One piece" | |
| 32 | ||
| 33 | // Search anime | |
| 34 | animeList := searchAnime(string(query), mode) | |
| 35 | fmt.Println(animeList) | |
| 36 | } | |
| 37 | ||
| 38 | // searchAnime performs the API call and fetches anime information | |
| 39 | func searchAnime(query, mode string) string { | |
| 40 | const ( | |
| 41 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" | |
| 42 | allanimeRef = "https://allanime.to" | |
| 43 | allanimeBase = "allanime.day" | |
| 44 | allanimeAPI = "https://api." + allanimeBase + "/api" | |
| 45 | ) | |
| 46 | ||
| 47 | 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 } } }` | |
| 48 | ||
| 49 | // Build the request URL | |
| 50 | 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) | |
| 51 | ||
| 52 | // Make the HTTP request | |
| 53 | req, err := http.NewRequest("GET", url, nil) | |
| 54 | if err != nil { | |
| 55 | fmt.Println("Error creating HTTP request:", err) | |
| 56 | return "" | |
| 57 | } | |
| 58 | req.Header.Set("User-Agent", agent) | |
| 59 | req.Header.Set("Referer", allanimeRef) | |
| 60 | ||
| 61 | client := &http.Client{} | |
| 62 | resp, err := client.Do(req) | |
| 63 | if err != nil { | |
| 64 | fmt.Println("Error making HTTP request:", err) | |
| 65 | return "" | |
| 66 | } | |
| 67 | defer resp.Body.Close() | |
| 68 | ||
| 69 | body, err := ioutil.ReadAll(resp.Body) | |
| 70 | if err != nil { | |
| 71 | fmt.Println("Error reading response body:", err) | |
| 72 | return "" | |
| 73 | } | |
| 74 | ||
| 75 | // Parse the JSON response | |
| 76 | var response Response | |
| 77 | err = json.Unmarshal(body, &response) | |
| 78 | if err != nil { | |
| 79 | fmt.Println("Error parsing JSON:", err) | |
| 80 | return "" | |
| 81 | } | |
| 82 | ||
| 83 | // Format and return the anime list | |
| 84 | var animeList strings.Builder | |
| 85 | for _, anime := range response.Data.Shows.Edges { | |
| 86 | // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64 | |
| 87 | ||
| 88 | var episodesStr string | |
| 89 | ||
| 90 | switch v := anime.AvailableEpisodes.(type) { | |
| 91 | case float64: | |
| 92 | episodesStr = fmt.Sprintf("%d", int(v)) // Handle numbers | |
| 93 | case string: | |
| 94 | episodesStr = v // Handle string | |
| 95 | case nil: | |
| 96 | episodesStr = "N/A" // Handle nulls | |
| 97 | default: | |
| 98 | episodesStr = "Unknown" | |
| 99 | } | |
| 100 | ||
| 101 | ||
| 102 | animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr)) | |
| 103 | ||
| 104 | // In the formatting part | |
| 105 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%d episodes)NEWLINEFROMHERE\n", anime.ID, anime.Name, availableEpisodes)) | |
| 106 | ||
| 107 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%d episodes)NEWLINEFROMHERE\n", anime.ID, anime.Name, anime.AvailableEpisodes)) | |
| 108 | } | |
| 109 | return animeList.String() | |
| 110 | } | |
| 111 | ||
| 112 | // getEnv returns the value of an environment variable or a default value if it's not set | |
| 113 | func getEnv(key, defaultValue string) string { | |
| 114 | value := os.Getenv(key) | |
| 115 | if value == "" { | |
| 116 | return defaultValue | |
| 117 | } | |
| 118 | return value | |
| 119 | } | |
| \ No newline at end of file | ||
internal/episode_list.go added +106
| @@ -0,0 +1,106 @@ | ||
| 1 | package main | |
| 2 | ||
| 3 | import ( | |
| 4 | "encoding/json" | |
| 5 | "fmt" | |
| 6 | "io/ioutil" | |
| 7 | "net/http" | |
| 8 | "sort" | |
| 9 | "strconv" | |
| 10 | "strings" | |
| 11 | ) | |
| 12 | ||
| 13 | type EpisodesResponse struct { | |
| 14 | Data struct { | |
| 15 | Show struct { | |
| 16 | ID string `json:"_id"` | |
| 17 | AvailableEpisodesDetail map[string]interface{} `json:"availableEpisodesDetail"` | |
| 18 | } `json:"show"` | |
| 19 | } `json:"data"` | |
| 20 | } | |
| 21 | ||
| 22 | func main() { | |
| 23 | // Get environment variables | |
| 24 | // Read the ID from the file | |
| 25 | id := "ReooPAxPMsHM4KPMY" | |
| 26 | ||
| 27 | // Fetch episodes list | |
| 28 | episodeList := episodesList(string(id), "sub") | |
| 29 | ||
| 30 | // Write the episode list to a file | |
| 31 | fmt.Println(episodeList) | |
| 32 | } | |
| 33 | ||
| 34 | // episodesList performs the API call and fetches the episodes list | |
| 35 | func episodesList(showID, mode string) string { | |
| 36 | const ( | |
| 37 | agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" | |
| 38 | allanimeRef = "https://allanime.to" | |
| 39 | allanimeBase = "allanime.day" | |
| 40 | allanimeAPI = "https://api." + allanimeBase + "/api" | |
| 41 | ) | |
| 42 | ||
| 43 | episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}` | |
| 44 | ||
| 45 | // Build the request URL | |
| 46 | url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql) | |
| 47 | ||
| 48 | // Make the HTTP request | |
| 49 | req, err := http.NewRequest("GET", url, nil) | |
| 50 | if err != nil { | |
| 51 | fmt.Println("Error creating HTTP request:", err) | |
| 52 | return "" | |
| 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 | fmt.Println("Error making HTTP request:", err) | |
| 61 | return "" | |
| 62 | } | |
| 63 | defer resp.Body.Close() | |
| 64 | ||
| 65 | body, err := ioutil.ReadAll(resp.Body) | |
| 66 | if err != nil { | |
| 67 | fmt.Println("Error reading response body:", err) | |
| 68 | return "" | |
| 69 | } | |
| 70 | ||
| 71 | // Parse the JSON response | |
| 72 | var response EpisodesResponse | |
| 73 | err = json.Unmarshal(body, &response) | |
| 74 | if err != nil { | |
| 75 | fmt.Println("Error parsing JSON:", err) | |
| 76 | return "" | |
| 77 | } | |
| 78 | ||
| 79 | // Extract and sort the episodes | |
| 80 | episodes := extractEpisodes(response.Data.Show.AvailableEpisodesDetail, mode) | |
| 81 | return strings.Join(episodes, "\n") | |
| 82 | } | |
| 83 | ||
| 84 | // extractEpisodes extracts the episodes list from the availableEpisodesDetail field | |
| 85 | func 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("%.1f", ep)) | |
| 104 | } | |
| 105 | return episodesStr | |
| 106 | } | |