Commit 26646485b3

26646485b355cbc5da4ea13a8e8710454a91840c

parent: 3bfcdea64b

Unsigned

Wraient <wraient.911@gmail.com> · 2026-04-05 18:27 UTC

update: allanime api fix

Layout: unified · split

internal/anilist.go +20 −10
@@ -435,9 +435,9 @@ func GetAnimeIDAndImage(anilistMediaID int) (int, string, error) {
435 435
436// Function to get user data from AniList 436// Function to get user data from AniList
437func GetUserData(token string, userID int) (map[string]interface{}, error) { 437func GetUserData(token string, userID int) (map[string]interface{}, error) {
438 query := fmt.Sprintf(` 438 query := `
439 { 439 query ($userId: Int, $type: MediaType) {
440 MediaListCollection(userId: %d, type: ANIME) { 440 MediaListCollection(userId: $userId, type: $type) {
441 lists { 441 lists {
442 entries { 442 entries {
443 media { 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 headers := map[string]string{ 478 headers := map[string]string{
474 "Authorization": "Bearer " + token, 479 "Authorization": "Bearer " + token,
475 "Content-Type": "application/json", 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 if err != nil { 484 if err != nil {
480 return nil, err 485 return nil, err
481 } 486 }
@@ -484,9 +489,9 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
484} 489}
485 490
486func GetUserDataPreview(token string, userID int) (map[string]interface{}, error) { 491func GetUserDataPreview(token string, userID int) (map[string]interface{}, error) {
487 query := fmt.Sprintf(` 492 query := `
488 { 493 query ($userId: Int, $type: MediaType) {
489 MediaListCollection(userId: %d, type: ANIME) { 494 MediaListCollection(userId: $userId, type: $type) {
490 lists { 495 lists {
491 entries { 496 entries {
492 media { 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 headers := map[string]string{ 535 headers := map[string]string{
526 "Authorization": "Bearer " + token, 536 "Authorization": "Bearer " + token,
527 "Content-Type": "application/json", 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 if err != nil { 541 if err != nil {
532 return nil, err 542 return nil, err
533 } 543 }
internal/episode_list.go +20 −19
@@ -1,6 +1,7 @@
1package internal 1package internal
2 2
3import ( 3import (
4 "bytes"
4 "encoding/json" 5 "encoding/json"
5 "fmt" 6 "fmt"
6 "io" 7 "io"
@@ -33,40 +34,40 @@ type episodesResponse struct {
33// episodesList performs the API call and fetches the episodes list 34// episodesList performs the API call and fetches the episodes list
34func EpisodesList(showID, mode string) ([]string, error) { 35func EpisodesList(showID, mode string) ([]string, error) {
35 preferredMode := normalizeTranslationType(mode) 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 episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}` 38 episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}`
44 39
45 // Build the request URL 40 // Build POST request body
46 url := fmt.Sprintf("%s?variables={\"showId\":\"%s\"}&query=%s", allanimeAPI, showID, episodesListGql) 41 requestBody, err := json.Marshal(map[string]interface{}{
47 episodes := []string{} 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 49 // Make the HTTP POST request
50 req, err := http.NewRequest("GET", url, nil) 50 req, err := http.NewRequest("POST", "https://api.allanime.day/api", bytes.NewBuffer(requestBody))
51 if err != nil { 51 if err != nil {
52 Log(fmt.Sprint("Error creating HTTP request:", err)) 52 return nil, fmt.Errorf("failed to create request: %w", err)
53 return episodes, err
54 } 53 }
55 req.Header.Set("User-Agent", agent) 54 req.Header.Set("Content-Type", "application/json")
56 req.Header.Set("Referer", allanimeRef) 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 client := &http.Client{} 59 client := &http.Client{}
59 resp, err := client.Do(req) 60 resp, err := client.Do(req)
60 if err != nil { 61 if err != nil {
61 Log(fmt.Sprint("Error making HTTP request:", err)) 62 Log(fmt.Sprint("Error making HTTP request:", err))
62 return episodes, err 63 return nil, err
63 } 64 }
64 defer resp.Body.Close() 65 defer resp.Body.Close()
65 66
66 body, err := io.ReadAll(resp.Body) 67 body, err := io.ReadAll(resp.Body)
67 if err != nil { 68 if err != nil {
68 Log(fmt.Sprint("Error reading response body:", err)) 69 Log(fmt.Sprint("Error reading response body:", err))
69 return episodes, err 70 return nil, err
70 } 71 }
71 72
72 // Parse the JSON response 73 // Parse the JSON response
@@ -74,11 +75,11 @@ func EpisodesList(showID, mode string) ([]string, error) {
74 err = json.Unmarshal(body, &response) 75 err = json.Unmarshal(body, &response)
75 if err != nil { 76 if err != nil {
76 Log(fmt.Sprint("Error parsing JSON:", err)) 77 Log(fmt.Sprint("Error parsing JSON:", err))
77 return episodes, err 78 return nil, err
78 } 79 }
79 80
80 // Extract and sort the episodes 81 // Extract and sort the episodes
81 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode) 82 episodes := extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode)
82 if len(episodes) == 0 { 83 if len(episodes) == 0 {
83 fallbackMode := alternateTranslationType(preferredMode) 84 fallbackMode := alternateTranslationType(preferredMode)
84 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, fallbackMode) 85 episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, fallbackMode)
internal/episode_url.go +15 −15
@@ -1,11 +1,11 @@
1package internal 1package internal
2 2
3import ( 3import (
4 "bytes"
4 "encoding/json" 5 "encoding/json"
5 "fmt" 6 "fmt"
6 "io" 7 "io"
7 "net/http" 8 "net/http"
8 "net/url"
9 "regexp" 9 "regexp"
10 "strings" 10 "strings"
11 "time" 11 "time"
@@ -170,41 +170,41 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
170func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) { 170func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
171 query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}` 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 "showId": id, 174 "showId": id,
175 "translationType": normalizeTranslationType(mode), 175 "translationType": normalizeTranslationType(mode),
176 "episodeString": fmt.Sprintf("%d", epNo), 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 if err != nil { 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 client := &http.Client{} 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 if err != nil { 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 req.Header.Set("Referer", "https://allanime.to") 196 req.Header.Set("Referer", "https://allanime.to")
197 req.Header.Set("Origin", "https://allanime.to")
198 198
199 resp, err := client.Do(req) 199 resp, err := client.Do(req)
200 if err != nil { 200 if err != nil {
201 return nil, err 201 return nil, fmt.Errorf("failed to send request: %w", err)
202 } 202 }
203 defer resp.Body.Close() 203 defer resp.Body.Close()
204 204
205 body, err := io.ReadAll(resp.Body) 205 body, err := io.ReadAll(resp.Body)
206 if err != nil { 206 if err != nil {
207 return nil, err 207 return nil, fmt.Errorf("failed to read response: %w", err)
208 } 208 }
209 209
210 var response allanimeResponse 210 var response allanimeResponse