refactored you code added error handling and updated regex for parsing response
This commit is contained in:
parent
db9f3a5fa7
commit
f43107aa76
3 changed files with 98 additions and 82 deletions
|
@ -1,10 +1,7 @@
|
||||||
import you
|
import you
|
||||||
|
|
||||||
# simple request with links and details
|
# simple request with links and details
|
||||||
response = you.Completion.create(
|
response = you.Completion.create(prompt="hello world", detailed=True, include_links=True)
|
||||||
prompt = "hello world",
|
|
||||||
detailed = True,
|
|
||||||
includelinks = True)
|
|
||||||
|
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
@ -16,16 +13,14 @@ print(response)
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
|
|
||||||
#chatbot
|
# chatbot
|
||||||
|
|
||||||
chat = []
|
chat = []
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
prompt = input("You: ")
|
prompt = input("You: ")
|
||||||
|
|
||||||
response = you.Completion.create(
|
response = you.Completion.create(prompt=prompt, chat=chat)
|
||||||
prompt = prompt,
|
|
||||||
chat = chat)
|
|
||||||
|
|
||||||
print("Bot:", response["response"])
|
print("Bot:", response["response"])
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ import you
|
||||||
|
|
||||||
# simple request with links and details
|
# simple request with links and details
|
||||||
response = you.Completion.create(
|
response = you.Completion.create(
|
||||||
prompt = "hello world",
|
prompt="hello world",
|
||||||
detailed = True,
|
detailed=True,
|
||||||
includelinks = True,)
|
include_links=True, )
|
||||||
|
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ print(response)
|
||||||
# }
|
# }
|
||||||
# }
|
# }
|
||||||
|
|
||||||
#chatbot
|
# chatbot
|
||||||
|
|
||||||
chat = []
|
chat = []
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ while True:
|
||||||
prompt = input("You: ")
|
prompt = input("You: ")
|
||||||
|
|
||||||
response = you.Completion.create(
|
response = you.Completion.create(
|
||||||
prompt = prompt,
|
prompt=prompt,
|
||||||
chat = chat)
|
chat=chat)
|
||||||
|
|
||||||
print("Bot:", response["response"])
|
print("Bot:", response["response"])
|
||||||
|
|
||||||
|
|
135
you/__init__.py
135
you/__init__.py
|
@ -1,78 +1,99 @@
|
||||||
from tls_client import Session
|
import re
|
||||||
from re import findall
|
from json import loads
|
||||||
from json import loads, dumps
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from fake_useragent import UserAgent
|
||||||
|
from tls_client import Session
|
||||||
|
|
||||||
|
|
||||||
class Completion:
|
class Completion:
|
||||||
|
@staticmethod
|
||||||
def create(
|
def create(
|
||||||
prompt : str,
|
prompt: str,
|
||||||
page : int = 1,
|
page: int = 1,
|
||||||
count : int = 10,
|
count: int = 10,
|
||||||
safeSearch : str = "Moderate",
|
safe_search: str = 'Moderate',
|
||||||
onShoppingpage : bool = False,
|
on_shopping_page: bool = False,
|
||||||
mkt : str = "",
|
mkt: str = '',
|
||||||
responseFilter : str = "WebPages,Translations,TimeZone,Computation,RelatedSearches",
|
response_filter: str = 'WebPages,Translations,TimeZone,Computation,RelatedSearches',
|
||||||
domain : str = "youchat",
|
domain: str = 'youchat',
|
||||||
queryTraceId : str = None,
|
query_trace_id: str = None,
|
||||||
chat : list = [],
|
chat: list = None,
|
||||||
includelinks : bool = False,
|
include_links: bool = False,
|
||||||
detailed : bool = False,
|
detailed: bool = False,
|
||||||
debug : bool = False ) -> dict:
|
debug: bool = False,
|
||||||
|
) -> dict:
|
||||||
|
if chat is None:
|
||||||
|
chat = []
|
||||||
|
|
||||||
client = Session(client_identifier="chrome_108")
|
client = Session(client_identifier='chrome_108')
|
||||||
client.headers = {
|
client.headers = Completion.__get_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=Moderate; uuid_guest={str(uuid4())}',
|
|
||||||
"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",
|
|
||||||
}
|
|
||||||
|
|
||||||
response = client.get(f"https://you.com/api/streamingSearch", params = {
|
response = client.get(
|
||||||
"q" : prompt,
|
f'https://you.com/api/streamingSearch',
|
||||||
"page" : page,
|
params={
|
||||||
"count" : count,
|
'q': prompt,
|
||||||
"safeSearch" : safeSearch,
|
'page': page,
|
||||||
"onShoppingPage" : onShoppingpage,
|
'count': count,
|
||||||
"mkt" : mkt,
|
'safeSearch': safe_search,
|
||||||
"responseFilter" : responseFilter,
|
'onShoppingPage': on_shopping_page,
|
||||||
"domain" : domain,
|
'mkt': mkt,
|
||||||
"queryTraceId" : str(uuid4()) if queryTraceId is None else queryTraceId,
|
'responseFilter': response_filter,
|
||||||
"chat" : str(chat), # {"question":"","answer":" '"}
|
'domain': domain,
|
||||||
}
|
'queryTraceId': str(uuid4()) if query_trace_id is None else query_trace_id,
|
||||||
|
'chat': str(chat), # {'question':'','answer':' ''}
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
print('\n\n------------------\n\n')
|
print('\n\n------------------\n\n')
|
||||||
print(response.text)
|
print(response.text)
|
||||||
print('\n\n------------------\n\n')
|
print('\n\n------------------\n\n')
|
||||||
|
|
||||||
youChatSerpResults = findall(r'youChatSerpResults\ndata: (.*)\n\nevent', response.text)[0]
|
if 'youChatToken' not in response.text:
|
||||||
thirdPartySearchResults = findall(r"thirdPartySearchResults\ndata: (.*)\n\nevent", response.text)[0]
|
return Completion.__get_failure_response()
|
||||||
#slots = findall(r"slots\ndata: (.*)\n\nevent", response.text)[0]
|
|
||||||
|
|
||||||
text = response.text.split('}]}\n\nevent: youChatToken\ndata: {"youChatToken": "')[-1]
|
you_chat_serp_results = re.search(
|
||||||
text = text.replace('"}\n\nevent: youChatToken\ndata: {"youChatToken": "', '')
|
r'(?<=event: youChatSerpResults\ndata:)(.*\n)*?(?=event: )', response.text
|
||||||
text = text.replace('event: done\ndata: I\'m Mr. Meeseeks. Look at me.\n\n', '')
|
).group()
|
||||||
text = text[:-4] # trims '"}', along with the last two remaining newlines
|
third_party_search_results = re.search(
|
||||||
|
r'(?<=event: thirdPartySearchResults\ndata:)(.*\n)*?(?=event: )', response.text
|
||||||
|
).group()
|
||||||
|
# slots = findall(r"slots\ndata: (.*)\n\nevent", response.text)[0]
|
||||||
|
|
||||||
|
text = ''.join(re.findall(r'{\"youChatToken\": \"(.*?)\"}', response.text))
|
||||||
|
|
||||||
extra = {
|
extra = {
|
||||||
'youChatSerpResults' : loads(youChatSerpResults),
|
'youChatSerpResults': loads(you_chat_serp_results),
|
||||||
#'slots' : loads(slots)
|
# 'slots' : loads(slots)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'response': text,
|
'response': text.replace('\\n', '\n').replace('\\\\', '\\'),
|
||||||
'links' : loads(thirdPartySearchResults)['search']["third_party_search_results"] if includelinks else None,
|
'links': loads(third_party_search_results)['search']['third_party_search_results']
|
||||||
'extra' : extra if detailed else None,
|
if include_links
|
||||||
|
else None,
|
||||||
|
'extra': extra if detailed else None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __get_headers(cls) -> dict:
|
||||||
|
return {
|
||||||
|
'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=Moderate; uuid_guest={str(uuid4())}',
|
||||||
|
'user-agent': UserAgent().random,
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __get_failure_response(cls) -> dict:
|
||||||
|
return dict(response='Unable to fetch the response, Please try again.', links=[], extra={})
|
||||||
|
|
Loading…
Reference in a new issue