Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Always include "__Secure-1PSIDTS" to avoid the "SNlM0e value not foun…
Browse files Browse the repository at this point in the history
…d" error.
  • Loading branch information
SetoKaiba committed Dec 31, 2023
1 parent a38f26f commit b1aa195
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 83 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,16 @@ Simple Usage
from bardapi import Bard

token = 'xxxxxxx'
bard = Bard(token=token)
token_ts = 'xxxxxxx'
bard = Bard(token=token, token_ts=token_ts)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
```
Or you can use this
```python
from bardapi import Bard
import os
os.environ['_BARD_API_KEY']="xxxxxxx"
os.environ['_BARD_API_TS']="xxxxxxx"

Bard().get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
```
Expand All @@ -132,6 +134,9 @@ import os
# set your __Secure-1PSID value to key
token = 'xxxxxxx'

# set your __Secure-1PSIDTS value to key
token_ts = 'xxxxxxx'

# set your input text
input_text = "나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘"

Expand All @@ -146,6 +151,7 @@ Addressing errors caused by delayed responses in environments like Google Colab
from bardapi import Bard
import os
os.environ['_BARD_API_KEY']="xxxxxxx"
os.environ['_BARD_API_TS']="xxxxxxx"

bard = Bard(timeout=30) # Set timeout in seconds
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
Expand All @@ -167,7 +173,7 @@ proxies = {
'https': 'https://proxy.example.com:8080'
}

bard = Bard(token='xxxxxxx', proxies=proxies, timeout=30)
bard = Bard(token='xxxxxxx', token_ts='xxxxxxx', proxies=proxies, timeout=30)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
```

Expand All @@ -183,7 +189,7 @@ import requests
proxy_url = "http://xxxxxxxxxxxxxx:@smartproxy.crawlbase.com:8012"
proxies = {"http": proxy_url, "https": proxy_url}

bard = Bard(token='xxxxxxx', proxies=proxies, timeout=30)
bard = Bard(token='xxxxxxx', token_ts='xxxxxxx', proxies=proxies, timeout=30)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']
```

Expand All @@ -196,7 +202,9 @@ from bardapi import Bard
import requests
# import os
# os.environ['_BARD_API_KEY'] = 'xxxxxxx'
# os.environ['_BARD_API_TS'] = 'xxxxxxx'
token='xxxxxxx'
token_ts='xxxxxxx'

session = requests.Session()
session.headers = {
Expand All @@ -208,9 +216,11 @@ session.headers = {
"Referer": "https://bard.google.com/",
}
session.cookies.set("__Secure-1PSID", os.getenv("_BARD_API_KEY"))
session.cookies.set("__Secure-1PSIDTS", os.getenv("_BARD_API_TS"))
# session.cookies.set("__Secure-1PSID", token)
# session.cookies.set("__Secure-1PSIDTS", token_ts)

bard = Bard(token=token, session=session, timeout=30)
bard = Bard(token=token, token_ts=token_ts, session=session, timeout=30)
bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content']

# Continued conversation without set new session
Expand Down Expand Up @@ -281,7 +291,7 @@ As an experimental feature, it is possible to ask questions with an image. Howev
```python
from bardapi import Bard

bard = Bard(token='xxxxxxx')
bard = Bard(token='xxxxxxx', token_ts='xxxxxxx')
image = open('image.jpg', 'rb').read() # (jpeg, png, webp) are supported.
bard_answer = bard.ask_about_image('What is in the image?', image)
print(bard_answer['content'])
Expand All @@ -293,7 +303,7 @@ The user is solely responsible for all code, and it is imperative to consult Goo
```python
from bardapi import Bard

bard = Bard(token='xxxxxxx')
bard = Bard(token='xxxxxxx', token_ts='xxxxxxx')
audio = bard.speech('Hello, I am Bard! How can I help you today?')
with open("speech.ogg", "wb") as f:
f.write(bytes(audio['audio']))
Expand Down
14 changes: 11 additions & 3 deletions bardapi/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ChatBard(Bard):
def __init__(
self,
token: Optional[str] = None,
token_ts: Optional[str] = None,
timeout: int = 20,
proxies: Optional[dict] = None,
session: Optional[requests.Session] = None,
Expand All @@ -38,6 +39,7 @@ def __init__(
Args:
token (str, optional): Bard API token.
token_ts (str, optional): Bard API token.
timeout (int, optional, default = 20): Request timeout in seconds.
proxies (dict, optional): Proxy configuration for requests.
session (requests.Session, optional): Requests session object.
Expand All @@ -46,10 +48,10 @@ def __init__(
token_from_browser (bool, optional, default = False): Gets a token from the browser
"""

self.session = session or self._init_session(token)
self.session = session or self._init_session(token, token_ts)
self.language = language or os.getenv("_BARD_API_LANG") or "english"
self.timeout = int(timeout or os.getenv("_BARD_API_TIMEOUT") or 30)
self.token = token or os.getenv("_BARD_API_KEY") or self._get_api_key()
self.token, self.token_ts = (token, token_ts) or (os.getenv("_BARD_API_KEY"), os.getenv("_BARD_API_TS")) or self._get_api_key()
self.token_from_browser = token_from_browser
self.proxies = proxies
self.google_translator_api_key = google_translator_api_key
Expand All @@ -64,19 +66,25 @@ def _init_session(token):
session = requests.Session()
session.headers = SESSION_HEADERS
session.cookies.set("__Secure-1PSID", token)
session.cookies.set("__Secure-1PSIDTS", token_ts)
return session

@staticmethod
def _get_api_key():
key = input("Enter the Bard API Key(__Secure-1PSID): ")
key_ts = input("Enter the Bard API Key(__Secure-1PSIDTS): ")
if not key:
print("Bard API(__Secure-1PSID) Key must be entered.")
exit(1)
return key
if not key_ts:
print("Bard API(__Secure-1PSIDTS) Key must be entered.")
exit(1)
return key, key_ts

def _init_bard(self):
return Bard(
token=self.token,
token_ts=self.token_ts,
session=self.session,
google_translator_api_key=self.google_translator_api_key,
timeout=self.timeout,
Expand Down
36 changes: 22 additions & 14 deletions bardapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Bard:
def __init__(
self,
token: Optional[str] = None,
token_ts: Optional[str] = None,
timeout: int = 20,
proxies: Optional[dict] = None,
session: Optional[requests.Session] = None,
Expand All @@ -69,7 +70,7 @@ def __init__(
run_code (bool, optional, default = False): Whether to directly execute the code included in the answer (Python only)
token_from_browser (bool, optional, default = False): Gets a token from the browser
"""
self.token = self._get_token(token, token_from_browser)
self.token, self.token_ts = self._get_token(token, token_ts, token_from_browser)
self.proxies = proxies
self.timeout = timeout
self._reqid = int("".join(random.choices(string.digits, k=4)))
Expand All @@ -89,28 +90,29 @@ def __init__(
if google_translator_api_key:
assert translate

def _get_token(self, token: str, token_from_browser: bool) -> str:
def _get_token(self, token: str, token_ts: str, token_from_browser: bool) -> str:
"""
Get the Bard API token either from the provided token or from the browser cookie.
Args:
token (str): Bard API token.
token_ts (str): Bard API token ts
token_from_browser (bool): Whether to extract the token from the browser cookie.
Returns:
str: The Bard API token.
Raises:
Exception: If the token is not provided and can't be extracted from the browser.
"""
if token:
return token
elif os.getenv("_BARD_API_KEY"):
return os.getenv("_BARD_API_KEY")
if token and token_ts:
return token, token_ts
elif os.getenv("_BARD_API_KEY") and os.getenv("_BARD_API_TS"):
return os.getenv("_BARD_API_KEY"), os.getenv("_BARD_API_TS")
elif token_from_browser:
extracted_cookie_dict = extract_bard_cookie(cookies=False)
extracted_cookie_dict = extract_bard_cookie()
if not extracted_cookie_dict:
raise Exception("Failed to extract cookie from browsers.")
return extracted_cookie_dict["__Secure-1PSID"]
return extracted_cookie_dict["__Secure-1PSID"], extracted_cookie_dict["__Secure-1PSIDTS"]
else:
raise Exception(
"Bard API Key must be provided as token argument or extracted from browser."
Expand All @@ -130,6 +132,7 @@ def _get_session(self, session: Optional[requests.Session]) -> requests.Session:
new_session = requests.Session()
new_session.headers = SESSION_HEADERS
new_session.cookies.set("__Secure-1PSID", self.token)
new_session.cookies.set("__Secure-1PSIDTS", self.token_ts)
new_session.proxies = self.proxies
return new_session
else:
Expand Down Expand Up @@ -158,7 +161,7 @@ def _get_snim0e(self) -> str:
snim0e = re.search(r"SNlM0e\":\"(.*?)\"", resp.text)
if not snim0e:
raise Exception(
"SNlM0e value not found. Double-check __Secure-1PSID value or pass it as token='xxxxx'."
"SNlM0e value not found. Double-check __Secure-1PSID and __Secure-1PSIDTS value or pass it as token='xxxxx' and token_ts='xxxxx'."
)
return snim0e.group(1)

Expand Down Expand Up @@ -351,7 +354,8 @@ def get_answer(
Example:
>>> token = 'xxxxxx'
>>> bard = Bard(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = Bard(token=token, token_ts=token_ts)
>>> response = bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")
>>> print(response['content'])
Expand Down Expand Up @@ -523,7 +527,8 @@ def speech(self, input_text: str, lang: str = "en-US") -> dict:
Example:
>>> token = 'xxxxxx'
>>> bard = Bard(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = Bard(token=token, token_ts=token_ts)
>>> audio = bard.speech("hello!")
>>> with open("bard.ogg", "wb") as f:
>>> f.write(bytes(audio['audio']))
Expand Down Expand Up @@ -582,7 +587,8 @@ def export_conversation(self, bard_answer, title: str = "") -> dict:
Example:
>>> token = 'xxxxxx'
>>> bard = Bard(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = Bard(token=token, token_ts=token_ts)
>>> bard_answer = bard.get_answer("hello!")
>>> url = bard.export_conversation(bard_answer, title="Export Conversation")
>>> print(url['url'])
Expand Down Expand Up @@ -641,7 +647,8 @@ def ask_about_image(
Example:
>>> token = 'xxxxxx'
>>> bard = Bard(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = Bard(token=token, token_ts=token_ts)
>>> image = open('image.jpg', 'rb').read()
>>> bard_answer = bard.ask_about_image("what is in the image?", image)['content']
Expand Down Expand Up @@ -824,7 +831,8 @@ def export_replit(
Example:
>>> token = 'xxxxxx'
>>> bard = Bard(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = Bard(token=token, token_ts=token_ts)
>>> bard_answer = bard.get_answer("Give me python code to print hello world")
>>> url = bard.export_replit(bard_answer['code'], bard_answer['program_lang'])
>>> print(url['url'])
Expand Down
38 changes: 22 additions & 16 deletions bardapi/core_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class BardAsync:
def __init__(
self,
token: Optional[str] = None,
token_ts: Optional[str] = None,
timeout: int = 20,
proxies: Optional[dict] = None,
client: Optional[AsyncClient] = None,
Expand All @@ -65,7 +66,7 @@ def __init__(
token_from_browser (bool, optional, default = False): Gets a token from the browser
"""

self.token = self._get_token(token, token_from_browser)
self.token, self.token_ts = self._get_token(token, token_ts, token_from_browser)
self.proxies = proxies
self.timeout = timeout
self._reqid = int("".join(random.choices(string.digits, k=4)))
Expand All @@ -74,7 +75,7 @@ def __init__(
self.choice_id = ""
self.client = self._get_client(client) # Creating an httpx async client for asynchronous core code
self.language = language
self.cookie_dict = {"__Secure-1PSID": self.token}
self.cookie_dict = {"__Secure-1PSID": self.token, "__Secure-1PSIDTS": self.token_ts}
self.run_code = run_code or False
self.google_translator_api_key = google_translator_api_key
self.SNlM0e = self._get_snim0e()
Expand All @@ -95,7 +96,7 @@ def _get_snim0e(self):
:return: The SNlM0e value as a string.
"""
if isinstance(self.SNlM0e, str):
if hasattr(self, 'SNlM0e') and isinstance(self.SNlM0e, str):
return self.SNlM0e

if not self.token or self.token[-1] != ".":
Expand All @@ -119,7 +120,7 @@ def _get_snim0e(self):
self.SNlM0e = snim0e_match.group(1)
return self.SNlM0e

def _get_token(self, token: str, token_from_browser: bool) -> str:
def _get_token(self, token: str, token_ts: str, token_from_browser: bool) -> str:
"""
Get the Bard API token either from the provided token or from the browser cookie.
Expand All @@ -131,15 +132,15 @@ def _get_token(self, token: str, token_from_browser: bool) -> str:
Raises:
Exception: If the token is not provided and can't be extracted from the browser.
"""
if token:
return token
elif os.getenv("_BARD_API_KEY"):
return os.getenv("_BARD_API_KEY")
if token and token_ts:
return token, token_ts
elif os.getenv("_BARD_API_KEY") and os.getenv("_BARD_API_TS"):
return os.getenv("_BARD_API_KEY"), os.getenv("_BARD_API_TS")
elif token_from_browser:
extracted_cookie_dict = extract_bard_cookie(cookies=False)
extracted_cookie_dict = extract_bard_cookie()
if not extracted_cookie_dict:
raise Exception("Failed to extract cookie from browsers.")
return extracted_cookie_dict["__Secure-1PSID"]
return extracted_cookie_dict["__Secure-1PSID"], extracted_cookie_dict["__Secure-1PSIDTS"]
else:
raise Exception(
"Bard API Key must be provided as token argument or extracted from browser."
Expand All @@ -159,7 +160,7 @@ async def _get_client(self, session: Optional[AsyncClient]) -> AsyncClient:
async_client = AsyncClient(
http2=True,
headers=SESSION_HEADERS,
cookies={"__Secure-1PSID": self.token},
cookies={"__Secure-1PSID": self.token, "__Secure-1PSIDTS": self.token_ts},
timeout=self.timeout,
proxies=self.proxies,
)
Expand All @@ -178,7 +179,8 @@ async def get_answer(self, input_text: str) -> dict:
>>>
>>> async def main():
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = BardAsync(token=token, token_ts=token_ts)
>>> response = await bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")
>>> print(response['content'])
>>>
Expand Down Expand Up @@ -358,7 +360,8 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict:
>>>
>>> async def main():
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = BardAsync(token=token, token_ts=token_ts)
>>> audio = await bard.speech("Hello")
>>> with open("bard.ogg", "wb") as f:
>>> f.write(bytes(audio['audio']))
Expand Down Expand Up @@ -421,7 +424,8 @@ async def export_conversation(self, bard_answer, title: str = "") -> dict:
>>>
>>> async def main():
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = BardAsync(token=token, token_ts=token_ts)
>>> bard_answer = await bard.get_answer("hello!")
>>> url = await bard.export_conversation(bard_answer, title="Export Conversation")
>>> print(url['url'])
Expand Down Expand Up @@ -509,7 +513,8 @@ async def export_replit(
>>>
>>> async def main():
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = BardAsync(token=token, token_ts=token_ts)
>>> bard_answer = await bard.get_answer("Give me python code to print hello world")
>>> url = await bard.export_replit(bard_answer['code'], bard_answer['program_lang'])
>>> print(url['url'])
Expand Down Expand Up @@ -609,7 +614,8 @@ async def ask_about_image(
>>>
>>> async def main():
>>> token = 'xxxxxx'
>>> bard = BardAsync(token=token)
>>> token_ts = 'xxxxxx'
>>> bard = BardAsync(token=token, token_ts=token_ts)
>>> image = open('image.jpg', 'rb').read()
>>> bard_answer = await bard.ask_about_image("what is in the image?", image)
>>> print(bard_answer['content'])
Expand Down
Loading

0 comments on commit b1aa195

Please sign in to comment.