156 lines
3.6 KiB
Go
156 lines
3.6 KiB
Go
package pixiv
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
//GetBookmarks .
|
|
func (p *Pixiv) GetBookmarks(userID string) (illusts []Illust, err error) {
|
|
type ajaxBody struct {
|
|
Works []struct {
|
|
ID string `json:"id"`
|
|
} `json:"works"`
|
|
Total int `json:"total"`
|
|
}
|
|
offset := 0
|
|
for {
|
|
req := fmt.Sprintf("https://www.pixiv.net/ajax/user/%s/illusts/bookmarks?tag=&offset=%d&limit=%d&rest=show&lang=en", userID, offset, p.ItemsPerRequest)
|
|
rawAjaxBody, err := p.ajaxRequest(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var body ajaxBody
|
|
err = json.Unmarshal(rawAjaxBody, &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, illust := range body.Works {
|
|
illusts = append(illusts, Illust{ID: illust.ID, complited: false})
|
|
}
|
|
if len(illusts) < body.Total {
|
|
offset = len(illusts)
|
|
log.Printf("Fetched %d/%d bookmarks", offset, body.Total)
|
|
continue
|
|
} else {
|
|
log.Printf("Fetched all %d bookmarks of user %s", offset, userID)
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//GetFollows .
|
|
func (p *Pixiv) GetFollows(userID string) (users []User, err error) {
|
|
type ajaxBody struct {
|
|
Users []User `json:"users"`
|
|
Total int `json:"total"`
|
|
}
|
|
offset := 0
|
|
for {
|
|
req := fmt.Sprintf("https://www.pixiv.net/ajax/user/%s/following?offset=%d&limit=%d&rest=show&tag=&lang=en", userID, offset, p.ItemsPerRequest)
|
|
rawAjaxBody, err := p.ajaxRequest(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var body ajaxBody
|
|
err = json.Unmarshal(rawAjaxBody, &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
users = append(users, body.Users[:]...)
|
|
if len(users) < body.Total {
|
|
offset = len(users)
|
|
log.Printf("Fetched %d/%d follows", offset, body.Total)
|
|
continue
|
|
} else {
|
|
log.Printf("Fetched all %d follows of user %s", offset, userID)
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//GetIllust .
|
|
func (p *Pixiv) GetIllust(illustID string) (r Illust, err error) {
|
|
req := fmt.Sprintf("https://www.pixiv.net/ajax/illust/%s?lang=en", illustID)
|
|
apiResp, err := p.ajaxRequest(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(apiResp, &r)
|
|
if err != nil {
|
|
log.Printf("Unmarshaling of api response returned an error: %s", err.Error())
|
|
return
|
|
}
|
|
if r.PageCount > 1 {
|
|
req := fmt.Sprintf("https://www.pixiv.net/ajax/illust/%s/pages?lang=en", illustID)
|
|
apiResp, err := p.ajaxRequest(req)
|
|
if err != nil {
|
|
return Illust{}, err
|
|
}
|
|
err = json.Unmarshal(apiResp, &r.Pages)
|
|
if err != nil {
|
|
log.Printf("Unmarshaling of api response returned an error: %s", err.Error())
|
|
return Illust{}, err
|
|
}
|
|
} else {
|
|
var page IllustPage
|
|
page.URLs = r.URLs
|
|
page.Width = r.Width
|
|
page.Height = r.Height
|
|
r.Pages = append(r.Pages, page)
|
|
}
|
|
return
|
|
}
|
|
|
|
//GetUserIllustsID .
|
|
func (p *Pixiv) GetUserIllustsID(userID string) (illusts []Illust, err error) {
|
|
type ajaxBody struct {
|
|
Illusts map[string]interface{} `json:"illusts"`
|
|
}
|
|
var body ajaxBody
|
|
req := fmt.Sprintf("https://www.pixiv.net/ajax/user/%s/profile/all?lang=en", userID)
|
|
rawAjaxBody, err := p.ajaxRequest(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = json.Unmarshal(rawAjaxBody, &body)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for ID := range body.Illusts {
|
|
illusts = append(illusts, Illust{ID: ID, complited: false})
|
|
}
|
|
return
|
|
}
|
|
|
|
//ComplateIllust .
|
|
func (p *Pixiv) ComplateIllust(illust *Illust) (err error) {
|
|
*illust, err = p.GetIllust(illust.ID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
illust.complited = true
|
|
return
|
|
}
|
|
|
|
//ComplateIllustMultiple .
|
|
func (p *Pixiv) ComplateIllustMultiple(illusts *[]Illust) (err error) {
|
|
for i, illust := range *illusts {
|
|
(*illusts)[i], err = p.GetIllust(illust.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
(*illusts)[i].complited = true
|
|
}
|
|
return
|
|
}
|
|
|
|
//Complited .
|
|
func (i *Illust) Complited() bool {
|
|
return i.complited
|
|
}
|