From f88b7fcbaa0b775e6f07eec4f81a4ba73d4a47ef Mon Sep 17 00:00:00 2001 From: Ahmadreza Zibaei Date: Mon, 29 Jul 2019 09:52:23 +0430 Subject: [PATCH] count in user-search query --- search.go | 12 +++++++++--- tests/search_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/search_test.go diff --git a/search.go b/search.go index 2203a9ba..1036e0e3 100644 --- a/search.go +++ b/search.go @@ -2,6 +2,7 @@ package goinsta import ( "encoding/json" + "fmt" "strconv" "time" ) @@ -69,8 +70,12 @@ func newSearch(inst *Instagram) *Search { return search } -// User search by username -func (search *Search) User(user string) (*SearchResult, error) { +// User search by username, you can use count optional parameter to get more than 50 items. +func (search *Search) User(user string, countParam ...int) (*SearchResult, error) { + count := 50 + if len(countParam) > 0 { + count = countParam[0] + } insta := search.inst body, err := insta.sendRequest( &reqOptions{ @@ -78,7 +83,8 @@ func (search *Search) User(user string) (*SearchResult, error) { Query: map[string]string{ "ig_sig_key_version": goInstaSigKeyVersion, "is_typeahead": "true", - "query": user, + "q": user, + "count": fmt.Sprintf("%d", count), "rank_token": insta.rankToken, }, }, diff --git a/tests/search_test.go b/tests/search_test.go new file mode 100644 index 00000000..b3c1f56d --- /dev/null +++ b/tests/search_test.go @@ -0,0 +1,32 @@ +package tests + +import ( + "testing" +) + +func TestSearchUser(t *testing.T) { + count := 20 + + insta, err := getRandomAccount() + if err != nil { + t.Fatal(err) + return + } + result, err := insta.Search.User("a", count) + if err != nil { + t.Fatal(err) + return + } + if result.Status != "ok" { + t.Fatal(result.Status) + return + } + t.Logf("result length is %d", len(result.Users)) + if len(result.Users) < count { + t.Fatalf("length is less than %d", count) + return + } + for _, user := range result.Users { + t.Logf("user %s with id %d\n", user.Username, user.ID) + } +}