Commit 1698b06bc8
Unsigned
Layout: unified · split
VERSION.txt +1 −1
| @@ -1 +1 @@ | ||
| 1 | 0.0.8 | |
| \ No newline at end of file | ||
| 1 | 0.0.10 | |
| \ No newline at end of file | ||
internal/anime_list.go +38 −19
| @@ -7,6 +7,7 @@ import ( | ||
| 7 | 7 | "net/http" |
| 8 | 8 | "os" |
| 9 | 9 | "path/filepath" |
| 10 | "net/url" | |
| 10 | 11 | // "strings" |
| 11 | 12 | ) |
| 12 | 13 | |
| @@ -39,9 +40,9 @@ type response struct { | ||
| 39 | 40 | // fmt.Println(animeList) |
| 40 | 41 | // } |
| 41 | 42 | |
| 42 | // searchAnime performs the API call and fetches anime information | |
| 43 | 43 | func SearchAnime(query, mode string) (map[string]string, error) { |
| 44 | 44 | userCurdConfig := GetGlobalConfig() |
| 45 | var logFile string | |
| 45 | 46 | if userCurdConfig == nil { |
| 46 | 47 | logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log") |
| 47 | 48 | } else { |
| @@ -54,18 +55,47 @@ func SearchAnime(query, mode string) (map[string]string, error) { | ||
| 54 | 55 | allanimeAPI = "https://api." + allanimeBase + "/api" |
| 55 | 56 | ) |
| 56 | 57 | |
| 57 | // Format and return the anime list | |
| 58 | // Prepare the anime list | |
| 58 | 59 | animeList := make(map[string]string) |
| 59 | 60 | |
| 60 | 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 } } }` | |
| 61 | searchGql := `query($search: SearchInput, $limit: Int, $page: Int, $translationType: VaildTranslationTypeEnumType, $countryOrigin: VaildCountryOriginEnumType) { | |
| 62 | shows(search: $search, limit: $limit, page: $page, translationType: $translationType, countryOrigin: $countryOrigin) { | |
| 63 | edges { | |
| 64 | _id | |
| 65 | name | |
| 66 | availableEpisodes | |
| 67 | __typename | |
| 68 | } | |
| 69 | } | |
| 70 | }` | |
| 71 | ||
| 72 | // Prepare the GraphQL variables | |
| 73 | variables := map[string]interface{}{ | |
| 74 | "search": map[string]interface{}{ | |
| 75 | "allowAdult": false, | |
| 76 | "allowUnknown": false, | |
| 77 | "query": query, | |
| 78 | }, | |
| 79 | "limit": 40, | |
| 80 | "page": 1, | |
| 81 | "translationType": mode, | |
| 82 | "countryOrigin": "ALL", | |
| 83 | } | |
| 84 | ||
| 85 | // Marshal the variables to JSON | |
| 86 | variablesJSON, err := json.Marshal(variables) | |
| 87 | if err != nil { | |
| 88 | Log(fmt.Sprintf("Error encoding variables to JSON: %v", err), logFile) | |
| 89 | return animeList, err | |
| 90 | } | |
| 61 | 91 | |
| 62 | 92 | // Build the request URL |
| 63 | 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) | |
| 93 | url := fmt.Sprintf("%s?variables=%s&query=%s", allanimeAPI, url.QueryEscape(string(variablesJSON)), url.QueryEscape(searchGql)) | |
| 64 | 94 | |
| 65 | 95 | // Make the HTTP request |
| 66 | 96 | req, err := http.NewRequest("GET", url, nil) |
| 67 | 97 | if err != nil { |
| 68 | Log(fmt.Sprint("Error creating HTTP request:", err), logFile) | |
| 98 | Log(fmt.Sprintf("Error creating HTTP request: %v", err), logFile) | |
| 69 | 99 | return animeList, err |
| 70 | 100 | } |
| 71 | 101 | req.Header.Set("User-Agent", agent) |
| @@ -74,7 +104,7 @@ func SearchAnime(query, mode string) (map[string]string, error) { | ||
| 74 | 104 | client := &http.Client{} |
| 75 | 105 | resp, err := client.Do(req) |
| 76 | 106 | if err != nil { |
| 77 | Log(fmt.Sprint("Error making HTTP request:", err), logFile) | |
| 107 | Log(fmt.Sprintf("Error making HTTP request: %v", err), logFile) | |
| 78 | 108 | return animeList, err |
| 79 | 109 | } |
| 80 | 110 | defer resp.Body.Close() |
| @@ -82,11 +112,11 @@ func SearchAnime(query, mode string) (map[string]string, error) { | ||
| 82 | 112 | // Read the response body |
| 83 | 113 | body, err := io.ReadAll(resp.Body) |
| 84 | 114 | if err != nil { |
| 85 | Log(fmt.Sprint("Error reading response body:", err), logFile) | |
| 115 | Log(fmt.Sprintf("Error reading response body: %v", err), logFile) | |
| 86 | 116 | return animeList, err |
| 87 | 117 | } |
| 88 | 118 | |
| 89 | // Debug: Log the response status and first part of body | |
| 119 | // Debug: Log the response status and first part of the body | |
| 90 | 120 | Log(fmt.Sprintf("Response Status: %s", resp.Status), logFile) |
| 91 | 121 | Log(fmt.Sprintf("Response Body (first 500 chars): %s", string(body[:min(len(body), 500)])), logFile) |
| 92 | 122 | |
| @@ -99,17 +129,8 @@ func SearchAnime(query, mode string) (map[string]string, error) { | ||
| 99 | 129 | } |
| 100 | 130 | |
| 101 | 131 | for _, anime := range response.Data.Shows.Edges { |
| 102 | // availableEpisodes, _ := anime.AvailableEpisodes.Int64() // Converts json.Number to int64 | |
| 103 | ||
| 104 | 132 | var episodesStr string |
| 105 | ||
| 106 | // Log(anime.AvailableEpisodes, logFile) | |
| 107 | // episodesStr = anime.AvailableEpisodes.sub | |
| 108 | ||
| 109 | //anime = {"dub":0,"raw":0,"sub":4} | |
| 110 | ||
| 111 | 133 | if episodes, ok := anime.AvailableEpisodes.(map[string]interface{}); ok { |
| 112 | // Log(episodes, logFile) | |
| 113 | 134 | if subEpisodes, ok := episodes["sub"].(float64); ok { |
| 114 | 135 | episodesStr = fmt.Sprintf("%d", int(subEpisodes)) |
| 115 | 136 | } else { |
| @@ -117,9 +138,7 @@ func SearchAnime(query, mode string) (map[string]string, error) { | ||
| 117 | 138 | episodesStr = "Unknown" |
| 118 | 139 | } |
| 119 | 140 | } |
| 120 | ||
| 121 | 141 | animeList[anime.ID] = fmt.Sprintf("%s (%s episodes)", anime.Name, episodesStr) |
| 122 | // animeList.WriteString(fmt.Sprintf("%s\t%s (%s episodes)\n", anime.ID, anime.Name, episodesStr)) | |
| 123 | 142 | } |
| 124 | 143 | return animeList, nil |
| 125 | 144 | } |