Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Commit

Permalink
count in user-search query
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmdrz committed Jul 29, 2019
1 parent cf9eb4c commit f88b7fc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goinsta

import (
"encoding/json"
"fmt"
"strconv"
"time"
)
Expand Down Expand Up @@ -69,16 +70,21 @@ 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{
Endpoint: urlSearchUser,
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,
},
},
Expand Down
32 changes: 32 additions & 0 deletions tests/search_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit f88b7fc

Please sign in to comment.