Skip to content

Commit

Permalink
Add MissingRequirementsError to You
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Apr 21, 2024
1 parent db2d6cf commit f66cd9f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
recursive-include g4f/gui/server *
recursive-include g4f/gui/client *
recursive-include g4f/Provider/npm *
recursive-include g4f/Provider/gigachat_crt *
recursive-include g4f/Provider/gigachat_crt *
recursive-include g4f/Provider/you *
6 changes: 5 additions & 1 deletion g4f/Provider/You.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..image import ImageResponse, to_bytes, is_accepted_format
from ..requests import StreamSession, FormData, raise_for_status
from .you.har_file import get_telemetry_ids
from .. import debug

class You(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://you.com"
Expand Down Expand Up @@ -163,6 +164,9 @@ async def create_cookies(cls, client: StreamSession) -> Cookies:
if not cls._telemetry_ids:
cls._telemetry_ids = await get_telemetry_ids()
user_uuid = str(uuid.uuid4())
telemetry_id = cls._telemetry_ids.pop()
if debug.logging:
print(f"Use telemetry_id: {telemetry_id}")
async with client.post(
"https://web.stytch.com/sdk/v1/passwords",
headers={
Expand All @@ -173,7 +177,7 @@ async def create_cookies(cls, client: StreamSession) -> Cookies:
"Referer": "https://you.com/"
},
json={
"dfp_telemetry_id": cls._telemetry_ids.pop(),
"dfp_telemetry_id": telemetry_id,
"email": f"{user_uuid}@gmail.com",
"password": f"{user_uuid}#{user_uuid}",
"session_duration_minutes": 129600
Expand Down
14 changes: 4 additions & 10 deletions g4f/Provider/you/har_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import json
import os
import os.path
import random
import requests

from ...requests import StreamSession, raise_for_status
from ...errors import MissingRequirementsError
from ... import debug

class NoValidHarFileError(Exception):
Expand Down Expand Up @@ -71,21 +73,13 @@ async def get_dfp_telemetry_id(proxy: str = None):
chatArks = readHAR()
return await sendRequest(random.choice(chatArks), proxy)

def read_telemetry_file() -> list:
with open("hardir/you.com_telemetry_ids.txt", "r") as f:
ids = f.readlines()
random.shuffle(ids)
return ids

async def get_telemetry_ids(proxy: str = None) -> list:
if debug.logging:
print('Getting telemetry_id for you.com with nodriver')
try:
from nodriver import start
except ImportError:
if debug.logging:
print('Install "nodriver" package | pip install -U nodriver')
return read_telemetry_file()
raise MissingRequirementsError('Install "nodriver" package | pip install -U nodriver')
try:
browser = await start()
tab = browser.main_tab
Expand All @@ -103,7 +97,7 @@ async def get_telemetry_id():
# with open("hardir/you.com_telemetry_ids.txt", "a") as f:
# f.write((await get_telemetry_id()) + "\n")

return [await get_telemetry_id() for _ in range(10)]
return [await get_telemetry_id() for _ in range(4)]
finally:
try:
await tab.close()
Expand Down

0 comments on commit f66cd9f

Please sign in to comment.