@@ -42,14 +42,15 @@ class GrokAI:
42
42
# 检查 NewChat 和 ChatID 是否都有值
43
43
def __init__ (self ,GrokAccount : GrokAccount ):
44
44
self .GrokAccount = GrokAccount
45
+ self .Console = Console ()
45
46
obj = self .GrokAccount
46
47
47
48
has_new_chat = obj .NewChat # 检查 NewChat 是否为 True
48
49
has_chat_id = obj .ChatID is not None # 检查 ChatID 是否非 None
49
50
if has_new_chat and has_chat_id :
50
51
raise ValueError ("NewChat 和 ChatID 不能同时为 True 和非 None" )
51
52
52
- def Chat (self , disableSearch : bool = False , enableImageGeneration : bool = True , imageGenerationCount : int = 2 , isReasoning : bool = False ):
53
+ def Chat (self , disableSearch : bool = False , enableImageGeneration : bool = True , imageGenerationCount : int = 2 , isReasoning : bool = False , test : bool = False ):
53
54
self .cookies = self .GrokAccount .cookies
54
55
self .message = self .GrokAccount .message
55
56
self .modelName = self .GrokAccount .modelName
@@ -85,26 +86,49 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
85
86
'disableTextFollowUps' : True
86
87
}
87
88
88
- console = Console ()
89
+ console = self .Console
90
+ # 创建 cloudscraper 实例
89
91
scraper = cloudscraper .create_scraper ()
90
92
response = scraper .post (self .url , headers = self .headers , cookies = self .cookies , json = self .data , stream = True )
93
+ if test is True :
94
+ # 這是測試代碼!!!!
95
+ console .print (f"Request URL: { self .url } " , style = "bold blue" )
96
+ console .print (f"Headers: { self .headers } " , style = "bold blue" )
97
+ console .print (f"Cookies: { self .cookies } " , style = "bold blue" )
98
+ console .print (f"Data: { self .data } " , style = "bold blue" )
99
+
91
100
92
101
# 檢查狀態碼
93
102
if response .status_code == 200 :
94
103
for line in response .iter_lines ():
95
104
if line :
96
105
decoded_line = line .decode ('utf-8' )
97
106
try :
107
+ # 解析 JSON
98
108
json_data = json .loads (decoded_line )
99
- token = json_data .get ('result' , {}).get ('token' , '' )
100
- if token : # 只打印非空 token
109
+
110
+ # 提取 token
111
+ token = json_data .get ("result" , {}).get ("response" , {}).get ("token" , "" )
112
+
113
+ # 如果 token 存在,逐字輸出
114
+ if token :
101
115
print_by_char_rich (console , token , delay = 0.05 )
116
+
102
117
except json .JSONDecodeError :
103
118
console .print ("解析 JSON 失败" , style = "bold red" )
104
- continue
105
119
else :
106
120
console .print ("请求失败" , style = "bold red" )
107
121
console .print (response .text )
122
+ '''
123
+ for line in response.iter_lines():
124
+ if line:
125
+ decoded_line = line.decode('utf-8')
126
+ try:
127
+ json_data = json.loads(decoded_line)
128
+ print(json_data)
129
+ except json.JSONDecodeError:
130
+ print("解析 JSON 失败", style="bold red")
131
+ continue'''
108
132
109
133
'''
110
134
@@ -203,5 +227,4 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
203
227
else:
204
228
console.print("请求失败", style="bold red")
205
229
console.print(response.text)
206
-
207
230
'''
0 commit comments