2
2
3
3
import re
4
4
import json
5
- import base64
6
5
import uuid
7
6
8
7
from ..typing import AsyncResult , Messages , ImageType , Cookies
9
8
from .base_provider import AsyncGeneratorProvider , ProviderModelMixin
10
9
from .helper import format_prompt
11
10
from ..image import ImageResponse , ImagePreview , EXTENSIONS_MAP , to_bytes , is_accepted_format
12
- from ..requests import StreamSession , FormData , raise_for_status
13
- from .you .har_file import get_telemetry_ids
11
+ from ..requests import StreamSession , FormData , raise_for_status , get_nodriver
12
+ from ..cookies import get_cookies
13
+ from ..errors import MissingRequirementsError
14
14
from .. import debug
15
15
16
16
class You (AsyncGeneratorProvider , ProviderModelMixin ):
@@ -57,6 +57,7 @@ async def create_async_generator(
57
57
proxy : str = None ,
58
58
timeout : int = 240 ,
59
59
chat_mode : str = "default" ,
60
+ cookies : Cookies = None ,
60
61
** kwargs ,
61
62
) -> AsyncResult :
62
63
if image is not None or model == cls .default_vision_model :
@@ -69,12 +70,22 @@ async def create_async_generator(
69
70
else :
70
71
chat_mode = "custom"
71
72
model = cls .get_model (model )
73
+ if cookies is None and chat_mode != "default" :
74
+ try :
75
+ cookies = get_cookies (".you.com" )
76
+ except MissingRequirementsError :
77
+ browser = await get_nodriver (proxy = proxy )
78
+ page = await browser .get (cls .url )
79
+ await page .wait_for ('[data-testid="user-profile-button"]' , timeout = 900 )
80
+ cookies = {}
81
+ for c in await page .send (nodriver .cdp .network .get_cookies ([cls .url ])):
82
+ cookies [c .name ] = c .value
83
+ await page .close ()
72
84
async with StreamSession (
73
85
proxy = proxy ,
74
86
impersonate = "chrome" ,
75
87
timeout = (30 , timeout )
76
88
) as session :
77
- cookies = await cls .get_cookies (session ) if chat_mode != "default" else None
78
89
upload = ""
79
90
if image is not None :
80
91
upload_file = await cls .upload_file (
@@ -156,65 +167,4 @@ async def upload_file(cls, client: StreamSession, cookies: Cookies, file: bytes,
156
167
result = await response .json ()
157
168
result ["user_filename" ] = filename
158
169
result ["size" ] = len (file )
159
- return result
160
-
161
- @classmethod
162
- async def get_cookies (cls , client : StreamSession ) -> Cookies :
163
- if not cls ._cookies or cls ._cookies_used >= 5 :
164
- cls ._cookies = await cls .create_cookies (client )
165
- cls ._cookies_used = 0
166
- cls ._cookies_used += 1
167
- return cls ._cookies
168
-
169
- @classmethod
170
- def get_sdk (cls ) -> str :
171
- return base64 .standard_b64encode (json .dumps ({
172
- "event_id" :f"event-id-{ str (uuid .uuid4 ())} " ,
173
- "app_session_id" :f"app-session-id-{ str (uuid .uuid4 ())} " ,
174
- "persistent_id" :f"persistent-id-{ uuid .uuid4 ()} " ,
175
- "client_sent_at" :"" ,"timezone" :"" ,
176
- "stytch_user_id" :f"user-live-{ uuid .uuid4 ()} " ,
177
- "stytch_session_id" :f"session-live-{ uuid .uuid4 ()} " ,
178
- "app" :{"identifier" :"you.com" },
179
- "sdk" :{"identifier" :"Stytch.js Javascript SDK" ,"version" :"3.3.0"
180
- }}).encode ()).decode ()
181
-
182
- def get_auth () -> str :
183
- auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c819"
184
- auth_token = f"public-token-live-{ auth_uuid } :public-token-live-{ auth_uuid } "
185
- auth = base64 .standard_b64encode (auth_token .encode ()).decode ()
186
- return f"Basic { auth } "
187
-
188
- @classmethod
189
- async def create_cookies (cls , client : StreamSession ) -> Cookies :
190
- if not cls ._telemetry_ids :
191
- cls ._telemetry_ids = await get_telemetry_ids ()
192
- user_uuid = str (uuid .uuid4 ())
193
- telemetry_id = cls ._telemetry_ids .pop ()
194
- if debug .logging :
195
- print (f"Use telemetry_id: { telemetry_id } " )
196
- async with client .post (
197
- "https://web.stytch.com/sdk/v1/passwords" ,
198
- headers = {
199
- "Authorization" : cls .get_auth (),
200
- "X-SDK-Client" : cls .get_sdk (),
201
- "X-SDK-Parent-Host" : cls .url ,
202
- "Origin" : "https://you.com" ,
203
- "Referer" : "https://you.com/"
204
- },
205
- json = {
206
- "dfp_telemetry_id" : telemetry_id ,
207
- "email" : f"{ user_uuid } @gmail.com" ,
208
- "password" : f"{ user_uuid } #{ user_uuid } " ,
209
- "session_duration_minutes" : 129600
210
- }
211
- ) as response :
212
- await raise_for_status (response )
213
- session = (await response .json ())["data" ]
214
-
215
- return {
216
- "stytch_session" : session ["session_token" ],
217
- 'stytch_session_jwt' : session ["session_jwt" ],
218
- 'ydc_stytch_session' : session ["session_token" ],
219
- 'ydc_stytch_session_jwt' : session ["session_jwt" ],
220
- }
170
+ return result
0 commit comments