71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package pixiv
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type ajaxResp struct {
|
|
Error bool `json:"error"`
|
|
Message string `json:"message"`
|
|
Body json.RawMessage `json:"body"`
|
|
}
|
|
|
|
func (p *Pixiv) ajaxRequest(addr string) (r json.RawMessage, err error) {
|
|
req, err := http.NewRequest("GET", addr, nil)
|
|
if err != nil {
|
|
err = fmt.Errorf("NewRequest failed (%s)", addr)
|
|
return
|
|
}
|
|
req.AddCookie(&p.phpsessid)
|
|
req.Header.Set("User-Agent", p.Ua)
|
|
req.Header.Set("Referer", "https://www.pixiv.net")
|
|
var resp *http.Response
|
|
for i := 0; i < p.RetryCount; i++ {
|
|
resp, err = p.client.Do(req)
|
|
if err != nil {
|
|
log.Printf("%s", err.Error())
|
|
log.Printf("Retry %d of %d...", i, p.RetryCount)
|
|
continue
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
log.Printf("GET %s failed: bad status code: %d", addr, resp.StatusCode)
|
|
log.Printf("Retry %d of %d...", i, p.RetryCount)
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
log.Printf("%s", err.Error())
|
|
return
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
log.Printf("GET %s failed: bad status code: %d", addr, resp.StatusCode)
|
|
return
|
|
}
|
|
respData, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Println("ioutil.ReadAll(resp.Body) failed")
|
|
return
|
|
}
|
|
var ajax ajaxResp
|
|
err = json.Unmarshal(respData, &ajax)
|
|
if err != nil {
|
|
log.Println("json.Unmarshal(respData, &ajax) failed")
|
|
return
|
|
}
|
|
if ajax.Error {
|
|
err = errors.New(ajax.Message)
|
|
log.Println("ajax error")
|
|
return
|
|
}
|
|
r = ajax.Body
|
|
return
|
|
}
|