Skip to content

Commit b82a2b8

Browse files
committed
feat(scraper): update AllAnime API requests to use POST method with JSON body
1 parent 562045e commit b82a2b8

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ require (
6767
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
6868
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
6969
github.com/mattn/go-runewidth v0.0.22 // indirect
70-
github.com/mattn/go-sqlite3 v1.14.38
70+
github.com/mattn/go-sqlite3 v1.14.41
7171
github.com/muesli/cancelreader v0.2.2 // indirect
7272
github.com/nsf/termbox-go v1.1.1 // indirect
7373
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
107107
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
108108
github.com/mattn/go-runewidth v0.0.22 h1:76lXsPn6FyHtTY+jt2fTTvsMUCZq1k0qwRsAMuxzKAk=
109109
github.com/mattn/go-runewidth v0.0.22/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
110-
github.com/mattn/go-sqlite3 v1.14.38 h1:tDUzL85kMvOrvpCt8P64SbGgVFtJB11GPi2AdmITgb4=
111-
github.com/mattn/go-sqlite3 v1.14.38/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
110+
github.com/mattn/go-sqlite3 v1.14.41 h1:8p7Pwz5NHkEbWSqc/ygU4CBGubhFFkpgP9KwcdkAHNA=
111+
github.com/mattn/go-sqlite3 v1.14.41/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
112112
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
113113
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
114114
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=

internal/scraper/allanime.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
package scraper
33

44
import (
5+
"bytes"
56
"encoding/json"
67
"fmt"
78
"io"
89
"maps"
910
"net/http"
10-
"net/url"
1111
"regexp"
1212
"sort"
1313
"strings"
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
const (
22-
AllAnimeReferer = "https://allanime.to"
22+
AllAnimeReferer = "https://allmanga.to"
2323
AllAnimeBase = "allanime.day"
2424
AllAnimeAPI = "https://api.allanime.day/api"
2525
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
@@ -132,14 +132,21 @@ func (c *AllAnimeClient) SearchAnime(query string, options ...any) ([]*models.An
132132
return nil, fmt.Errorf("failed to marshal variables: %w", err)
133133
}
134134

135-
// Build the request URL exactly like Curd
136-
reqURL := fmt.Sprintf("%s?variables=%s&query=%s", c.apiBase, url.QueryEscape(string(variablesJSON)), url.QueryEscape(searchGql))
135+
// Build the POST request body with variables and query
136+
reqBody, err := json.Marshal(map[string]any{
137+
"variables": json.RawMessage(variablesJSON),
138+
"query": searchGql,
139+
})
140+
if err != nil {
141+
return nil, fmt.Errorf("failed to marshal request body: %w", err)
142+
}
137143

138-
req, err := http.NewRequest("GET", reqURL, nil)
144+
req, err := http.NewRequest("POST", c.apiBase, bytes.NewReader(reqBody))
139145
if err != nil {
140146
return nil, fmt.Errorf("failed to create request: %w", err)
141147
}
142148

149+
req.Header.Set("Content-Type", "application/json")
143150
req.Header.Set("User-Agent", c.userAgent)
144151
req.Header.Set("Referer", c.referer)
145152

@@ -234,15 +241,21 @@ func (c *AllAnimeClient) GetEpisodesList(animeID string, mode string) ([]string,
234241
if err != nil {
235242
return nil, fmt.Errorf("failed to marshal variables: %w", err)
236243
}
237-
reqURL := fmt.Sprintf("%s?variables=%s&query=%s",
238-
c.apiBase,
239-
url.QueryEscape(string(varsBytes)),
240-
url.QueryEscape(episodesListGql))
241244

242-
req, err := http.NewRequest("GET", reqURL, nil)
245+
// Build the POST request body
246+
reqBody, err := json.Marshal(map[string]any{
247+
"variables": json.RawMessage(varsBytes),
248+
"query": episodesListGql,
249+
})
250+
if err != nil {
251+
return nil, fmt.Errorf("failed to marshal request body: %w", err)
252+
}
253+
254+
req, err := http.NewRequest("POST", c.apiBase, bytes.NewReader(reqBody))
243255
if err != nil {
244256
return nil, fmt.Errorf("failed to create request: %w", err)
245257
}
258+
req.Header.Set("Content-Type", "application/json")
246259
req.Header.Set("User-Agent", c.userAgent)
247260
req.Header.Set("Referer", c.referer)
248261

@@ -480,18 +493,22 @@ func (c *AllAnimeClient) GetEpisodeURL(animeID string, episodeNo string, mode st
480493
if err != nil {
481494
return "", nil, fmt.Errorf("failed to marshal variables: %w", err)
482495
}
483-
variables := string(varsBytes)
484496

485-
req, err := http.NewRequest("GET", c.apiBase+"/api", nil)
497+
// Build the POST request body
498+
reqBody, err := json.Marshal(map[string]any{
499+
"variables": json.RawMessage(varsBytes),
500+
"query": episodeEmbedGQL,
501+
})
486502
if err != nil {
487-
return "", nil, fmt.Errorf("failed to create request: %w", err)
503+
return "", nil, fmt.Errorf("failed to marshal request body: %w", err)
488504
}
489505

490-
q := req.URL.Query()
491-
q.Add("variables", variables)
492-
q.Add("query", episodeEmbedGQL)
493-
req.URL.RawQuery = q.Encode()
506+
req, err := http.NewRequest("POST", c.apiBase, bytes.NewReader(reqBody))
507+
if err != nil {
508+
return "", nil, fmt.Errorf("failed to create request: %w", err)
509+
}
494510

511+
req.Header.Set("Content-Type", "application/json")
495512
req.Header.Set("Referer", c.referer)
496513
req.Header.Set("User-Agent", c.userAgent)
497514

0 commit comments

Comments
 (0)