1
1
# -*- coding: utf-8 -*-
2
2
3
3
from dataclasses import dataclass
4
+ from typing import Literal
4
5
import cloudscraper
5
6
from rich .console import Console
6
7
import json
@@ -15,11 +16,7 @@ def print_by_char_rich(console, text, delay=0.002):
15
16
@dataclass
16
17
class GrokAccount :
17
18
cookies : dict
18
- message : str
19
19
headers : dict
20
- NewChat : bool = True
21
- ChatID : str = None
22
- modelName : str = "grok-2"
23
20
24
21
25
22
class GrokAI :
@@ -45,31 +42,33 @@ class GrokAI:
45
42
def __init__ (self ,GrokAccount : GrokAccount ):
46
43
self .GrokAccount = GrokAccount
47
44
self .Console = Console ()
48
- obj = self .GrokAccount
45
+ # obj = self.GrokAccount
49
46
50
- has_new_chat = obj .NewChat # 检查 NewChat 是否为 True
51
- has_chat_id = obj .ChatID is not None # 检查 ChatID 是否非 None
47
+
48
+
49
+ def Chat (self ,meassge : str ,disableSearch : bool = False , enableImageGeneration : bool = True , isReasoning : bool = False ,debug : bool = False ,NewChat :bool = True ,ChatID :str = None ,ModelName : Literal ['grok-3' ,'grok-latest' ] = 'grok-latest' ,deepsearch : Literal ['deepsearch,deepersearch' ,None ] = None ) -> None :
50
+ has_new_chat = NewChat # 检查 NewChat 是否为 True
51
+ has_chat_id = ChatID is not None # 检查 ChatID 是否非 None
52
52
if has_new_chat and has_chat_id :
53
53
raise ValueError ("NewChat 和 ChatID 不能同时为 True 和非 None" )
54
54
55
- def Chat (self , disableSearch : bool = False , enableImageGeneration : bool = True , imageGenerationCount : int = 2 , isReasoning : bool = False ,test : bool = False ):
56
55
self .cookies = self .GrokAccount .cookies
57
- self .message = self . GrokAccount . message
58
- self .modelName = self . GrokAccount . modelName
59
- self .NewChat = self . GrokAccount . NewChat
60
- self .ChatID = self . GrokAccount . ChatID
56
+ self .message = meassge
57
+ self .modelName = ModelName
58
+ self .NewChat = NewChat
59
+ self .ChatID = ChatID
61
60
self .headers = self .GrokAccount .headers
62
61
self .headers ['referer' ] = 'https://grok.com/chat/'
63
62
self .responseUrl : str
64
63
global has_valid_response
65
64
has_valid_response = False
66
65
67
- scraper = cloudscraper .create_scraper ()
66
+ scraper = cloudscraper .create_scraper (delay = 10 , browser = { 'browser' : 'chrome' , 'platform' : 'windows' , 'mobile' : False } )
68
67
69
68
if self .NewChat is False :
70
69
self .url = f"https://grok.com/rest/app-chat/conversations/{ self .ChatID } /responses"
71
70
response = scraper .get (f"https://grok.com/rest/app-chat/conversations/{ self .ChatID } /response-node" )
72
- ''' if response.status_code == 200:
71
+ if response .status_code == 200 :
73
72
response_data = response .json ()
74
73
response_nodes = response_data .get ("responseNodes" , [])
75
74
if response_nodes :
@@ -81,7 +80,7 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
81
80
responseId = None
82
81
else :
83
82
print ("No responseNodes found in the JSON data." )
84
- responseId = None'''
83
+ responseId = None
85
84
else :
86
85
self .url = "https://grok.com/rest/app-chat/conversations/new"
87
86
@@ -96,7 +95,7 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
96
95
'returnRawGrokInXaiRequest' : False ,
97
96
'fileAttachments' : [],
98
97
'enableImageStreaming' : True ,
99
- 'imageGenerationCount' : imageGenerationCount ,
98
+ 'imageGenerationCount' : 2 ,
100
99
'forceConcise' : False ,
101
100
'toolOverrides' : {},
102
101
'enableSideBySide' : True ,
@@ -106,18 +105,21 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
106
105
'disableTextFollowUps' : True
107
106
}
108
107
108
+ if deepsearch is not None :
109
+ self .data ["deepsearchPreset" ] = deepsearch
110
+
109
111
console = self .Console
110
112
# 创建 cloudscraper 实例
111
113
112
114
response = scraper .post (self .url , headers = self .headers , cookies = self .cookies , json = self .data , stream = True )
113
- if test is True :
115
+ if debug is True :
114
116
# 這是測試代碼!!!!
115
117
console .print (f"Request URL: { self .url } " , style = "bold blue" )
116
118
console .print (f"Headers: { self .headers } " , style = "bold blue" )
117
119
console .print (f"Cookies: { self .cookies } " , style = "bold blue" )
118
120
console .print (f"Data: { self .data } " , style = "bold blue" )
119
121
120
-
122
+ text = response . text
121
123
# 檢查狀態碼
122
124
if response .status_code == 200 :
123
125
for line in response .iter_lines ():
@@ -129,12 +131,13 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
129
131
130
132
# 提取 token
131
133
token = json_data .get ("result" , {}).get ("response" , {}).get ("token" , "" )
132
-
134
+ generated_image_urls = json_data . get ( "result" , {}). get ( "modelResponse" , {}). get ( "generatedImageUrls" , [])
133
135
# 如果 token 存在,逐字輸出
134
- if token :
136
+ if token or generated_image_urls :
137
+ # 如果有圖片網址,打印圖片網址
135
138
136
139
has_valid_response = True
137
- print_by_char_rich (console , token , delay = 0.05 )
140
+ print_by_char_rich (console , token if token else generated_image_urls , delay = 0.05 )
138
141
139
142
except json .JSONDecodeError :
140
143
console .print ("解析 JSON 失败" , style = "bold red" )
@@ -146,10 +149,14 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
146
149
else :
147
150
console .print ("请求失败" , style = "bold red" )
148
151
console .print (response .status_code , style = "bold red" )
149
- if "Just a moment..." in response . text or "<title>Just a moment...</title>" in response . text :
152
+ if "Just a moment..." in text or "<title>Just a moment...</title>" in text :
150
153
console .print ("請求被 Cloudflare 防護攔截,請更新 Cookies 或 Headers。" , style = "bold red" )
151
154
else :
152
- console .print (response .text , style = "bold red" )
155
+ console .print (text , style = "bold red" )
156
+
157
+ if debug is True :
158
+ # 這是測試代碼!!!!
159
+ console .print (text , style = "bold yellow" )
153
160
'''
154
161
for line in response.iter_lines():
155
162
if line:
0 commit comments