Skip to content

Commit 2664648

Browse files
committed
update: allanime api fix
1 parent 3bfcdea commit 2664648

3 files changed

Lines changed: 55 additions & 44 deletions

File tree

internal/anilist.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ func GetAnimeIDAndImage(anilistMediaID int) (int, string, error) {
435435

436436
// Function to get user data from AniList
437437
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) {
441441
lists {
442442
entries {
443443
media {
@@ -468,14 +468,19 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
468468
}
469469
}
470470
}
471-
}`, userID)
471+
}`
472+
473+
variables := map[string]interface{}{
474+
"userId": userID,
475+
"type": "ANIME",
476+
}
472477

473478
headers := map[string]string{
474479
"Authorization": "Bearer " + token,
475480
"Content-Type": "application/json",
476481
}
477482

478-
response, err := makePostRequest("https://graphql.anilist.co", query, nil, headers)
483+
response, err := makePostRequest("https://graphql.anilist.co", query, variables, headers)
479484
if err != nil {
480485
return nil, err
481486
}
@@ -484,9 +489,9 @@ func GetUserData(token string, userID int) (map[string]interface{}, error) {
484489
}
485490

486491
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) {
490495
lists {
491496
entries {
492497
media {
@@ -520,14 +525,19 @@ func GetUserDataPreview(token string, userID int) (map[string]interface{}, error
520525
}
521526
}
522527
}
523-
}`, userID)
528+
}`
529+
530+
variables := map[string]interface{}{
531+
"userId": userID,
532+
"type": "ANIME",
533+
}
524534

525535
headers := map[string]string{
526536
"Authorization": "Bearer " + token,
527537
"Content-Type": "application/json",
528538
}
529539

530-
response, err := makePostRequest("https://graphql.anilist.co", query, nil, headers)
540+
response, err := makePostRequest("https://graphql.anilist.co", query, variables, headers)
531541
if err != nil {
532542
return nil, err
533543
}

internal/episode_list.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -33,52 +34,52 @@ type episodesResponse struct {
3334
// episodesList performs the API call and fetches the episodes list
3435
func EpisodesList(showID, mode string) ([]string, error) {
3536
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-
)
4237

4338
episodesListGql := `query ($showId: String!) { show( _id: $showId ) { _id availableEpisodesDetail }}`
4439

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+
}
4848

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))
5151
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)
5453
}
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")
5758

5859
client := &http.Client{}
5960
resp, err := client.Do(req)
6061
if err != nil {
6162
Log(fmt.Sprint("Error making HTTP request:", err))
62-
return episodes, err
63+
return nil, err
6364
}
6465
defer resp.Body.Close()
6566

6667
body, err := io.ReadAll(resp.Body)
6768
if err != nil {
6869
Log(fmt.Sprint("Error reading response body:", err))
69-
return episodes, err
70+
return nil, err
7071
}
7172

7273
// Parse the JSON response
7374
var response episodesResponse
7475
err = json.Unmarshal(body, &response)
7576
if err != nil {
7677
Log(fmt.Sprint("Error parsing JSON:", err))
77-
return episodes, err
78+
return nil, err
7879
}
7980

8081
// Extract and sort the episodes
81-
episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode)
82+
episodes := extractEpisodes(response.Data.Show.AvailableEpisodesDetail, preferredMode)
8283
if len(episodes) == 0 {
8384
fallbackMode := alternateTranslationType(preferredMode)
8485
episodes = extractEpisodes(response.Data.Show.AvailableEpisodesDetail, fallbackMode)

internal/episode_url.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package internal
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"io"
78
"net/http"
8-
"net/url"
99
"regexp"
1010
"strings"
1111
"time"
@@ -170,41 +170,41 @@ func GetEpisodeURL(config CurdConfig, id string, epNo int) ([]string, error) {
170170
func getEpisodeURLForMode(id, mode string, epNo int) ([]string, error) {
171171
query := `query($showId:String!,$translationType:VaildTranslationTypeEnumType!,$episodeString:String!){episode(showId:$showId,translationType:$translationType,episodeString:$episodeString){episodeString sourceUrls}}`
172172

173-
variables := map[string]string{
173+
variables := map[string]interface{}{
174174
"showId": id,
175175
"translationType": normalizeTranslationType(mode),
176176
"episodeString": fmt.Sprintf("%d", epNo),
177177
}
178178

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+
})
180184
if err != nil {
181-
return nil, err
185+
return nil, fmt.Errorf("failed to marshal request body: %w", err)
182186
}
183187

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-
190188
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))
192190
if err != nil {
193-
return nil, err
191+
return nil, fmt.Errorf("failed to create request: %w", err)
194192
}
195193

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")
197196
req.Header.Set("Referer", "https://allanime.to")
197+
req.Header.Set("Origin", "https://allanime.to")
198198

199199
resp, err := client.Do(req)
200200
if err != nil {
201-
return nil, err
201+
return nil, fmt.Errorf("failed to send request: %w", err)
202202
}
203203
defer resp.Body.Close()
204204

205205
body, err := io.ReadAll(resp.Body)
206206
if err != nil {
207-
return nil, err
207+
return nil, fmt.Errorf("failed to read response: %w", err)
208208
}
209209

210210
var response allanimeResponse

0 commit comments

Comments
 (0)