Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
reduce complexity: remove --at
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Cheong committed Mar 23, 2023
1 parent 6bf12b7 commit 7c867c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"cSpell.words": ["reqid"]
"cSpell.words": [
"reqid",
"snlm"
]
}
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ 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.
- At: `window.WIZ_global_data.SNlM0e` (Enter this in console and copy the output)

## Usage

```bash
$ python3 -m Bard -h
usage: Bard.py [-h] --session SESSION --at AT
usage: Bard.py [-h] --session SESSION

options:
-h, --help show this help message and exit
--session SESSION __Secure-1PSID cookie.
--at AT window.WIZ_global_data.SNlM0e
```

## [Developer Documentation](https://github.com/acheong08/Bard/blob/main/DOCUMENTATION.md)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="GoogleBard",
version="0.0.3",
version="0.0.4",
license="GNU General Public License v2.0",
author="Antonio Cheong",
author_email="acheong@student.dalat.org",
Expand Down
45 changes: 26 additions & 19 deletions src/Bard.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from rich.markdown import Markdown


def create_session() -> PromptSession:
def __create_session() -> PromptSession:
return PromptSession(history=InMemoryHistory())


def create_completer(commands: list, pattern_str: str = "$") -> WordCompleter:
def __create_completer(commands: list, pattern_str: str = "$") -> WordCompleter:
return WordCompleter(words=commands, pattern=re.compile(pattern_str))


Expand Down Expand Up @@ -54,27 +54,38 @@ class Chatbot:
__slots__ = [
"headers",
"_reqid",
"at",
"SNlM0e",
"conversation_id",
"response_id",
"choice_id",
"session",
]

def __init__(self, session_id, at):
self.headers = {
def __init__(self, session_id):
headers = {
"Host": "bard.google.com",
"X-Same-Domain": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Origin": "https://bard.google.com",
"Referer": "https://bard.google.com/",
"Cookie": "__Secure-1PSID=" + session_id + ";",
}
self._reqid = int("".join(random.choices(string.digits, k=4)))
self.at = at # window.WIZ_global_data.SNlM0e
self.conversation_id = ""
self.response_id = ""
self.choice_id = ""
self.session = requests.Session()
self.session.headers = headers
self.session.cookies.set("__Secure-1PSID", session_id)
self.SNlM0e = self.__get_snlm0e()

def __get_snlm0e(self):
resp = self.session.get(url="https://bard.google.com/", timeout=10)
# Find "SNlM0e":"<ID>"
if resp.status_code != 200:
raise Exception("Could not get Google Bard")
SNlM0e = re.search(r"SNlM0e\":\"(.*?)\"", resp.text).group(1)
return SNlM0e

def ask(self, message: str) -> dict:
"""
Expand All @@ -95,12 +106,14 @@ def ask(self, message: str) -> dict:
None,
[self.conversation_id, self.response_id, self.choice_id],
]
data = {"f.req": json.dumps([None, json.dumps(message_struct)]), "at": self.at}
data = {
"f.req": json.dumps([None, json.dumps(message_struct)]),
"at": self.SNlM0e,
}

# do the request!
resp = requests.post(
resp = self.session.post(
"https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate",
headers=self.headers,
params=params,
data=data,
timeout=120,
Expand Down Expand Up @@ -141,17 +154,11 @@ def ask(self, message: str) -> dict:
type=str,
required=True,
)
parser.add_argument(
"--at",
help="window.WIZ_global_data.SNlM0e",
type=str,
required=True,
)
args = parser.parse_args()

chatbot = Chatbot(args.session, args.at)
prompt_session = create_session()
completions = create_completer(["!exit", "!reset"])
chatbot = Chatbot(args.session)
prompt_session = __create_session()
completions = __create_completer(["!exit", "!reset"])
console = Console()
try:
while True:
Expand Down

0 comments on commit 7c867c2

Please sign in to comment.