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

Commit

Permalink
Merge pull request #19 from lyon-esport/handle-more-than-100-users
Browse files Browse the repository at this point in the history
[Fix] Handle more than 100 users
  • Loading branch information
egguy authored Jun 5, 2021
2 parents e7ca994 + d838650 commit 82722d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
57 changes: 50 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -152,13 +153,34 @@ func (c *Client) GetToken() error {
return nil
}

// GetStreams will get a list of live Streams
// GetStreams is a wrapper to handle more than 100 Streams
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) GetStreams(streamsList []string) ([]StreamData, int, error) {
// since first, when uninitialized is 0, we have to set it to the default value
/*if i.First == 0 {
i.First = 20
}*/
var s []StreamData
var token int

for i := 0; i < int(math.Ceil(float64(len(streamsList))/100)); i++ {
end := i*100+100
if len(streamsList) < end {
end = len(streamsList)
}

r, token, err := c.get100Streams(streamsList[i*100:end])
if err != nil {
return s, token, nil
}
s = append(s, r...)
}

return s, token, nil
}

// get100Streams will get a list of live Streams, limited to 100 users
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) get100Streams(streamsList []string) ([]StreamData, int, error) {
if len(streamsList) >= 100 {
return nil, 0, errors.New("can't get more than 100 users")
}

var uri *url.URL
var header = http.Header{}
Expand Down Expand Up @@ -210,9 +232,31 @@ func (c Client) GetStreams(streamsList []string) ([]StreamData, int, error) {
return s.Data, token, nil
}

// GetUsers will get a list of users information
// GetUsers is a wrapper to handle more than 100 Users
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) GetUsers(usersList []string) ([]UserData, error) {
var s []UserData

for i := 0; i < int(math.Ceil(float64(len(usersList))/100)); i++ {
end := i*100+100
if len(usersList) < end {
end = len(usersList)
}

r, err := c.get100Users(usersList[i*100:end])
if err != nil {
return s, nil
}

s = append(s, r...)
}

return s, nil
}

// get100Users will get a list of users information, limited to 100 users
// The url query parameter are defined by the GetStreamsInput struct
func (c Client) get100Users(usersList []string) ([]UserData, error) {
var uri *url.URL
var header = http.Header{}

Expand Down Expand Up @@ -283,6 +327,5 @@ func (c Client) GetFollows(userID string) (int, error) {
}

json.Unmarshal(body, &s)
// fmt.Println(s.Total)
return s.Total, nil
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func scrapeStreams(twitch *Client) {
break
}
}
log.Debug("Scraped ", streamScraped, "/", len(streamsID), " stream")
log.Debug("Scraped ", streamScraped, "/", len(streamsID), " streams")

time.Sleep(30 * time.Second)
}
Expand Down

0 comments on commit 82722d0

Please sign in to comment.