func (self *Item) emulateImageUrlOf(size ImageSize, page int) (string, error) { baseUrl, ok := self.ImageUrls[size] if !ok { return "", pp.Errorf("Image size %v is not available", size) } u, err := url.Parse(baseUrl) if err != nil { return "", err } dir, originalName := path.Split(u.Path) extension := path.Ext(originalName) var name string switch size { case SIZE_128x128: name = fmt.Sprintf("%d_p%d_square1200.%s", self.ID, page, extension) case SIZE_480x960: fallthrough case SIZE_SMALL: fallthrough case SIZE_MEDIUM: name = fmt.Sprintf("%d_p%d_master1200.%s", self.ID, page, extension) case SIZE_LARGE: name = fmt.Sprintf("%d_p%d.%s", self.ID, page, extension) default: return "", pp.Errorf("Unsupported image size: %s", size) } u.Path = path.Join(dir, name) return u.String(), nil }
func (ep *APIEndpoint) readAndParse(resData io.Reader, ret interface{}) error { apiResponse, err := ReadAPIResponse(resData) if err != nil { return err } err = json.Unmarshal(apiResponse.Response, ret) if err != nil { return pp.Errorf("JSON Parse failed: %v", err) } if apiResponse.Status != "success" { return pp.Errorf("Request failed: %v", apiResponse) } return nil }
// call this api with given client, path and params. And stores its response to ret // through json.Unmarshal func (ep *APIEndpoint) call(client *http.Client, path string, params map[string]string, ret interface{}) error { req, err := ep.RequestGET(path, params) if err != nil { return err } res, err := client.Do(req) if err != nil { return err } if res.StatusCode != 200 { return pp.Errorf("%v - \n %v", res.Status, res.Request) } defer func() { // Drain and close the body to let the Transport reuse the connection io.Copy(ioutil.Discard, res.Body) res.Body.Close() }() body, err := ep.openResponse(res) if err != nil { return err } decBody, err := gzip.NewReader(body) if err != nil { return err } ep.readAndParse(decBody, ret) return nil }
func (t *teeReadCloser) Close() error { var rErr, wErr error if t.rc != nil { rErr = t.rc.Close() } if t.wc != nil { wErr = t.wc.Close() } if rErr != nil || wErr != nil { return pp.Errorf("TeeReadCloser.Close error:\n input reader error: %v \n output writer error %v", rErr, wErr) } else { return nil } }
func (self *Item) OpenImage(px *Pixiv, size ImageSize, pace int) (io.ReadCloser, error) { url, ok := self.ImageUrls[size] if !ok { return nil, pp.Errorf("Image size %v is not available", size) } client, err := px.PlainClient() if err != nil { return nil, err } req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } setRequestHeaders(req) resp, err := client.Do(req) if err != nil { return nil, err } return resp.Body, nil }