diff --git a/.vscode/settings.json b/.vscode/settings.json index 3ff8a21..4f40e20 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "cSpell.words": ["reqid"] + "cSpell.words": [ + "reqid", + "snlm" + ] } diff --git a/README.md b/README.md index d2c7282..9ab71da 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/setup.py b/setup.py index aad6772..879ba92 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/src/Bard.py b/src/Bard.py index 29c653a..6808485 100644 --- a/src/Bard.py +++ b/src/Bard.py @@ -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)) @@ -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":"" + 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: """ @@ -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, @@ -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: