Skip to content

Commit

Permalink
budfixed
Browse files Browse the repository at this point in the history
  • Loading branch information
canxin121 committed Jul 20, 2023
1 parent 72fbbf2 commit 118b76e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Go to https://bard.google.com/

- F12 for console
- Copy the values
- Session: Go to Application → Cookies → `__Secure-1PSID`. Copy the value of that cookie.
- Session: Go to Application → Cookies → `__Secure-1PSID` and `__Secure-1PSIDTS`. Copy the value of those cookie.

## Usage

Expand All @@ -21,13 +21,14 @@ usage: Bard.py [-h] --session SESSION

options:
-h, --help show this help message and exit
--session SESSION __Secure-1PSID cookie.
--__Secure_1PSID --__Secure_1PSIDTS pass two cookies
```

### Quick mode
```
$ export BARD_QUICK="true"
$ export BARD_SESSION="<__Secure-1PSID>"
$ export BARD___Secure-1PSID="<__Secure-1PSID>"
$ export BARD___Secure-1PSIDTS="<__Secure-1PSIDTS>"
$ python3 -m Bard
```
Environment variables can be placed in .zshrc.
Expand All @@ -38,7 +39,8 @@ Example bash shortcut:
# USAGE2: echo "QUESTION" | bard
bard () {
export BARD_QUICK=true
export BARD_SESSION=<REDACTED>.
export BARD___Secure-1PSID==<REDACTED>.
export BARD___Secure-1PSIDTS==<REDACTED>.
python3 -m Bard "${@:-$(</dev/stdin)}" | tail -n+7
}
```
Expand All @@ -48,11 +50,12 @@ bard () {
from os import environ
from Bard import Chatbot

token = environ.get("BARD_TOKEN")

chatbot = Chatbot(token)
Secure_1PSID = environ.get("BARD__Secure-1PSID")
Secure_1PSIDTS = environ.get("BARD__Secure-1PSIDTS")
chatbot = Chatbot(Secure_1PSID, Secure_1PSIDTS)

chatbot.ask("Hello, how are you?")

```

Credits:
Expand Down
6 changes: 3 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from os import environ

from Bard import Chatbot

token = environ.get("BARD_TOKEN")
Secure_1PSID = environ.get("BARD__Secure-1PSID")
Secure_1PSIDTS = environ.get("BARD__Secure-1PSIDTS")

chatbot = Chatbot(token)
chatbot = Chatbot(Secure_1PSID, Secure_1PSIDTS)

chatbot.ask("Hello, how are you?")
41 changes: 24 additions & 17 deletions src/Bard.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ class Chatbot:

def __init__(
self,
session_id: str,
secure_1psid: str,
secure_1psidts : str,
proxy: dict = None,
timeout: int = 20,
):
self.loop = asyncio.get_event_loop()
self.async_chatbot = self.loop.run_until_complete(
AsyncChatbot.create(session_id, proxy, timeout),
AsyncChatbot.create(secure_1psid, secure_1psidts, proxy, timeout),
)

def save_conversation(self, file_path: str, conversation_name: str):
Expand Down Expand Up @@ -102,14 +103,16 @@ class AsyncChatbot:
"response_id",
"choice_id",
"proxy",
"session_id",
"secure_1psidts",
"secure_1psid",
"session",
"timeout",
]

def __init__(
self,
session_id: str,
secure_1psid: str,
secure_1psidts : str,
proxy: dict = None,
timeout: int = 20,
):
Expand All @@ -126,23 +129,26 @@ def __init__(
self.conversation_id = ""
self.response_id = ""
self.choice_id = ""
self.session_id = session_id
self.secure_1psid = secure_1psid
self.secure_1psidts = secure_1psidts
self.session = httpx.AsyncClient(proxies=self.proxy)
self.session.headers = headers
self.session.cookies.set("__Secure-1PSID", session_id)
self.session.cookies.set("__Secure-1PSID", secure_1psid)
self.session.cookies.set("__Secure-1PSIDTS", secure_1psidts)
self.timeout = timeout

@classmethod
async def create(
cls,
session_id: str,
secure_1psid: str,
secure_1psidts:str,
proxy: dict = None,
timeout: int = 20,
) -> "AsyncChatbot":
"""
Async constructor.
"""
instance = cls(session_id, proxy, timeout)
instance = cls(secure_1psid,secure_1psidts, proxy, timeout)
instance.SNlM0e = await instance.__get_snlm0e()
return instance

Expand Down Expand Up @@ -211,9 +217,9 @@ async def load_conversation(self, file_path: str, conversation_name: str) -> boo

async def __get_snlm0e(self):
# Find "SNlM0e":"<ID>"
if not self.session_id or self.session_id[-1] != ".":
if not (self.secure_1psid and self.secure_1psidts) or self.secure_1psid[-1] != ".":
raise Exception(
"__Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value.",
"Enter correct __Secure-1PSID and __Secure-1PSIDTS value. __Secure-1PSID value must end with a single dot. ",
)
resp = await self.session.get(
"https://bard.google.com/",
Expand Down Expand Up @@ -297,11 +303,12 @@ async def ask(self, message: str) -> dict:
)
console = Console()
if os.getenv("BARD_QUICK"):
session = os.getenv("BARD_SESSION")
if not session:
print("BARD_SESSION environment variable not set.")
Secure_1PSID = os.getenv("BARD___Secure-1PSID")
Secure_1PSIDTS = os.getenv("BARD__Secure-1PSIDTS")
if not (Secure_1PSID and Secure_1PSIDTS):
print("BARD___Secure-1PSID or BARD__Secure-1PSIDTS environment variable not set.")
sys.exit(1)
chatbot = Chatbot(session)
chatbot = Chatbot(Secure_1PSID,Secure_1PSIDTS)
# Join arguments into a single string
MESSAGE = " ".join(sys.argv[1:])
response = chatbot.ask(MESSAGE)
Expand All @@ -310,14 +317,14 @@ async def ask(self, message: str) -> dict:
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument(
"--session",
help="__Secure-1PSID cookie.",
"--__Secure_1PSID --__Secure_1PSIDTS",
help="__Secure-1PSID cookie and __Secure_1PSIDTS cookie.",
type=str,
required=True,
)
args = parser.parse_args()

chatbot = Chatbot(args.session)
chatbot = Chatbot(args.__Secure_1PSID, args.__Secure_1PSIDTS)
prompt_session = __create_session()
completions = __create_completer(["!exit", "!reset"])

Expand Down

0 comments on commit 118b76e

Please sign in to comment.