-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
294 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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") | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.