diff --git a/requester/flaresolverr.go b/requester/flaresolverr.go index 1f1839f..a21bd2b 100644 --- a/requester/flaresolverr.go +++ b/requester/flaresolverr.go @@ -6,6 +6,8 @@ import ( "fmt" "io" "net/http" + "net/http/cookiejar" + "net/url" "strings" "sync" ) @@ -19,6 +21,10 @@ type FlareSolverr struct { initiated bool } +var ( + ErrListSessions = fmt.Errorf("failed to list sessions") +) + func NewFlareSolverr(url string, timeoutMilli int) *FlareSolverr { poolSize := 5 httpClient := &http.Client{} @@ -48,6 +54,14 @@ func (f *FlareSolverr) FillSessionPool() error { // Pre-initialize the pool with existing sessions sessions, err := f.ListSessions() if err != nil { + // if fail to list sessions, it may not support the sessions.list command + // create new dumb sessions to fill the pool + if err == ErrListSessions { + for len(f.sessionPool) < cap(f.sessionPool) { + f.sessionPool <- "dumb-session" + } + return nil + } fmt.Println("Failed to list existing FlareSolverr sessions:", err) return err } else { @@ -134,6 +148,9 @@ func (f *FlareSolverr) ListSessions() ([]string, error) { if err != nil { return nil, err } + if sessionsResponse["sessions"] == nil { + return nil, ErrListSessions + } sessions := sessionsResponse["sessions"].([]interface{}) var sessionIDs []string @@ -172,7 +189,7 @@ type Response struct { } `json:"solution"` } -func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) { +func (f *FlareSolverr) Get(_url string) (io.ReadCloser, error) { // Check if the FlareSolverr instance was initiated if !f.initiated { return io.NopCloser(bytes.NewReader([]byte(""))), nil @@ -188,7 +205,7 @@ func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) { body := map[string]string{ "cmd": "request.get", - "url": url, + "url": _url, "maxTimeout": fmt.Sprintf("%d", f.maxTimeout), "session": session, } @@ -196,6 +213,7 @@ func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) { if err != nil { return nil, err } + req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1", f.url), bytes.NewBuffer(jsonBody)) if err != nil { return nil, err @@ -225,6 +243,40 @@ func (f *FlareSolverr) Get(url string) (io.ReadCloser, error) { return nil, fmt.Errorf("under attack") } - // Return the response body + // If the response body is empty but cookies are present, make a new request + if response.Solution.Response == "" && len(response.Solution.Cookies) > 0 { + // Create a new request with cookies + client := &http.Client{} + cookieJar, err := cookiejar.New(&cookiejar.Options{}) + if err != nil { + return nil, err + } + for _, cookie := range response.Solution.Cookies { + cookieJar.SetCookies(&url.URL{Host: cookie.Domain}, []*http.Cookie{ + { + Name: cookie.Name, + Value: cookie.Value, + Domain: cookie.Domain, + Path: cookie.Path, + }, + }) + } + client.Jar = cookieJar + + secondReq, err := http.NewRequest("GET", _url, nil) + if err != nil { + return nil, err + } + + secondResp, err := client.Do(secondReq) + if err != nil { + return nil, err + } + + // Return the body of the second request + return secondResp.Body, nil + } + + // Return the original response body return io.NopCloser(bytes.NewReader([]byte(response.Solution.Response))), nil } diff --git a/schema/audio.go b/schema/audio.go index 6f7d8b7..b9ec36a 100644 --- a/schema/audio.go +++ b/schema/audio.go @@ -1,10 +1,13 @@ package schema +import "strings" + type Audio string const ( AudioPortuguese = "Português" AudioPortuguese2 = "Portugues" + AudioPortuguese3 = "PT-BR" AudioEnglish = "Inglês" AudioEnglish2 = "Ingles" AudioSpanish = "Espanhol" @@ -33,6 +36,7 @@ const ( var AudioList = []Audio{ AudioPortuguese, AudioPortuguese2, + AudioPortuguese3, AudioEnglish, AudioEnglish2, AudioSpanish, @@ -64,7 +68,7 @@ func (a Audio) String() string { func GetAudioFromString(s string) *Audio { for _, a := range AudioList { - if string(a) == s { + if strings.EqualFold(string(a), s) { return &a } } @@ -77,6 +81,8 @@ func (a Audio) toTag() string { return "brazilian" case AudioPortuguese2: return "brazilian" + case AudioPortuguese3: + return "brazilian" case AudioEnglish: return "eng" case AudioEnglish2: