File size: 2,591 Bytes
2ca28b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884c66a
2ca28b3
 
 
 
 
 
 
 
 
 
 
 
 
ba2dbb6
96c650a
2ca28b3
8e847a8
 
 
 
 
2ca28b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from uuid import uuid4
from re import findall
import tls_client

class Completion:
    async def create(self, prompt):
        """
        Create a completion for the given prompt using the you.com API.

        Args:
            prompt (str): The prompt for which completion is requested.
            proxy (str, optional): The proxy to be used for the API request. Defaults to None.

        Returns:
            str: The completion result as a string.

        Raises:
            Exception: If unable to fetch the response or the required token from the response.
        """
        client = tls_client.Session(client_identifier="firefox108")
        client.headers = {
            "authority": "you.com",
            "accept": "text/event-stream",
            "accept-language": "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
            "cache-control": "no-cache",
            "referer": "https://you.com/search?q=who+are+you&tbm=youchat",
            "sec-ch-ua": '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": '"Windows"',
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
            "cookie": f"safesearch_guest=Off; uuid_guest={str(uuid4())}",
            
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0",
        }
        
        # Add print statements to display headers and user agent
        print("Headers:", client.headers)
        print("User-Agent:", client.headers["user-agent"])
        
        params = {
            "q": prompt,
            "page": 1,
            "count": 10,
            "safeSearch": "Off",
            "onShoppingPage": False,
            "mkt": "",
            "responseFilter": "WebPages,Translations,TimeZone,Computation,RelatedSearches",
            "domain": "youchat",
            "queryTraceId": str(uuid4()),
            "chat": [],
        }
        resp = client.get(
            "https://you.com/api/streamingSearch", params=params, timeout_seconds=30
        )
        
        print("Response Status Code:", resp.status_code)
        print("Response Text:", resp.text)

        if "youChatToken" not in resp.text:
            raise Exception("Unable to fetch response.")
        return (
            "".join(findall(r"{\"youChatToken\": \"(.*?)\"}", resp.text))
            .replace("\\n", "\n")
            .replace("\\\\", "\\")
            .replace('\\"', '"')
        )