Skip to content

Commit 703d1d8

Browse files
committed
feat: auto refresh cookie
1 parent 189103a commit 703d1d8

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
echo "Building for Linux.."
2+
GOOS=linux GOARCH=amd64 go build -o oa-helper_linux_amd64 .
3+
GOOD=linux GOARCH=arm64 go build -o oa-helper_linux_arm64 .
4+
echo "Building for Windows.."
5+
GOOS=windows GOARCH=amd64 go build -o oa-helper_windows_amd64.exe .
6+
echo "Building for Mac.."
7+
GOOS=darwin GOARCH=amd64 go build -o oa-helper_darwin_amd64 .
8+
GOOS=darwin GOARCH=arm64 go build -o oa-helper_darwin_arm64 .

main.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,19 @@ func main() {
1616
panic(err)
1717
}
1818
InitChatGPTClient(model.Conf.ApiKey)
19+
i := 0
1920
for true {
2021
err = StartTask()
2122
if err != nil {
2223
logx.Error("Error while doing task: ", err)
2324
}
25+
if i++; i == 10 {
26+
i = 0
27+
err = RefreshCookie()
28+
if err != nil {
29+
logx.Error("Error while refreshing cookie: ", err)
30+
}
31+
}
2432
time.Sleep(2 * time.Second)
2533
}
26-
// s, err := GetLabelsFromChatGPT(`{
27-
// "spam": 0,
28-
// "fails_task": 0,
29-
// "lang_mismatch": 0,
30-
// "not_appropriate": 0,
31-
// "pii": 0,
32-
// "hate_speech": 0,
33-
// "sexual_content": 0,
34-
// "quality": 0.75,
35-
// "helpfulness": 1,
36-
// "creativity": 0.5,
37-
// "humor": 0,
38-
// "toxicity": 0,
39-
// "violence": 0
40-
// }`)
41-
// if err != nil {
42-
// panic(err)
43-
// }
44-
// for l, v := range s {
45-
// println(l, " ", v)
46-
// }
4734
}

model/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,16 @@ func LoadConfig() error {
9595

9696
return nil
9797
}
98+
99+
func UpdateCookie(cookie string) error {
100+
Conf.OaCookie = cookie
101+
file, err := json.MarshalIndent(Conf, "", " ")
102+
if err != nil {
103+
return err
104+
}
105+
err = os.WriteFile("config.json", file, 0644)
106+
if err != nil {
107+
return err
108+
}
109+
return nil
110+
}

open-assistant.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/zeromicro/go-zero/core/jsonx"
88
"github.com/zeromicro/go-zero/core/logx"
99
"open-assistant-helper-go/model"
10+
"strings"
1011
)
1112

1213
var rty = resty.New()
@@ -28,6 +29,24 @@ func CancelTask(id string) error {
2829
return nil
2930
}
3031

32+
func RefreshCookie() error {
33+
logx.Infof("RefreshCookie")
34+
resp, err := rty.R().
35+
SetHeaders(map[string]string{
36+
"cookie": model.Conf.OaCookie,
37+
}).
38+
Get("https://open-assistant.io/api/auth/session")
39+
if err != nil {
40+
return err
41+
}
42+
cs := resp.Header()["Set-Cookie"]
43+
if len(cs) == 0 {
44+
return fmt.Errorf("no cookie")
45+
}
46+
c := strings.Join(cs, "")
47+
return model.UpdateCookie(c)
48+
}
49+
3150
func GetLabelsFromChatGPT(resp string) (map[model.OALabel]float32, error) {
3251
var j map[string]interface{}
3352
err := jsonx.Unmarshal([]byte(resp), &j)

0 commit comments

Comments
 (0)