Skip to content

Commit

Permalink
Handling of inconsistent type for next_max_id field (fixes ahmdrz#305) (
Browse files Browse the repository at this point in the history
  • Loading branch information
buckket authored Jun 11, 2020
1 parent e280985 commit f3fdb17
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
)

// Users is a struct that stores many user's returned by many different methods.
Expand All @@ -17,11 +18,12 @@ type Users struct {
err error
endpoint string

Status string `json:"status"`
BigList bool `json:"big_list"`
Users []User `json:"users"`
PageSize int `json:"page_size"`
NextID string `json:"next_max_id"`
Status string `json:"status"`
BigList bool `json:"big_list"`
Users []User `json:"users"`
PageSize int `json:"page_size"`
RawNextID json.RawMessage `json:"next_max_id"`
NextID string `json:"-"`
}

func newUsers(inst *Instagram) *Users {
Expand Down Expand Up @@ -66,6 +68,19 @@ func (users *Users) Next() bool {
usrs := Users{}
err = json.Unmarshal(body, &usrs)
if err == nil {
if len(usrs.RawNextID) > 0 && usrs.RawNextID[0] == '"' && usrs.RawNextID[len(usrs.RawNextID)-1] == '"' {
if err := json.Unmarshal(usrs.RawNextID, &usrs.NextID); err != nil {
users.err = err
return false
}
} else {
var nextID int64
if err := json.Unmarshal(usrs.RawNextID, &nextID); err != nil {
users.err = err
return false
}
usrs.NextID = strconv.FormatInt(nextID, 10)
}
*users = usrs
if !usrs.BigList || usrs.NextID == "" {
users.err = ErrNoMore
Expand Down

0 comments on commit f3fdb17

Please sign in to comment.