-
Notifications
You must be signed in to change notification settings - Fork 2
/
getUsersID.go
127 lines (115 loc) · 3.45 KB
/
getUsersID.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package repos
import (
"encoding/json"
"errors"
"log"
"strings"
"time"
"github.com/khades/servbot/httpclient"
cache "github.com/patrickmn/go-cache"
)
var userIDCacheObject = cache.New(60*time.Minute, 600*time.Second)
type twitchUserInfo struct {
ID string `json:"_id"`
DisplayName string `json:"display_name"`
Name string `json:"name"`
}
type usersLoginStruct struct {
Users []twitchUserInfo `json:"users"`
}
func GetUsernameByID(userID *string) (*string, error) {
value, found := userIDCacheObject.Get("id-" + *userID)
if found == false {
usersString := "https://api.twitch.tv/kraken/users/" + *userID
resp, error := httpclient.TwitchV5(*userID, "GET", usersString, nil)
if error != nil {
return nil, error
}
if resp != nil {
defer resp.Body.Close()
}
var twitchUser twitchUserInfo
marshallError := json.NewDecoder(resp.Body).Decode(&twitchUser)
if marshallError != nil {
return nil, marshallError
}
if twitchUser.Name == "" {
return nil, errors.New("Not found")
}
userName := strings.ToLower(twitchUser.Name)
userIDCacheObject.Set("username-"+userName, *userID, 0)
userIDCacheObject.Set("id-"+*userID, userName, 0)
return &userName, nil
} else {
result := value.(string)
return &result, nil
}
}
func GetUsersID(users *[]string) (*map[string]string, error) {
notFoundUsers := []string{}
result := make(map[string]string)
//log.Printf("Input users: %d", len(*users))
////log.Println(*users)
for _, user := range *users {
value, found := userIDCacheObject.Get("username-" + user)
if found {
stringValue := value.(string)
if stringValue != "rejected" {
result[user] = stringValue
}
} else {
notFoundUsers = append(notFoundUsers, user)
}
}
if len(notFoundUsers) == 0 {
//log.Printf("Found users: %d", len(result))
return &result, nil
}
sliceStart := 0
//log.Printf("Found users: %d", len(result))
//log.Printf("Unfound users: %d", len(notFoundUsers))
log.Println(notFoundUsers)
for index, _ := range notFoundUsers {
if index == len(notFoundUsers)-1 || index-sliceStart > 48 {
////log.Printf("%d - %d", sliceStart, index)
usersString := "https://api.twitch.tv/kraken/users?login=" + strings.Join(notFoundUsers[sliceStart:index+1], ",")
//log.Printf(usersString)
resp, error := httpclient.TwitchV5(Config.ClientID, "GET", usersString, nil)
if error != nil {
return nil, error
}
if resp != nil {
defer resp.Body.Close()
}
var usersWithID usersLoginStruct
marshallError := json.NewDecoder(resp.Body).Decode(&usersWithID)
if marshallError != nil {
return nil, marshallError
}
//log.Printf("That request returned %d users", len(usersWithID.Users))
//log.Println(usersWithID)
for _, user := range usersWithID.Users {
result[user.DisplayName] = user.ID
userIDCacheObject.Set("username-"+strings.ToLower(user.Name), user.ID, 0)
userIDCacheObject.Set("id-"+user.ID, strings.ToLower(user.Name), 600*time.Minute)
}
if len(usersWithID.Users) == 0 {
//log.Println(len(notFoundUsers[sliceStart : index+1]))
//log.Println(sliceStart)
//log.Println(index + 1)
for _, user := range notFoundUsers[sliceStart : index+1] {
userIDCacheObject.Set("username-"+user, "rejected", 600*time.Minute)
}
}
sliceStart = index
}
}
// if len(result) != len(*users) {
// for _, user := range *users {
// if result[user] == "" {
// }
// }
// }
//log.Printf("Returning %d users", len(result))
return &result, nil
}