/
main.py
141 lines (116 loc) · 4.34 KB
/
main.py
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
import json
import time
from curl_cffi import requests
class Claude:
def __init__(self,org_uuid,con_uuid,cookie):
self.org_uuid = org_uuid
self.con_uuid = con_uuid
self.cookie = cookie
def send_message(self, query):
# sentry_trace = self.ctx.call('Sentry_Trace')
# traceid = sentry_trace.split("-")[0]
url = "https://claude.ai/api/append_message"
payload = json.dumps({
"completion": {
"prompt": f"{query}",
"timezone": "Asia/Kolkata",
"model": "claude-2"
},
"organization_uuid": f"{self.org_uuid}",
"conversation_uuid": f"{self.con_uuid}",
"text": f"{query}",
"attachments": []
})
headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept': 'text/event-stream, text/event-stream',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Origin': 'https://claude.ai',
'DNT': '1',
'Connection': 'keep-alive',
'Cookie': f'{self.cookie}',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'TE': 'trailers'
}
try:
response = requests.post(url, headers=headers, data=payload,impersonate="chrome110",proxies=proxies,timeout=400)
print(response.text)
except Exception as error:
print(error)
return False
if len(response.text) >=60:
return True
return False
def chat_conversation_history(self):
url = f"https://claude.ai/api/organizations/{self.org_uuid}/chat_conversations/{self.con_uuid}"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
'Cookie': f'{self.cookie}'
}
response = requests.get( url, headers=headers, proxies =proxies,impersonate="chrome110")
response.encoding = 'utf-8'
return response.json()
def get_last_answer(history,cache):
max_index = 0
for i in history["chat_messages"]:
if i["index"] > max_index:
max_index = i["index"]
body = i
if max_index not in cache:
cache.append(max_index)
answer = body["text"]
return answer,cache
return None,cache
def main(org_uuid,con_uuid,cookie):
claude = Claude(org_uuid, con_uuid,cookie)
cache = []
while True:
query = input("human:")
result = claude.send_message(query)
if result:
print("send success")
else:
continue
try_time_current = 0
#最大重试次数
try_time_max = 5
while True:
history = claude.chat_conversation_history()
answer,cache = get_last_answer(history, cache)
print("cache :" ,cache)
if answer == None or answer == query:
time.sleep(1)
try_time_current += 1
if try_time_current > try_time_max:
answer = "消息获取失败"
break
continue
else:
break
print("robot: ", answer)
if __name__ == '__main__':
# 填写自己代理服务器的地址 如: 自行修改
proxy_url = "http://127.0.0.1:33210"
# 构建代理字典,键是协议(http、https等),值是代理服务器地址
proxies = {
"http": proxy_url,
"https": proxy_url
}
#org_uuid= ###测试时取消注释
#con_uuid= ###
#cookie = ###
# claude = Claude(org_uuid, con_uuid,cookie)
main(org_uuid,con_uuid,cookie)