justlog/helix/user.go

154 lines
3.7 KiB
Go
Raw Normal View History

2018-12-02 14:53:01 +01:00
package helix
2018-12-02 19:23:54 +01:00
import (
"net/http"
2020-03-02 20:33:58 +01:00
helixClient "github.com/nicklaw5/helix"
2018-12-02 19:23:54 +01:00
log "github.com/sirupsen/logrus"
)
2020-03-02 20:33:58 +01:00
// Client wrapper for helix
2018-12-02 19:23:54 +01:00
type Client struct {
clientID string
2020-03-02 20:33:58 +01:00
client *helixClient.Client
2018-12-02 19:23:54 +01:00
httpClient *http.Client
}
var (
2020-03-02 20:33:58 +01:00
userCacheByID map[string]*UserData
userCacheByUsername map[string]*UserData
2018-12-02 19:23:54 +01:00
)
func init() {
2020-03-02 20:33:58 +01:00
userCacheByID = map[string]*UserData{}
userCacheByUsername = map[string]*UserData{}
2018-12-02 19:23:54 +01:00
}
2020-03-02 20:33:58 +01:00
// NewClient Create helix client
2018-12-02 19:23:54 +01:00
func NewClient(clientID string) Client {
2020-03-02 20:33:58 +01:00
client, err := helixClient.NewClient(&helixClient.Options{
ClientID: clientID,
})
if err != nil {
panic(err)
}
2018-12-02 19:23:54 +01:00
return Client{
clientID: clientID,
2020-03-02 20:33:58 +01:00
client: client,
2018-12-02 19:23:54 +01:00
httpClient: &http.Client{},
}
}
type userResponse struct {
Data []UserData `json:"data"`
}
2020-03-02 20:33:58 +01:00
// UserData exported data from twitch
2018-12-02 19:23:54 +01:00
type UserData struct {
ID string `json:"id"`
Login string `json:"login"`
DisplayName string `json:"display_name"`
Type string `json:"type"`
BroadcasterType string `json:"broadcaster_type"`
Description string `json:"description"`
ProfileImageURL string `json:"profile_image_url"`
OfflineImageURL string `json:"offline_image_url"`
ViewCount int `json:"view_count"`
Email string `json:"email"`
}
2020-03-02 20:33:58 +01:00
// GetUsersByUserIds receive userData for given ids
2018-12-02 19:23:54 +01:00
func (c *Client) GetUsersByUserIds(userIDs []string) (map[string]UserData, error) {
var filteredUserIDs []string
for _, id := range userIDs {
if _, ok := userCacheByID[id]; !ok {
filteredUserIDs = append(filteredUserIDs, id)
}
}
2020-03-02 20:33:58 +01:00
if len(filteredUserIDs) > 0 {
resp, err := c.client.GetUsers(&helixClient.UsersParams{
IDs: filteredUserIDs,
})
2018-12-02 19:23:54 +01:00
if err != nil {
2020-03-02 20:33:58 +01:00
return map[string]UserData{}, err
2018-12-02 19:23:54 +01:00
}
2020-03-02 20:33:58 +01:00
log.Infof("%d GetUsersByUserIds %v", resp.StatusCode, filteredUserIDs)
for _, user := range resp.Data.Users {
data := &UserData{
ID: user.ID,
Login: user.Login,
DisplayName: user.Login,
Type: user.Type,
BroadcasterType: user.BroadcasterType,
Description: user.Description,
ProfileImageURL: user.ProfileImageURL,
OfflineImageURL: user.OfflineImageURL,
ViewCount: user.ViewCount,
Email: user.Email,
2018-12-02 19:23:54 +01:00
}
2020-03-02 20:33:58 +01:00
userCacheByID[user.ID] = data
userCacheByUsername[user.Login] = data
2018-12-02 19:23:54 +01:00
}
}
result := make(map[string]UserData)
for _, id := range userIDs {
2020-03-02 20:33:58 +01:00
result[id] = *userCacheByID[id]
2018-12-02 19:23:54 +01:00
}
return result, nil
}
// GetUsersByUsernames fetches userdata from helix
2018-12-02 19:23:54 +01:00
func (c *Client) GetUsersByUsernames(usernames []string) (map[string]UserData, error) {
var filteredUsernames []string
for _, username := range usernames {
if _, ok := userCacheByUsername[username]; !ok {
filteredUsernames = append(filteredUsernames, username)
}
}
2020-03-02 20:33:58 +01:00
if len(filteredUsernames) > 0 {
resp, err := c.client.GetUsers(&helixClient.UsersParams{
Logins: filteredUsernames,
})
2018-12-02 19:23:54 +01:00
if err != nil {
2020-03-02 20:33:58 +01:00
return map[string]UserData{}, err
2018-12-02 19:23:54 +01:00
}
2020-03-02 20:33:58 +01:00
log.Infof("%d GetUsersByUsernames %v", resp.StatusCode, filteredUsernames)
for _, user := range resp.Data.Users {
data := &UserData{
ID: user.ID,
Login: user.Login,
DisplayName: user.Login,
Type: user.Type,
BroadcasterType: user.BroadcasterType,
Description: user.Description,
ProfileImageURL: user.ProfileImageURL,
OfflineImageURL: user.OfflineImageURL,
ViewCount: user.ViewCount,
Email: user.Email,
2018-12-02 19:23:54 +01:00
}
2020-03-02 20:33:58 +01:00
userCacheByID[user.ID] = data
userCacheByUsername[user.Login] = data
2018-12-02 19:23:54 +01:00
}
}
result := make(map[string]UserData)
for _, username := range usernames {
2020-03-02 20:33:58 +01:00
result[username] = *userCacheByUsername[username]
2018-12-02 19:23:54 +01:00
}
return result, nil
}