-
Notifications
You must be signed in to change notification settings - Fork 11
/
claude.goo
178 lines (167 loc) · 7.99 KB
/
claude.goo
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
package main
import (
_ "embed"
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/stealth"
"github.com/google/uuid"
"github.com/tidwall/gjson"
"strconv"
"strings"
"time"
)
var page_claude *rod.Page
var relogin_claude = true
var channel_claude chan string
func Claude2() {
channel_claude = make(chan string)
defer func() {
if err := recover(); err != nil {
relogin_claude = true
}
}()
//page_claude = browser.MustPage("https://claude.ai")
page_claude = stealth.MustPage(browser)
page_claude.MustNavigate("https://claude.ai/api/organizations").MustWaitLoad()
org_json := page_claude.MustElementX("//pre").MustText()
org_uuid := gjson.Get(string(org_json), "0.uuid").String()
time.Sleep(6 * time.Second)
new_uuid := uuid.New().String()
new_uuid_url := "https://claude.ai/api/organizations/" + org_uuid + "/chat_conversations"
create_new_converastion_json := `{"uuid":"` + new_uuid + `","name":""}`
create_new_converastion_js := `
(new_uuid_url, sdata) => {
var xhr = new XMLHttpRequest();
xhr.open("POST", new_uuid_url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Referer', 'https://claude.ai/chats');
xhr.setRequestHeader('Origin', 'https://claude.ai');
xhr.setRequestHeader('TE', 'trailers');
xhr.setRequestHeader('DNT', '1');
xhr.setRequestHeader('Connection', 'keep-alive');
xhr.setRequestHeader('Accept', 'text/event-stream, text/event-stream');
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var res_text = xhr.responseText;
console.log(res_text);
}
}
console.log(sdata);
xhr.send(sdata);
}
`
page_claude.MustEval(create_new_converastion_js, new_uuid_url, create_new_converastion_json).Str()
// posted new conversation uuid
time.Sleep(3 * time.Second) // delay to simulate human being
var record_chat_messages string
var response_chat_messages string
for i := 1; i <= 20; i++ {
create_json := page_claude.MustNavigate("https://claude.ai/api/organizations/" + org_uuid + "/chat_conversations/" + new_uuid).MustElementX("//pre").MustText()
message_uuid := gjson.Get(string(create_json), "uuid").String()
if message_uuid == new_uuid {
//fmt.Println("create_conversation success...")
relogin_claude = false
record_chat_messages = gjson.Get(string(create_json), "chat_messages").String()
break
}
time.Sleep(2 * time.Second)
}
if relogin_claude == true {
sprint("✘ Claude")
//page_claude.MustPDF("./tmp/Claude✘.pdf")
}
if relogin_claude == false {
sprint("✔ Claude")
for {
select {
case question := <-channel_claude:
question = strings.Replace(question, "\r", "\n", -1)
question = strings.Replace(question, "\"", "\\\"", -1)
question = strings.Replace(question, "\n", "\\n", -1)
question = strings.TrimSuffix(question, "\n")
// re-activate
page_claude.MustNavigate("https://claude.ai/api/account/statsig/" + org_uuid).MustWaitLoad()
record_json := page_claude.MustNavigate("https://claude.ai/api/organizations/" + org_uuid + "/chat_conversations/" + new_uuid).MustElementX("//pre").MustText()
record_chat_messages = gjson.Get(string(record_json), "chat_messages").String()
record_count := gjson.Get(string(response_chat_messages), "#").Int()
page_claude.MustNavigate("https://claude.ai/api/organizations/" + org_uuid).MustWaitLoad()
time.Sleep(3 * time.Second) // delay to simulate human being
//question = strings.Replace(question, `"`, `\"`, -1) // escape " in input text when code into json
//d := `{"completion":{"prompt":"` + question + `","timezone":"Asia/Shanghai","model":"claude-2"},"organization_uuid":"` + org_uuid + `","conversation_uuid":"` + new_uuid + `","text":"` + question + `","attachments":[]}`
d := `{"prompt":"` + question + `","timezone":"Asia/Bangkok","model":"claude-2.1","attachments":[],"files":[]}`
//fmt.Println(d)
//js := `
// (sdata, new_uuid) => {
// var xhr = new XMLHttpRequest();
// xhr.open("POST", "https://claude.ai/api/append_message");
// xhr.setRequestHeader('Content-Type', 'application/json');
// xhr.setRequestHeader('Referer', 'https://claude.ai/chat/new_uuid');
// xhr.setRequestHeader('Origin', 'https://claude.ai');
// xhr.setRequestHeader('TE', 'trailers');
// xhr.setRequestHeader('Connection', 'keep-alive');
// xhr.setRequestHeader('Accept', 'text/event-stream, text/event-stream');
// xhr.onreadystatechange = function() {
// if (xhr.readyState == XMLHttpRequest.DONE) {
// var res_text = xhr.responseText;
// console.log(res_text);
// }
// }
// console.log(sdata);
// xhr.send(sdata);
// }
// `
js := `
(sdata, org_uuid, new_uuid) => {
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://claude.ai/api/organizations/org_uuid/chat_conversations/new_uuid/completion");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Referer', 'https://claude.ai/chat/new_uuid');
xhr.setRequestHeader('Origin', 'https://claude.ai');
xhr.setRequestHeader('TE', 'trailers');
xhr.setRequestHeader('Connection', 'keep-alive');
xhr.setRequestHeader('Accept', 'text/event-stream, text/event-stream');
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var res_text = xhr.responseText;
console.log(res_text);
}
}
console.log(sdata);
xhr.send(sdata);
}
`
page_claude.MustEval(js, d, org_uuid, new_uuid).Str()
fmt.Println("Claude generating...", role)
if role == ".all" {
channel_claude <- "click_claude"
}
time.Sleep(3 * time.Second) // delay to simulate human being
// wait answer
var claude_response = false
var response_json string
for i := 1; i <= 20; i++ {
if page_claude.MustNavigate("https://claude.ai/api/organizations/" + org_uuid + "/chat_conversations/" + new_uuid).MustHasX("//pre") {
response_json = page_claude.MustNavigate("https://claude.ai/api/organizations/" + org_uuid + "/chat_conversations/" + new_uuid).MustElementX("//pre").MustText()
response_chat_messages = gjson.Get(string(response_json), "chat_messages").String()
count := gjson.Get(string(response_chat_messages), "#").Int()
if response_chat_messages != record_chat_messages && count == record_count+2 {
claude_response = true
record_chat_messages = response_chat_messages
break
}
}
time.Sleep(3 * time.Second)
}
if claude_response == true {
count := gjson.Get(string(response_chat_messages), "#").Int()
answer := gjson.Get(string(response_json), "chat_messages.#(index=="+strconv.FormatInt(count-1, 10)+").text").String()
channel_claude <- answer
} else {
channel_claude <- "✘✘ Claude, Please check the internet connection and verify login status."
relogin_claude = true
//page_claude.MustPDF("./tmp/Claude✘.pdf")
}
}
}
}
}