Skip to content

Commit

Permalink
fix: better parse offset with field (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Jul 18, 2023
1 parent 5b04578 commit 51c5160
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 92 deletions.
9 changes: 6 additions & 3 deletions aapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

const (
apiBase = "https://app-api.pixiv.net/"

OffsetFieldMaxBookmarkID = "max_bookmark_id"
OffsetFieldOffset = "offset"
)

// AppPixivAPI -- App-API (6.x - app-api.pixiv.net)
Expand Down Expand Up @@ -113,7 +116,7 @@ func (a *AppPixivAPI) UserIllusts(uid uint64, _type string, offset int) ([]Illus
if err := a.request(path, params, data, true); err != nil {
return nil, 0, err
}
next, err := parseNextPageOffset(data.NextURL)
next, err := parseNextPageOffset(data.NextURL, OffsetFieldOffset)
return data.Illusts, next, err
}

Expand All @@ -139,7 +142,7 @@ func (a *AppPixivAPI) UserBookmarksIllust(uid uint64, restrict string, maxBookma
if err := a.request(path, params, data, true); err != nil {
return nil, 0, err
}
next, err := parseNextPageOffset(data.NextURL)
next, err := parseNextPageOffset(data.NextURL, OffsetFieldMaxBookmarkID)
return data.Illusts, next, err
}

Expand All @@ -159,7 +162,7 @@ func (a *AppPixivAPI) IllustFollow(restrict string, offset int) ([]Illust, int,
if err := a.request(path, params, data, true); err != nil {
return nil, 0, err
}
next, err := parseNextPageOffset(data.NextURL)
next, err := parseNextPageOffset(data.NextURL, OffsetFieldOffset)
return data.Illusts, next, err
}

Expand Down
164 changes: 83 additions & 81 deletions fixtures/response_illust_detail.json
Original file line number Diff line number Diff line change
@@ -1,89 +1,91 @@
{
"id": 104714396,
"title": "Business casual",
"type": "illust",
"image_urls": {
"square_medium": "https://i.pximg.net/c/540x540_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"original": ""
},
"caption": "oc minah",
"restrict": 0,
"user": {
"id": 33591988,
"name": "Chaesu",
"account": "chaesuart",
"comment": "",
"is_followed": false,
"profile_image_urls": {
"medium": "https://i.pximg.net/user-profile/img/2023/06/17/01/06/53/24554545_62f2fdca5632d9dbe9486a17cb3fda87_170.png"
}
},
"tags": [
{
"name": "Minah",
"translated_name": ""
"illust": {
"id": 104714396,
"title": "Business casual",
"type": "illust",
"image_urls": {
"square_medium": "https://i.pximg.net/c/540x540_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"original": ""
},
{
"name": "オリジナル",
"translated_name": ""
},
{
"name": "オリキャラ",
"translated_name": ""
},
{
"name": "うちの子",
"translated_name": ""
},
{
"name": "スニーカー",
"translated_name": ""
}
],
"tools": [],
"create_date": "2023-01-22T10:02:20+09:00",
"page_count": 3,
"width": 1108,
"height": 1400,
"sanity_level": 2,
"x_restrict": 0,
"series": null,
"meta_single_page": {
"original_image_url": ""
},
"meta_pages": [
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p0.jpg"
"caption": "oc minah",
"restrict": 0,
"user": {
"id": 33591988,
"name": "Chaesu",
"account": "chaesuart",
"comment": "",
"is_followed": false,
"profile_image_urls": {
"medium": "https://i.pximg.net/user-profile/img/2023/06/17/01/06/53/24554545_62f2fdca5632d9dbe9486a17cb3fda87_170.png"
}
},
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p1_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p1_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p1_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p1.jpg"
"tags": [
{
"name": "Minah",
"translated_name": ""
},
{
"name": "オリジナル",
"translated_name": ""
},
{
"name": "オリキャラ",
"translated_name": ""
},
{
"name": "うちの子",
"translated_name": ""
},
{
"name": "スニーカー",
"translated_name": ""
}
],
"tools": [],
"create_date": "2023-01-22T10:02:20+09:00",
"page_count": 3,
"width": 1108,
"height": 1400,
"sanity_level": 2,
"x_restrict": 0,
"series": null,
"meta_single_page": {
"original_image_url": ""
},
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p2_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p2_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p2_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p2.jpg"
"meta_pages": [
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p0_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p0.jpg"
}
},
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p1_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p1_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p1_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p1.jpg"
}
},
{
"image_urls": {
"square_medium": "https://i.pximg.net/c/360x360_10_webp/img-master/img/2023/01/22/10/02/20/104714396_p2_square1200.jpg",
"medium": "https://i.pximg.net/c/540x540_70/img-master/img/2023/01/22/10/02/20/104714396_p2_master1200.jpg",
"large": "https://i.pximg.net/c/600x1200_90_webp/img-master/img/2023/01/22/10/02/20/104714396_p2_master1200.jpg",
"original": "https://i.pximg.net/img-original/img/2023/01/22/10/02/20/104714396_p2.jpg"
}
}
}
],
"total_view": 17233,
"total_bookmarks": 3556,
"is_bookmarked": false,
"visible": true,
"is_muted": false,
"total_comments": 9,
"illust_ai_type": 1
],
"total_view": 17233,
"total_bookmarks": 3556,
"is_bookmarked": false,
"visible": true,
"is_muted": false,
"total_comments": 9,
"illust_ai_type": 1
}
}
12 changes: 6 additions & 6 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"strconv"
)

func parseNextPageOffset(s string) (int, error) {
// parseNextPageOffset parses next_url and returns the offset
//
// field is either "max_bookmark_id" or "offset"
func parseNextPageOffset(s, field string) (int, error) {
if s == "" {
return 0, nil
}
Expand All @@ -20,12 +23,9 @@ func parseNextPageOffset(s string) (int, error) {
return 0, fmt.Errorf("parse next_url raw query error: %s {%s}", s, err)
}

offsetParam := m.Get("max_bookmark_id")
offsetParam := m.Get(field)
if offsetParam == "" {
offsetParam = m.Get("offset")
if offsetParam == "" {
return 0, fmt.Errorf("offset param omitted: %s {%s}", offsetParam, err)
}
return 0, fmt.Errorf("offset param omitted: %s", field)
}

offset, err := strconv.Atoi(offsetParam)
Expand Down
25 changes: 23 additions & 2 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ import (
"github.com/stretchr/testify/require"
)

func TestParseNextPageURL(t *testing.T) {
func TestParseBookmarkNextPageURL(t *testing.T) {
r := require.New(t)
next, err := parseNextPageOffset("https://app-api.pixiv.net/v1/user/bookmarks/illust?filter=for_ios&restrict=private&user_id=60984430&max_bookmark_id=21354656694")
next, err := parseNextPageOffset("https://app-api.pixiv.net/v1/user/bookmarks/illust?filter=for_ios&restrict=private&user_id=60984430&max_bookmark_id=21354656694", OffsetFieldMaxBookmarkID)
r.Nil(err)
r.Equal(21354656694, next)
}

func TestParseOffsetNextPageURL(t *testing.T) {
r := require.New(t)
next, err := parseNextPageOffset("https://app-api.pixiv.net/v1/user/illusts?filter=for_ios&type=illust&user_id=490219&offset=30", OffsetFieldOffset)
r.Nil(err)
r.Equal(30, next)
}

func TestParseEmptyNextPageURL(t *testing.T) {
r := require.New(t)
next, err := parseNextPageOffset("", OffsetFieldOffset)
r.Nil(err)
r.Equal(0, next)
}

func TestParseInvalidNextPageURL(t *testing.T) {
r := require.New(t)
next, err := parseNextPageOffset("https://app-api.pixiv.net/v1/user/illusts?filter=for_ios&type=illust&user_id=490219&offset=30", "invalid")
r.EqualError(err, "offset param omitted: invalid")
r.Equal(0, next)
}

0 comments on commit 51c5160

Please sign in to comment.