This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
forked from lwydyby/poe-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
186 lines (167 loc) · 4.08 KB
/
utils.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package poe_api
import (
"encoding/json"
"math/rand"
"os"
"path/filepath"
"github.com/google/uuid"
)
func GetFinalResponse(ch <-chan map[string]interface{}) string {
var m map[string]interface{}
for message := range ch {
m = message
if message["state"] != "complete" {
continue
}
return message["text"].(string)
}
return m["text"].(string)
}
func GetTextStream(ch <-chan map[string]interface{}) <-chan string {
stream := make(chan string, 1)
go func() {
for message := range ch {
stream <- message["text_new"].(string)
}
close(stream)
}()
return stream
}
func generatePayload(queryName string, variables interface{}) interface{} {
if queryName == "recv" {
if rand.Float64() > 0.9 {
return []map[string]interface{}{
{
"category": "poe/bot_response_speed",
"data": variables,
},
{
"category": "poe/statsd_event",
"data": map[string]interface{}{
"key": "poe.speed.web_vitals.INP",
"value": rand.Intn(26) + 100,
"category": "time",
"path": "/[handle]",
"extra_data": map[string]interface{}{},
},
},
}
} else {
return []map[string]interface{}{
{
"category": "poe/bot_response_speed",
"data": variables,
},
}
}
}
extension := "61c1bfa1ba167fd0857e3f6eaf9699e847e6c3b09d69926b12b5390076fe36e6"
if queryName == "chatHelpers_sendMessageMutation_Mutation" {
extension = "5fd489242adf25bf399a95c6b16de9665e521b76618a97621167ae5e11e4bce4"
}
return &Payload{
// "query": queries[queryName],
QueryName: queryName,
Variables: variables,
Extensions: map[string]interface{}{
"hash": extension,
},
}
}
func generateNonce(length int) string {
if length == 0 {
length = 16
}
const lettersAndDigits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var nonce = make([]rune, length)
for i := range nonce {
nonce[i] = rune(lettersAndDigits[rand.Intn(len(lettersAndDigits))])
}
return string(nonce)
}
func getConfigPath() string {
var configPath string
if os.PathSeparator == '\\' {
configPath = filepath.Join(os.Getenv("APPDATA"), "poe-api")
} else {
configPath = filepath.Join(os.Getenv("HOME"), ".config", "poe-api")
}
return configPath
}
func setSavedDeviceID(userID, deviceID string) {
deviceIDPath := filepath.Join(getConfigPath(), "device_id.json")
deviceIDs := make(map[string]string)
if _, err := os.Stat(deviceIDPath); !os.IsNotExist(err) {
deviceIDBytes, err := os.ReadFile(deviceIDPath)
if err != nil {
panic(err)
}
err = json.Unmarshal(deviceIDBytes, &deviceIDs)
if err != nil {
panic(err)
}
}
deviceIDs[userID] = deviceID
err := os.MkdirAll(filepath.Dir(deviceIDPath), os.ModePerm)
if err != nil {
panic(err)
}
deviceIDBytes, err := json.MarshalIndent(deviceIDs, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(deviceIDPath, deviceIDBytes, 0644)
if err != nil {
panic(err)
}
}
func getSavedDeviceID(userID string) string {
deviceIDPath := filepath.Join(getConfigPath(), "device_id.json")
deviceIDs := make(map[string]string)
if _, err := os.Stat(deviceIDPath); !os.IsNotExist(err) {
deviceIDBytes, err := os.ReadFile(deviceIDPath)
if err != nil {
panic(err)
}
err = json.Unmarshal(deviceIDBytes, &deviceIDs)
if err != nil {
panic(err)
}
}
if deviceID, ok := deviceIDs[userID]; ok {
return deviceID
}
deviceID := uuid.New().String()
deviceIDs[userID] = deviceID
err := os.MkdirAll(filepath.Dir(deviceIDPath), os.ModePerm)
if err != nil {
panic(err)
}
deviceIDBytes, err := json.MarshalIndent(deviceIDs, "", " ")
if err != nil {
panic(err)
}
err = os.WriteFile(deviceIDPath, deviceIDBytes, 0644)
if err != nil {
panic(err)
}
return deviceID
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func reverseSlice(s []map[string]interface{}) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func containKey(key string, m map[string]interface{}) bool {
_, ok := m[key]
return ok
}
func getMap(m map[string]interface{}, key string) map[string]interface{} {
return m[key].(map[string]interface{})
}