Commit 26646485b3
Unsigned
Layout: unified · split
internal/anilist.go +20 −10
| @@ -435,9 +435,9 @@ func GetAnimeIDAndImage(anilistMediaID int) (int, string, error) { | ||
| 435 | 435 | |
| 436 | 436 | // Function to get user data from AniList |
| 437 | 437 | func GetUserData(token string, userID int) (map[string]interface{}, error) { |
| 438 | query := fmt.Sprintf(` | |
| 439 | { | |
| 440 | MediaListCollection(userId: %d, type: ANIME) { | |
| 438 | query := ` | |
| 439 | query ($userId: Int, $type: MediaType) { | |
| 440 | MediaListCollection(userId: $userId, type: $type) { | |
| 441 | 441 | lists { |
| 442 | 442 | entries { |
| 443 | 443 | media { |
| @@ -468,14 +468,19 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) { | ||
| 468 | 468 | } |
| 469 | 469 | } |
| 470 | 470 | } |
| 471 | }`, userID) | |
| 471 | }` | |
| 472 | ||
| 473 | variables := map[string]interface{}{ | |
| 474 | "userId": userID, | |
| 475 | "type": "ANIME", | |
| 476 | } | |
| 472 | 477 | |
| 473 | 478 | headers := map[string]string{ |
| 474 | 479 | "Authorization": "Bearer " + token, |
| 475 | 480 | "Content-Type": "application/json", |
| 476 | 481 | } |
| 477 | 482 | |
| 478 | response, err := makePostRequest("https://graphql.anilist.co", query, nil, headers) | |
| 483 | response, err := makePostRequest("https://graphql.anilist.co", query, variables, headers) | |
| 479 | 484 | if err != nil { |
| 480 | 485 | return nil, err |
| 481 | 486 | } |
| @@ -484,9 +489,9 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) { | ||
| 484 | 489 | } |
| 485 | 490 | |
| 486 | 491 | func GetUserDataPreview(token string, userID int) (map[string]interface{}, error) { |
| 487 | query := fmt.Sprintf(` | |
| 488 | { | |
| 489 | MediaListCollection(userId: %d, type: ANIME) { | |
| 492 | query := ` | |
| 493 | query ($userId: Int, $type: MediaType) { | |
| 494 | MediaListCollection(userId: $userId, type: $type) { | |
| 490 | 495 | lists { |
| 491 | 496 | entries { |
| 492 | 497 | media { |
| @@ -520,14 +525,19 @@ func GetUserDataPreview(token string, userID int) (map[string]interface{}, error | ||
| 520 | 525 | } |
| 521 | 526 | } |
| 522 | 527 | } |
| 523 | }`, userID) | |
| 528 | }` | |
| 529 | ||
| 530 | variables := map[string]interface{}{ | |
| 531 | "userId": userID, | |
| 532 | "type": "ANIME", | |
| 533 | } | |
| 524 | 534 | |
| 525 | 535 | headers := map[string]string{ |
| 526 | 536 | "Authorization": "Bearer " + token, |
| 527 | 537 | "Content-Type": "application/json", |
| 528 | 538 | } |
| 529 | 539 | |
| 530 | response, err := makePostRequest("https://graphql.anilist.co", query, nil, headers) | |
| 540 | response, err := makePostRequest("https://graphql.anilist.co", query, variables, headers) | |
| 531 | 541 | if err != nil { |
| 532 | 542 | return nil, err |
| 533 | 543 | } |
internal/episode_list.go +20 −19
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | package internal |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "bytes" | |
| 4 | 5 | "encoding/json" |
| 5 | 6 | "fmt" |
| 6 | 7 | "io" |
| @@ -33,40 +34,40 @@ type episodesResponse struct { | ||
| 33 | 34 | // episodesList performs the API call and fetches the episodes list |
| 34 | 35 | func EpisodesList(showID, mode string) ([]string, error) { |
| 35 | 36 | preferredMode := normalizeTranslationType(mode) |
| 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 | 37 | |
| 43 | 38 | episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}` |
| 44 | 39 | |
| 45 | // Build the request URL | |
| 46 | url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql) | |
| 47 | episodes := []string{} | |
| 40 | // Build POST request body | |
| 41 | requestBody, err := json.Marshal(map[string]interface{}{ | |
| 42 | "query": episodesListGql, | |
| 43 | "variables": map[string]string{"showId": showID}, | |
| 44 | }) | |
| 45 | if err != nil { | |
| 46 | return nil, fmt.Errorf("failed to marshal request body: %w", err) | |
| 47 | } | |
| 48 | 48 | |
| 49 | // Make the HTTP request | |
| 50 | req, err := http.NewRequest("GET", url, nil) | |
| 49 | // Make the HTTP POST request | |
| 50 | req, err := http.NewRequest("POST", "https://api.allanime.day/api", bytes.NewBuffer(requestBody)) | |
| 51 | 51 | if err != nil { |
| 52 | Log(fmt.Sprint("Error creating HTTP request:", err)) | |
| 53 | return episodes, err | |
| 52 | return nil, fmt.Errorf("failed to create request: %w", err) | |
| 54 | 53 | } |
| 55 | req.Header.Set("User-Agent", agent) | |
| 56 | req.Header.Set("Referer", allanimeRef) | |
| 54 | req.Header.Set("Content-Type", "application/json") | |
| 55 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") | |
| 56 | req.Header.Set("Referer", "https://allanime.to") | |
| 57 | req.Header.Set("Origin", "https://allanime.to") | |
| 57 | 58 | |
| 58 | 59 | client := &http.Client{} |
| 59 | 60 | resp, err := client.Do(req) |
| 60 | 61 | if err != nil { |
| 61 | 62 | Log(fmt.Sprint("Error making HTTP request:", err)) |
| 62 | return episodes, err | |
| 63 | return nil, err | |
| 63 | 64 | } |
| 64 | 65 | defer resp.Body.Close() |
| 65 | 66 | |
| 66 | 67 | body, err := io.ReadAll(resp.Body) |
| 67 | 68 | if err != nil { |
| 68 | 69 | Log(fmt.Sprint("Error reading response body:", err)) |
| 69 | return episodes, err | |
| 70 | return nil, err | |
| 70 | 71 | } |
| 71 | 72 | |
| 72 | 73 | // Parse the JSON response |
| @@ -74,11 +75,11 @@ func EpisodesList(showID, mode string) ([]string, error) { | ||
| 74 | 75 | err = json.Unmarshal(body, &response) |
| 75 | 76 | if err != nil { |
| 76 | 77 | Log(fmt.Sprint("Error parsing JSON:", err)) |
| 77 | return episodes, err | |
| 78 | return nil, err | |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | 81 | // Extract and sort the episodes |
| 81 | episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode) | |
| 82 | episodes := extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode) | |
| 82 | 83 | if len(episodes) == 0 { |
| 83 | 84 | fallbackMode := alternateTranslationType(preferredMode) |
| 84 | 85 | episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, fallbackMode) |
internal/episode_url.go +15 −15
| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | package internal |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "bytes" | |
| 4 | 5 | "encoding/json" |
| 5 | 6 | "fmt" |
| 6 | 7 | "io" |
| 7 | 8 | "net/http" |
| 8 | "net/url" | |
| 9 | 9 | "regexp" |
| 10 | 10 | "strings" |
| 11 | 11 | "time" |
| @@ -170,41 +170,41 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) { | ||
| 170 | 170 | func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { |
| 171 | 171 | query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` |
| 172 | 172 | |
| 173 | variables := map[string]string{ | |
| 173 | variables := map[string]interface{}{ | |
| 174 | 174 | "showId": id, |
| 175 | 175 | "translationType": normalizeTranslationType(mode), |
| 176 | 176 | "episodeString": fmt.Sprintf("%d", epNo), |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | variablesJSON, err := json.Marshal(variables) | |
| 179 | // Build POST request body | |
| 180 | requestBody, err := json.Marshal(map[string]interface{}{ | |
| 181 | "query": query, | |
| 182 | "variables": variables, | |
| 183 | }) | |
| 180 | 184 | if err != nil { |
| 181 | return nil, err | |
| 185 | return nil, fmt.Errorf("failed to marshal request body: %w", err) | |
| 182 | 186 | } |
| 183 | 187 | |
| 184 | values := url.Values{} | |
| 185 | values.Set("query", query) | |
| 186 | values.Set("variables", string(variablesJSON)) | |
| 187 | ||
| 188 | reqURL := fmt.Sprintf("%s/api?%s", "https://api.allanime.day", values.Encode()) | |
| 189 | ||
| 190 | 188 | client := &http.Client{} |
| 191 | req, err := http.NewRequest("GET", reqURL, nil) | |
| 189 | req, err := http.NewRequest("POST", "https://api.allanime.day/api", bytes.NewBuffer(requestBody)) | |
| 192 | 190 | if err != nil { |
| 193 | return nil, err | |
| 191 | return nil, fmt.Errorf("failed to create request: %w", err) | |
| 194 | 192 | } |
| 195 | 193 | |
| 196 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0") | |
| 194 | req.Header.Set("Content-Type", "application/json") | |
| 195 | req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36") | |
| 197 | 196 | req.Header.Set("Referer", "https://allanime.to") |
| 197 | req.Header.Set("Origin", "https://allanime.to") | |
| 198 | 198 | |
| 199 | 199 | resp, err := client.Do(req) |
| 200 | 200 | if err != nil { |
| 201 | return nil, err | |
| 201 | return nil, fmt.Errorf("failed to send request: %w", err) | |
| 202 | 202 | } |
| 203 | 203 | defer resp.Body.Close() |
| 204 | 204 | |
| 205 | 205 | body, err := io.ReadAll(resp.Body) |
| 206 | 206 | if err != nil { |
| 207 | return nil, err | |
| 207 | return nil, fmt.Errorf("failed to read response: %w", err) | |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | var response allanimeResponse |