diff --git a/README.md b/README.md index bf6bce8db..2976f3298 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,8 @@ 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 @@ -120,6 +121,7 @@ Or you can use this from bardapi import Bard import os os.environ['_BARD_API_KEY']="xxxxxxx" +os.environ['_BARD_API_TS']="xxxxxxx" Bard().get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content'] ``` @@ -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 = "나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘" @@ -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'] @@ -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'] ``` @@ -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'] ``` @@ -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 = { @@ -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 @@ -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']) @@ -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'])) diff --git a/bardapi/chat.py b/bardapi/chat.py index 314864ffb..494984afe 100644 --- a/bardapi/chat.py +++ b/bardapi/chat.py @@ -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, @@ -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. @@ -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 @@ -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, diff --git a/bardapi/core.py b/bardapi/core.py index b00f87ca5..3879feaea 100644 --- a/bardapi/core.py +++ b/bardapi/core.py @@ -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, @@ -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))) @@ -89,12 +90,13 @@ 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: @@ -102,15 +104,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." @@ -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: @@ -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) @@ -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']) @@ -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'])) @@ -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']) @@ -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'] @@ -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']) diff --git a/bardapi/core_async.py b/bardapi/core_async.py index 7e17e721b..9102a6c0a 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -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, @@ -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))) @@ -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() @@ -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] != ".": @@ -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. @@ -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." @@ -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, ) @@ -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']) >>> @@ -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'])) @@ -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']) @@ -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']) @@ -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']) diff --git a/bardapi/core_cookies.py b/bardapi/core_cookies.py index eff5e5a7d..5655a0be3 100644 --- a/bardapi/core_cookies.py +++ b/bardapi/core_cookies.py @@ -68,7 +68,7 @@ def _get_token(self, token_from_browser: bool) -> dict: Exception: If the token is not provided and can't be extracted from the browser. """ if token_from_browser: - extracted_cookie_dict = extract_bard_cookie(cookies=True) + extracted_cookie_dict = extract_bard_cookie() if not extracted_cookie_dict: raise Exception("Failed to extract cookie from browsers.") return extracted_cookie_dict @@ -332,7 +332,7 @@ def _get_token(self, token_from_browser: bool) -> dict: Exception: If the token is not provided and can't be extracted from the browser. """ if token_from_browser: - extracted_cookie_dict = extract_bard_cookie(cookies=True) + extracted_cookie_dict = extract_bard_cookie() if not extracted_cookie_dict: raise Exception("Failed to extract cookie from browsers.") return extracted_cookie_dict diff --git a/bardapi/utils.py b/bardapi/utils.py index de32d92b1..13d2fe8f5 100644 --- a/bardapi/utils.py +++ b/bardapi/utils.py @@ -63,7 +63,7 @@ def upload_image(image: bytes, filename: str = "Photo.jpg"): return resp.text -def extract_bard_cookie(cookies: bool = False) -> dict: +def extract_bard_cookie() -> dict: """ Extracts the specified Bard cookie(s) from the browser's cookies. @@ -106,14 +106,9 @@ def extract_bard_cookie(cookies: bool = False) -> dict: print(cookie.name) if cookie.name == "__Secure-1PSID" and cookie.value.endswith("."): cookie_dict["__Secure-1PSID"] = cookie.value - if cookies: - if cookie.name == "__Secure-1PSIDTS": - print(cookie.value) - cookie_dict["__Secure-1PSIDTS"] = cookie.value - elif cookie.name == "__Secure-1PSIDCC": - print(cookie.value) - cookie_dict["__Secure-1PSIDCC"] = cookie.value - if len(cookie_dict) == 3: + if cookie.name == "__Secure-1PSIDTS": + cookie_dict["__Secure-1PSIDTS"] = cookie.value + if len(cookie_dict) == 2: return cookie_dict except Exception as e: # Ignore exceptions and try the next browser function diff --git a/documents/README_DEV.md b/documents/README_DEV.md index 41ecd48a5..34befe78f 100644 --- a/documents/README_DEV.md +++ b/documents/README_DEV.md @@ -55,6 +55,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, @@ -112,7 +113,7 @@ class Bard: Get the requests Session object. """ - 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. """ @@ -138,7 +139,7 @@ print(bard_answer) ```python from bardapi import Bard -bard = Bard(token= xxxxxxxxx ) +bard = Bard(token= xxxxxxxxx, token_ts= xxxxxxxxx ) audio = bard.speech("Hello, I am bard! How can I assist you?", lang='en-US') # Get bytes audio object. # Save audio object. @@ -157,7 +158,7 @@ For commercial use cases, please refrain from using the unofficial Google Transl ```python from bardapi import Bard -bard = Bard(token=token, language='chinese (simplified)') +bard = Bard(token=token, token_ts=token_ts, language='chinese (simplified)') res = bard.get_answer("今天首尔的天气怎么样?") print(res['content']) ``` @@ -167,6 +168,7 @@ from bardapi import Bard import os os.environ["_BARD_API_LANG"] = 'chinese (simplified)' os.environ["_BARD_API_KEY"] = 'xxxxxxxxx' +os.environ["_BARD_API_TS"] = 'xxxxxxxxx' res = Bard().get_answer("今天首尔的天气怎么样?") print(res['content']) @@ -181,7 +183,7 @@ As an experimental feature, it is possible to ask questions with an image. Howev ```python from bardapi import Bard -bard = Bard(token='xxxxxxxx') +bard = Bard(token='xxxxxxxx', token_ts='xxxxxxxx') 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']) @@ -192,7 +194,7 @@ print(bard_answer['content']) ### Get image links ```python from bardapi import Bard -bard = Bard(token='xxxxxxxx') +bard = Bard(token='xxxxxxxx', token_ts='xxxxxxxx') res = bard.get_answer("Find me an image of the main entrance of Stanford University.") res['links'] # Get image links (list) res['images'] # Get images (list) @@ -206,7 +208,7 @@ Some errors in ChatBard have been fixed. However, it is recommended not to pass ```python from bardapi import ChatBard -chat = ChatBard(token="xxxxxxxx", language='en') +chat = ChatBard(token='xxxxxxxx', token_ts='xxxxxxxx', language='en') chat.start() ``` or @@ -214,6 +216,7 @@ or from bardapi import ChatBard import os os.environ["_BARD_API_KEY"]='xxxxxxxx' # Requird +os.environ["_BARD_API_TS"]='xxxxxxxx' # Requird os.environ["_BARD_API_LANG"]="Arabic" # Optional, Default to English os.environ["_BARD_API_TIMEOUT"]=30 # Optional, Session Timeout @@ -227,6 +230,7 @@ import os import requests token='xxxxxxxx' +token_ts='xxxxxxxx' session = requests.Session() session.headers = SESSION_HEADERS session.cookies.set("__Secure-1PSID", token) @@ -235,7 +239,7 @@ proxies = { 'https': 'https://proxy.example.com:8080' } -ChatBard(token=token, session=session, proxies=proxies, timeout=40, language="chinese (simplified)").start() +ChatBard(token=token, token_ts=token_ts, session=session, proxies=proxies, timeout=40, language="chinese (simplified)").start() ```
@@ -246,7 +250,7 @@ Bard UI offers a convenient way to share a specific answer from Bard by generati ```python from bardapi import Bard -bard = Bard(token='xxxxxxxx') +bard = Bard(token='xxxxxxxx', token_ts='xxxxxxxx') bard_answer = bard.get_answer('How are you?') url = bard.export_conversation(bard_answer, title='Example Shared conversation') print(url['url']) @@ -259,7 +263,7 @@ print(url['url']) ```python from bardapi import Bard -bard = Bard(token='xxxxxxxx') +bard = Bard(token='xxxxxxxx', token_ts='xxxxxxxx') bard_answer = bard.get_answer("code python to print hello world") # {'code': 'print("Hello World")', 'program_lang': 'python'} @@ -276,7 +280,7 @@ print(url['url']) # https://replit.com/external/v1/claims/xxx/claim ```python from bardapi import Bard -bard = Bard(token="xxxxxxxx", run_code=True) +bard = Bard(token="xxxxxxxx", token_ts='xxxxxxxx', run_code=True) bard.get_answer("code a pie chart in python for this data={'blue':25, 'red':30, 'green':30, 'purple':15}") ``` ![](assets/bardapi_run_code.png) @@ -291,7 +295,7 @@ BardAsync is present in translate_to.core_async.BardAsync ```python from bardapi import BardAsync -bard = BardAsync(token="xxxxxxxx") +bard = BardAsync(token="xxxxxxxx", token_ts='xxxxxxxx') await bard.get_answer("What is Metaverse?") ``` or @@ -299,7 +303,7 @@ or import asyncio from bardapi import BardAsync -bard = BardAsync(token="xxxxxxxx") +bard = BardAsync(token="xxxxxxxx", token_ts='xxxxxxxx') asyncio.run(bard.get_answer("What is Metaverse?")) ``` @@ -312,8 +316,7 @@ from bardapi import BardCookies cookie_dict = { "__Secure-1PSID": "xxxxxxxx", - "__Secure-1PSIDTS": "xxxxxxxx", - "__Secure-1PSIDCC", "xxxxxxxx", + "__Secure-1PSIDTS": "xxxxxxxx" # Any cookie values you want to pass session object. } @@ -328,7 +331,6 @@ from bardapi import Bard, SESSION_HEADERS session = requests.Session() session.cookies.set("__Secure-1PSID", "bard __Secure-1PSID token") -session.cookies.set( "__Secure-1PSIDCC", "bard __Secure-1PSIDCC token") session.cookies.set("__Secure-1PSIDTS", "bard __Secure-1PSIDTS token") session.headers = SESSION_HEADERS @@ -360,14 +362,16 @@ import requests # Set token token= 'xxxxxxxx' +token_ts= 'xxxxxxxx' # Set session session = requests.Session() session.headers = SESSION_HEADERS session.cookies.set("__Secure-1PSID", token) +session.cookies.set("__Secure-1PSIDTS", token_ts) # Give session and conversation id. (check manually) -bard = Bard(token=token, session=session, conversation_id="c_1f04f704a788e6e4", timeout=30) +bard = Bard(token=token, token_ts=token_ts, session=session, conversation_id="c_1f04f704a788e6e4", timeout=30) bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content'] ``` @@ -390,7 +394,8 @@ Bard does not support temperature or hyperparameter adjustments, but it is possi from bardapi import Bard, max_token, max_sentence token = 'xxxxxxx' -bard = Bard(token=token) +token_ts = 'xxxxxxx' +bard = Bard(token=token, token_ts=token_ts) # max_token==30 max_token(bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘")['content'], 30) diff --git a/documents/README_FAQ.md b/documents/README_FAQ.md index e627b89c4..0b62d2204 100644 --- a/documents/README_FAQ.md +++ b/documents/README_FAQ.md @@ -110,7 +110,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'] ``` diff --git a/func_test.ipynb b/func_test.ipynb index 4db44212d..2c7614a26 100644 --- a/func_test.ipynb +++ b/func_test.ipynb @@ -15,7 +15,8 @@ "metadata": {}, "outputs": [], "source": [ - "token = 'xxxxxxx'" + "token = 'xxxxxxx'\n", + "token_ts = 'xxxxxxx'" ] }, { @@ -24,7 +25,7 @@ "metadata": {}, "outputs": [], "source": [ - "bard = Bard(token=token)\n", + "bard = Bard(token=token, token_ts=token_ts)\n", "bard.get_answer(\"나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘\")['content']" ] }, @@ -36,7 +37,7 @@ "source": [ "from bardapi import Bard\n", "\n", - "bard = Bard(token=token, language='chinese (simplified)')\n", + "bard = Bard(token=token, token_ts=token_ts, language='chinese (simplified)')\n", "res = bard.get_answer(\"今天首尔的天气怎么样?\")\n", "print(res['content'])" ] @@ -48,7 +49,7 @@ "outputs": [], "source": [ "from bardapi import Bard\n", - "bard = Bard(token=token)\n", + "bard = Bard(token=token, token_ts=token_ts)\n", "res = bard.get_answer(\"Find me an image of the main entrance of Stanford University.\")\n", "res['links'] # Get image links (list)\n", "res['images'] # Get images (list)" @@ -61,7 +62,7 @@ "outputs": [], "source": [ "from bardapi import Bard\n", - "bard = Bard(token=token)\n", + "bard = Bard(token=token, token_ts=token_ts)\n", "res = bard.get_answer(\"Find me an image of the main entrance of Stanford University.\")\n", "res['links'] # Get image links (list)\n", "res['images'] # Get images (list)" @@ -79,12 +80,12 @@ "XQhVqoq8lHTI8oZ09DAdXKBTLGrMiT9xv61UNWs51CE6UmY16Qbs-jPWnMm7ciAXtJPopA.\n", "\n", "====================================\n", - "\u001b[44m Welcome to Chatbot \u001b[49m\n", + "\u001B[44m Welcome to Chatbot \u001B[49m\n", "====================================\n", "If you enter quit, q, or stop, the chat will end.\n", "USER: q\n", "====================================\n", - "\u001b[31mChat Ended.\u001b[39m\n", + "\u001B[31mChat Ended.\u001B[39m\n", "\n", "DanielPark's Chat Template\n", "====================================\n" @@ -94,7 +95,7 @@ "source": [ "from bardapi import ChatBard\n", " \n", - "chat = ChatBard(token='xxxxxxx')\n", + "chat = ChatBard(token='xxxxxxx', token_ts='xxxxxxx')\n", "chat.start()" ] }, @@ -106,7 +107,7 @@ "source": [ "from bardapi import Bard\n", " \n", - "bard = Bard(token=token, run_code=True)\n", + "bard = Bard(token=token, token_ts=token_ts, run_code=True)\n", "bard.get_answer(\"code a pie chart in python for this data={'blue':25, 'red':30, 'green':30, 'purple':15}\")" ] }, @@ -118,7 +119,7 @@ "source": [ "from bardapi import BardAsync \n", " \n", - "bard = BardAsync(token=token)\n", + "bard = BardAsync(token=token, token_ts=token_ts)\n", "res = await bard.get_answer(\"What is Metaverse?\")\n", "print(res['content'])" ] @@ -135,14 +136,16 @@ "\n", "# Set token\n", "token= token\n", + "token_ts= token_ts\n", "\n", "# Set session\n", "session = requests.Session()\n", "session.headers = SESSION_HEADERS\n", "session.cookies.set(\"__Secure-1PSID\", token) \n", + "session.cookies.set(\"__Secure-1PSIDTS\", token_ts) \n", "\n", "# Give session and conversation id\n", - "bard = Bard(token=token, session=session, conversation_id=\"c_1f04f704a788e6e4\", timeout=30)\n", + "bard = Bard(token=token, token_ts=token_ts, session=session, conversation_id=\"c_1f04f704a788e6e4\", timeout=30)\n", "bard.get_answer(\"나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘\")['content']" ] }, @@ -176,10 +179,9 @@ "session = requests.Session()\n", "token = '-'\n", "session.cookies.set(\"__Secure-1PSID\", token)\n", - "session.cookies.set( \"__Secure-1PSIDCC\", \"-\")\n", "session.cookies.set(\"__Secure-1PSIDTS\", \"-\")\n", "session.headers = SESSION_HEADERS\n", - "bard = Bard(token=token, session=session, timeout=30)\n", + "bard = Bard(token=token, token_ts=token_ts, session=session, timeout=30)\n", "\n", "new_cookies = bard.update_1PSIDTS()\n", "print('New cookies:', new_cookies)\n", diff --git a/scripts/google_api.ipynb b/scripts/google_api.ipynb index 502445697..363869292 100644 --- a/scripts/google_api.ipynb +++ b/scripts/google_api.ipynb @@ -48,6 +48,9 @@ "# set your __Secure-1PSID value to key\n", "os.environ['_BARD_API_KEY']=\"xxxxxxxxxx.\"\n", "\n", + "# set your __Secure-1PSIDTS value to key\n", + "os.environ['_BARD_APITS']=\"xxxxxxxxxx.\"\n", + "\n", "# set your input text\n", "input_text = \"What is Google Bard?\"\n", "\n", diff --git a/translate_to/asyncio_core.py b/translate_to/asyncio_core.py index 1afbe4e7a..a1c9de911 100644 --- a/translate_to/asyncio_core.py +++ b/translate_to/asyncio_core.py @@ -27,12 +27,14 @@ def __init__( Args: token (str): Bard API token. + token_ts (str): Bard API token ts. timeout (int): Request timeout in seconds. proxies (dict): Proxy configuration for requests. session (aiohttp.ClientSession): aiohttp session object. language (str): Language code for translation (e.g., "en", "ko", "ja"). """ self.token = token or os.getenv("_BARD_API_KEY") + self.token_ts = token_ts or os.getenv("_BARD_API_TS") self.proxies = proxies self.timeout = timeout self._reqid = int("".join(random.choices(string.digits, k=4))) @@ -103,7 +105,8 @@ async def get_answer(self, input_text: str) -> dict: Example: >>> token = 'xxxxxxxxxx' - >>> bard = Bard(token=token) + >>> token_ts = 'xxxxxxxxxx' + >>> bard = Bard(token=token, token_ts=token_ts) >>> response = await bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘") >>> print(response['content']) diff --git a/translate_to/core_async.py b/translate_to/core_async.py index 49ece4754..10cde2a9a 100644 --- a/translate_to/core_async.py +++ b/translate_to/core_async.py @@ -27,11 +27,13 @@ def __init__( Args: token (str): Bard API token. + token_ts (str): Bard API token ts. timeout (int): Request timeout in seconds. proxies (dict): Proxy configuration for requests. language (str): Language code for translation (e.g., "en", "ko", "ja"). """ self.token = token or os.getenv("_BARD_API_KEY") + self.token_ts = token_ts or os.getenv("_BARD_API_TS") self.proxies = proxies self.timeout = timeout self._reqid = int("".join(random.choices(string.digits, k=4))) @@ -56,7 +58,8 @@ async def get_answer(self, input_text: str) -> dict: Example: >>> token = 'xxxxxxxxxx' - >>> bard = BardAsync(token=token) + >>> token_ts = 'xxxxxxxxxx' + >>> bard = BardAsync(token=token, token_ts=token_ts) >>> response = await bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘") >>> print(response['content'])