diff --git a/bardapi/__init__.py b/bardapi/__init__.py index bb0aef071..0a6382ffe 100644 --- a/bardapi/__init__.py +++ b/bardapi/__init__.py @@ -12,7 +12,7 @@ IMG_UPLOAD_HEADERS, ) from bardapi.core_async import BardAsync -from bardapi.core_cookies import BardCookies +from bardapi.core_cookies import BardCookies, BardAsyncCookies from bardapi.utils import ( extract_links, upload_image, @@ -29,6 +29,7 @@ "ChatBard", "BardAsync", "BardCookies", + "BardAsyncCookies", "SESSION_HEADERS", "ALLOWED_LANGUAGES", "DEFAULT_LANGUAGE", diff --git a/bardapi/core.py b/bardapi/core.py index b83736ead..264e78652 100644 --- a/bardapi/core.py +++ b/bardapi/core.py @@ -96,7 +96,9 @@ def get_answer(self, input_text: str) -> dict: "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ params = { @@ -216,6 +218,7 @@ def get_answer(self, input_text: str) -> dict: "images": images, "langCode": langcode, "code": code, + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"], @@ -243,15 +246,18 @@ def speech(self, input_text: str, lang="en-US") -> dict: >>> bard = Bard(token=token) >>> audio = bard.speech("hello!") >>> with open("bard.ogg", "wb") as f: - >>> f.write(bytes(audio)) + >>> f.write(bytes(audio['audio'])) Args: input_text (str): Input text for the query. lang (str): Input language for the query. Returns: - bytes: audio in bytes format - with format of audio/ogg + dict: Answer from the Bard API in the following format: + { + "audio": bytes, + "status_code": int + } """ params = { "bl": "boq_assistant-bard-web-server_20230713.13_p0", @@ -288,7 +294,7 @@ def speech(self, input_text: str, lang="en-US") -> dict: resp_json = json.loads(resp_dict) audio_b64 = resp_json[0] audio_bytes = base64.b64decode(audio_b64) - return audio_bytes + return {"audio": audio_bytes, "status_code": resp.status_code} def export_conversation(self, bard_answer, title: str = ""): """ @@ -299,12 +305,17 @@ def export_conversation(self, bard_answer, title: str = ""): >>> bard = Bard(token=token) >>> bard_answer = bard.get_answer("hello!") >>> url = bard.export_conversation(bard_answer, title="Export Conversation") + >>> print(url['url']) Args: bard_answer (dict): bard_answer returned from get_answer title (str): Title for URL Returns: - string: public URL you can share + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ conv_id = bard_answer["conversation_id"] resp_id = bard_answer["response_id"] @@ -357,7 +368,7 @@ def export_conversation(self, bard_answer, title: str = ""): url = f"https://g.co/bard/share/{url_id}" # Increment request ID self._reqid += 100000 - return url + return {"url": url, "status_code": resp.status_code} def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> dict: """ @@ -385,7 +396,9 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ if self.google_translator_api_key is not None: @@ -512,7 +525,9 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di "choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]], "links": extract_links(parsed_answer[4]), "images": [""], + "langCode": "", "code": "", + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"], @@ -533,6 +548,7 @@ def export_replit( >>> bard = Bard(token=token) >>> bard_answer = bard.get_answer("code python to print hello world") >>> url = bard.export_replit(bard_answer['code'], bard_answer['langCode']) + >>> print(url['url']) Args: code (str): source code @@ -540,7 +556,11 @@ def export_replit( filename (str): filename for code language **kwargs: instructions, source_path Returns: - string: export URL to create repl + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ params = { "rpcids": "qACoKe", @@ -612,7 +632,7 @@ def export_replit( # increment request ID self._reqid += 100000 - return url + return {"url": url, "status_code": resp.status_code} def _get_snim0e(self) -> str: """ diff --git a/bardapi/core_async.py b/bardapi/core_async.py index 6041e2163..cf85e1d36 100644 --- a/bardapi/core_async.py +++ b/bardapi/core_async.py @@ -74,7 +74,7 @@ async def get_answer(self, input_text: str) -> dict: >>> import asyncio >>> >>> async def main(): - >>> token = 'xxxxxxxxxx' + >>> token = 'xxxxxx' >>> bard = BardAsync(token=token) >>> response = await bard.get_answer("나와 내 동년배들이 좋아하는 뉴진스에 대해서 알려줘") >>> print(response['content']) @@ -94,7 +94,10 @@ async def get_answer(self, input_text: str) -> dict: "textQuery": str, "choices": list, "links": list - "images": set + "images": set, + "langCode": str, + "code": str, + "status_code": int } """ self.SNlM0e = await self._get_snim0e() @@ -252,11 +255,11 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: >>> import asyncio >>> >>> async def main(): - >>> token = 'xxxxxxxxxx' + >>> token = 'xxxxxx' >>> bard = BardAsync(token=token) >>> audio = await bard.speech("Hello") >>> with open("bard.ogg", "wb") as f: - >>> f.write(bytes(audio)) + >>> f.write(bytes(audio['audio'])) >>> >>> asyncio.run(main()) @@ -265,8 +268,11 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: lang (str): Input language for the query Returns: - bytes: audio in bytes format - with format of audio/ogg + dict: Answer from the Bard API in the following format: + { + "audio": bytes, + "status_code": int + } """ params = { "bl": "boq_assistant-bard-web-server_20230419.00_p1", @@ -302,7 +308,7 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: resp_json = json.loads(resp_dict) audio_b64 = resp_json[0] audio_bytes = base64.b64decode(audio_b64) - return audio_bytes + return {"audio": audio_bytes, "status_code": resp.status_code} async def _get_snim0e(self): """ @@ -341,11 +347,11 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: >>> import asyncio >>> >>> async def main(): - >>> token = 'xxxxxxxxxx' + >>> token = 'xxxxxx' >>> bard = BardAsync(token=token) >>> bard_answer = await bard.get_answer("hello!") >>> url = await bard.export_conversation(bard_answer, title="Export Conversation") - >>> print(url) + >>> print(url['url']) >>> >>> asyncio.run(main()) @@ -353,7 +359,11 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: bard_answer (dict): bard_answer returned from get_answer title (str): Title for URL Returns: - string: public URL you can share + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ conv_id = bard_answer["conversation_id"] resp_id = bard_answer["response_id"] @@ -406,7 +416,10 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: url = f"https://g.co/bard/share/{url_id}" # Increment request ID self._reqid += 100000 - return url + return { + "url": url, + "status_code": resp.status_code, + } async def export_replit( self, code: str, langcode: str = None, filename: str = None, **kwargs @@ -418,11 +431,11 @@ async def export_replit( >>> import asyncio >>> >>> async def main(): - >>> token = 'xxxxxxxxxx' + >>> token = 'xxxxxx' >>> bard = BardAsync(token=token) >>> bard_answer = await bard.get_answer("code python to print hello world") >>> url = await bard.export_replit(bard_answer['code'], bard_answer['langCode']) - >>> print(url) + >>> print(url['url']) >>> >>> asyncio.run(main()) @@ -432,7 +445,12 @@ async def export_replit( filename (str): filename for code language **kwargs: instructions, source_path Returns: - string: export URL to create repl + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } + """ params = { "rpcids": "qACoKe", @@ -502,7 +520,7 @@ async def export_replit( # increment request ID self._reqid += 100000 - return url + return {"url": url, "status_code": resp.status_code} async def ask_about_image( self, input_text: str, image: bytes, lang: str = None @@ -513,7 +531,7 @@ async def ask_about_image( >>> import asyncio >>> >>> async def main(): - >>> token = 'xxxxxxxxxx' + >>> token = 'xxxxxx' >>> bard = BardAsync(token=token) >>> image = open('image.jpg', 'rb').read() >>> bard_answer = await bard.ask_about_image("what is in the image?", image) @@ -537,7 +555,9 @@ async def ask_about_image( "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ self.SNlM0e = await self._get_snim0e() @@ -667,7 +687,9 @@ async def ask_about_image( "choices": [{"id": x[0], "content": x[1]} for x in parsed_answer[4]], "links": extract_links(parsed_answer[4]), "images": [""], + "langCode": "", "code": "", + "status_code": resp.status_code, } self.conversation_id, self.response_id, self.choice_id = ( bard_answer["conversation_id"], diff --git a/bardapi/core_cookies.py b/bardapi/core_cookies.py index c938d425a..9842eeb2f 100644 --- a/bardapi/core_cookies.py +++ b/bardapi/core_cookies.py @@ -85,7 +85,9 @@ def get_answer(self, input_text: str) -> dict: "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ return super().get_answer(input_text) @@ -102,15 +104,18 @@ def speech(self, input_text: str, lang="en-US") -> dict: >>> bard = BardCookies(cookie_dict=cookies) >>> audio = bard.speech("hello!") >>> with open("bard.ogg", "wb") as f: - >>> f.write(bytes(audio)) + >>> f.write(bytes(audio['audio'])) Args: input_text (str): Input text for the query. lang (str): Input language for the query. Returns: - bytes: audio in bytes format - with format of audio/ogg + dict: Answer from the Bard API in the following format: + { + "audio": bytes, + "status_code": int + } """ return super().speech(input_text, lang) @@ -142,7 +147,9 @@ def ask_about_image(self, input_text: str, image: bytes, lang: str = None) -> di "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ return super().ask_about_image(input_text, image, lang) @@ -159,13 +166,17 @@ def export_conversation(self, bard_answer, title: str = ""): >>> bard = BardCookies(cookie_dict=cookies) >>> bard_answer = bard.get_answer("hello!") >>> url = bard.export_conversation(bard_answer, title="Export Conversation") - >>> print(url) + >>> print(url['url']) Args: bard_answer (dict): bard_answer returned from get_answer title (str): Title for URL Returns: - string: public URL you can share + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ return super().export_conversation(bard_answer, title) @@ -183,7 +194,7 @@ def export_replit( >>> bard = BardCookies(cookie_dict=cookies) >>> bard_answer = bard.get_answer("code python to print hello world") >>> url = bard.export_replit(bard_answer['code'], bard_answer['langCode']) - >>> print(url) + >>> print(url['url']) Args: code (str): source code @@ -191,7 +202,11 @@ def export_replit( filename (str): filename for code language **kwargs: instructions, source_path Returns: - string: export URL to create repl + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ return super().export_replit(code, langcode, filename, **kwargs) @@ -295,7 +310,10 @@ async def get_answer(self, input_text: str) -> dict: "textQuery": str, "choices": list, "links": list - "images": set + "images": set, + "langCode": str, + "code": str, + "status_code": int } """ return await super().get_answer(input_text) @@ -315,7 +333,7 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> audio = await bard.speech("Hello") >>> with open("bard.ogg", "wb") as f: - >>> f.write(bytes(audio)) + >>> f.write(bytes(audio['audio'])) >>> >>> asyncio.run(main()) @@ -324,8 +342,11 @@ async def speech(self, input_text: str, lang: str = "en-US") -> dict: lang (str): Input language for the query Returns: - bytes: audio in bytes format - with format of audio/ogg + dict: Answer from the Bard API in the following format: + { + "audio": bytes, + "status_code": int + } """ return await super().speech(input_text, lang) @@ -367,7 +388,9 @@ async def ask_about_image( "choices": list, "links": list, "images": set, - "code": str + "langCode": str, + "code": str, + "status_code": int } """ return await super().ask_about_image(input_text, image, lang) @@ -387,7 +410,7 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> bard_answer = await bard.get_answer("hello!") >>> url = await bard.export_conversation(bard_answer, title="Export Conversation") - >>> print(url) + >>> print(url['url']) >>> >>> asyncio.run(main()) @@ -395,7 +418,11 @@ async def export_conversation(self, bard_answer, title: str = "") -> str: bard_answer (dict): bard_answer returned from get_answer title (str): Title for URL Returns: - string: public URL you can share + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ return await super().export_conversation(bard_answer, title) @@ -416,7 +443,7 @@ async def export_replit( >>> bard = BardAsyncCookies(cookie_dict=cookies) >>> bard_answer = await bard.get_answer("code python to print hello world") >>> url = await bard.export_replit(bard_answer['code'], bard_answer['langCode']) - >>> print(url) + >>> print(url['url']) >>> >>> asyncio.run(main()) @@ -426,7 +453,11 @@ async def export_replit( filename (str): filename for code language **kwargs: instructions, source_path Returns: - string: export URL to create repl + dict: Answer from the Bard API in the following format: + { + "url": str, + "status_code": int + } """ return await super().export_replit(code, langcode, filename, **kwargs)