Merge pull request #165 from sudouser777/feature/code_refactor
Browse files- testing/you_test.py +7 -12
- you/README.md +9 -9
- you/__init__.py +82 -61
testing/you_test.py
CHANGED
@@ -1,10 +1,7 @@
|
|
1 |
import you
|
2 |
|
3 |
# simple request with links and details
|
4 |
-
response = you.Completion.create(
|
5 |
-
prompt = "hello world",
|
6 |
-
detailed = True,
|
7 |
-
includelinks = True)
|
8 |
|
9 |
print(response)
|
10 |
|
@@ -16,17 +13,15 @@ print(response)
|
|
16 |
# }
|
17 |
# }
|
18 |
|
19 |
-
#chatbot
|
20 |
|
21 |
chat = []
|
22 |
|
23 |
while True:
|
24 |
prompt = input("You: ")
|
25 |
-
|
26 |
-
response = you.Completion.create(
|
27 |
-
|
28 |
-
chat = chat)
|
29 |
-
|
30 |
print("Bot:", response["response"])
|
31 |
-
|
32 |
-
chat.append({"question": prompt, "answer": response["response"]})
|
|
|
1 |
import you
|
2 |
|
3 |
# simple request with links and details
|
4 |
+
response = you.Completion.create(prompt="hello world", detailed=True, include_links=True)
|
|
|
|
|
|
|
5 |
|
6 |
print(response)
|
7 |
|
|
|
13 |
# }
|
14 |
# }
|
15 |
|
16 |
+
# chatbot
|
17 |
|
18 |
chat = []
|
19 |
|
20 |
while True:
|
21 |
prompt = input("You: ")
|
22 |
+
|
23 |
+
response = you.Completion.create(prompt=prompt, chat=chat)
|
24 |
+
|
|
|
|
|
25 |
print("Bot:", response["response"])
|
26 |
+
|
27 |
+
chat.append({"question": prompt, "answer": response["response"]})
|
you/README.md
CHANGED
@@ -5,9 +5,9 @@ import you
|
|
5 |
|
6 |
# simple request with links and details
|
7 |
response = you.Completion.create(
|
8 |
-
prompt
|
9 |
-
detailed
|
10 |
-
|
11 |
|
12 |
print(response)
|
13 |
|
@@ -19,18 +19,18 @@ print(response)
|
|
19 |
# }
|
20 |
# }
|
21 |
|
22 |
-
#chatbot
|
23 |
|
24 |
chat = []
|
25 |
|
26 |
while True:
|
27 |
prompt = input("You: ")
|
28 |
-
|
29 |
response = you.Completion.create(
|
30 |
-
prompt
|
31 |
-
chat
|
32 |
-
|
33 |
print("Bot:", response["response"])
|
34 |
-
|
35 |
chat.append({"question": prompt, "answer": response["response"]})
|
36 |
```
|
|
|
5 |
|
6 |
# simple request with links and details
|
7 |
response = you.Completion.create(
|
8 |
+
prompt="hello world",
|
9 |
+
detailed=True,
|
10 |
+
include_links=True, )
|
11 |
|
12 |
print(response)
|
13 |
|
|
|
19 |
# }
|
20 |
# }
|
21 |
|
22 |
+
# chatbot
|
23 |
|
24 |
chat = []
|
25 |
|
26 |
while True:
|
27 |
prompt = input("You: ")
|
28 |
+
|
29 |
response = you.Completion.create(
|
30 |
+
prompt=prompt,
|
31 |
+
chat=chat)
|
32 |
+
|
33 |
print("Bot:", response["response"])
|
34 |
+
|
35 |
chat.append({"question": prompt, "answer": response["response"]})
|
36 |
```
|
you/__init__.py
CHANGED
@@ -1,78 +1,99 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
4 |
-
|
|
|
|
|
5 |
|
6 |
|
7 |
class Completion:
|
|
|
8 |
def create(
|
9 |
-
prompt
|
10 |
-
page
|
11 |
-
count
|
12 |
-
|
13 |
-
|
14 |
-
mkt
|
15 |
-
|
16 |
-
domain
|
17 |
-
|
18 |
-
chat
|
19 |
-
|
20 |
-
detailed
|
21 |
-
debug
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"cache-control" : "no-cache",
|
29 |
-
"referer" : "https://you.com/search?q=who+are+you&tbm=youchat",
|
30 |
-
"sec-ch-ua" : '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
|
31 |
-
"sec-ch-ua-mobile" : "?0",
|
32 |
-
"sec-ch-ua-platform": '"Windows"',
|
33 |
-
"sec-fetch-dest" : "empty",
|
34 |
-
"sec-fetch-mode" : "cors",
|
35 |
-
"sec-fetch-site" : "same-origin",
|
36 |
-
'cookie' : f'safesearch_guest=Moderate; uuid_guest={str(uuid4())}',
|
37 |
-
"user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
|
38 |
-
}
|
39 |
|
40 |
-
response = client.get(
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
)
|
53 |
-
|
54 |
-
|
55 |
if debug:
|
56 |
print('\n\n------------------\n\n')
|
57 |
print(response.text)
|
58 |
print('\n\n------------------\n\n')
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
68 |
|
69 |
extra = {
|
70 |
-
'youChatSerpResults'
|
71 |
-
#'slots' : loads(slots)
|
72 |
}
|
73 |
|
74 |
return {
|
75 |
-
'response': text,
|
76 |
-
'links'
|
77 |
-
|
|
|
|
|
78 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from json import loads
|
3 |
+
from uuid import uuid4
|
4 |
+
|
5 |
+
from fake_useragent import UserAgent
|
6 |
+
from tls_client import Session
|
7 |
|
8 |
|
9 |
class Completion:
|
10 |
+
@staticmethod
|
11 |
def create(
|
12 |
+
prompt: str,
|
13 |
+
page: int = 1,
|
14 |
+
count: int = 10,
|
15 |
+
safe_search: str = 'Moderate',
|
16 |
+
on_shopping_page: bool = False,
|
17 |
+
mkt: str = '',
|
18 |
+
response_filter: str = 'WebPages,Translations,TimeZone,Computation,RelatedSearches',
|
19 |
+
domain: str = 'youchat',
|
20 |
+
query_trace_id: str = None,
|
21 |
+
chat: list = None,
|
22 |
+
include_links: bool = False,
|
23 |
+
detailed: bool = False,
|
24 |
+
debug: bool = False,
|
25 |
+
) -> dict:
|
26 |
+
if chat is None:
|
27 |
+
chat = []
|
28 |
+
|
29 |
+
client = Session(client_identifier='chrome_108')
|
30 |
+
client.headers = Completion.__get_headers()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
response = client.get(
|
33 |
+
f'https://you.com/api/streamingSearch',
|
34 |
+
params={
|
35 |
+
'q': prompt,
|
36 |
+
'page': page,
|
37 |
+
'count': count,
|
38 |
+
'safeSearch': safe_search,
|
39 |
+
'onShoppingPage': on_shopping_page,
|
40 |
+
'mkt': mkt,
|
41 |
+
'responseFilter': response_filter,
|
42 |
+
'domain': domain,
|
43 |
+
'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
|
44 |
+
'chat': str(chat), # {'question':'','answer':' ''}
|
45 |
+
},
|
46 |
)
|
47 |
+
|
|
|
48 |
if debug:
|
49 |
print('\n\n------------------\n\n')
|
50 |
print(response.text)
|
51 |
print('\n\n------------------\n\n')
|
52 |
|
53 |
+
if 'youChatToken' not in response.text:
|
54 |
+
return Completion.__get_failure_response()
|
55 |
+
|
56 |
+
you_chat_serp_results = re.search(
|
57 |
+
r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
|
58 |
+
).group()
|
59 |
+
third_party_search_results = re.search(
|
60 |
+
r'(?<=event: thirdPartySearchResults\ndata:)(.*\n)*?(?=event: )', response.text
|
61 |
+
).group()
|
62 |
+
# slots = findall(r"slots\ndata: (.*)\n\nevent", response.text)[0]
|
63 |
+
|
64 |
+
text = ''.join(re.findall(r'{\"youChatToken\": \"(.*?)\"}', response.text))
|
65 |
|
66 |
extra = {
|
67 |
+
'youChatSerpResults': loads(you_chat_serp_results),
|
68 |
+
# 'slots' : loads(slots)
|
69 |
}
|
70 |
|
71 |
return {
|
72 |
+
'response': text.replace('\\n', '\n').replace('\\\\', '\\'),
|
73 |
+
'links': loads(third_party_search_results)['search']['third_party_search_results']
|
74 |
+
if include_links
|
75 |
+
else None,
|
76 |
+
'extra': extra if detailed else None,
|
77 |
}
|
78 |
+
|
79 |
+
@classmethod
|
80 |
+
def __get_headers(cls) -> dict:
|
81 |
+
return {
|
82 |
+
'authority': 'you.com',
|
83 |
+
'accept': 'text/event-stream',
|
84 |
+
'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',
|
85 |
+
'cache-control': 'no-cache',
|
86 |
+
'referer': 'https://you.com/search?q=who+are+you&tbm=youchat',
|
87 |
+
'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
|
88 |
+
'sec-ch-ua-mobile': '?0',
|
89 |
+
'sec-ch-ua-platform': '"Windows"',
|
90 |
+
'sec-fetch-dest': 'empty',
|
91 |
+
'sec-fetch-mode': 'cors',
|
92 |
+
'sec-fetch-site': 'same-origin',
|
93 |
+
'cookie': f'safesearch_guest=Moderate; uuid_guest={str(uuid4())}',
|
94 |
+
'user-agent': UserAgent().random,
|
95 |
+
}
|
96 |
+
|
97 |
+
@classmethod
|
98 |
+
def __get_failure_response(cls) -> dict:
|
99 |
+
return dict(response='Unable to fetch the response, Please try again.', links=[], extra={})
|