From 565f36882bff2b7f80380afd241ab35e8c827714 Mon Sep 17 00:00:00 2001 From: Srinivas T B Date: Tue, 16 Apr 2024 22:10:25 +0530 Subject: [PATCH] JSONDecodeError when attempting to get data from the web #240 issue --fix --- software/source/clients/browser.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 software/source/clients/browser.py diff --git a/software/source/clients/browser.py b/software/source/clients/browser.py new file mode 100644 index 0000000..6b70a61 --- /dev/null +++ b/software/source/clients/browser.py @@ -0,0 +1,39 @@ +import requests +import json + +class Browser: + def __init__(self, computer): + self.computer = computer + + def search(self, query): + + try: + + response = requests.get( + f'{self.computer.api_base.strip("/")}/browser/search', + params={"query": query}, + ) + response.raise_for_status() + return response.json()["result"] + except Exception as e: + print(f"Error with OpenInterpreter API: {e}. Trying with Google search.") + return self.fallback_search(query) + + def fallback_search(self, query): + """ + Fallback search using Google search when the primary API fails. + """ + try: + from googlesearch import search + + for result in search(query, num=1, stop=1, pause=2): + return json.dumps({'result': result}) + except Exception as e: + print(f"Error in Google search: {e}") + return json.dumps({'error': 'Both primary and fallback searches failed'}) + +# Example usage +if __name__ == "__main__": + computer = Computer(api_base='https://api.openinterpreter.com/v0/') + browser = Browser(computer) + print(browser.search("What is the weather going to be like tomorrow in Charlotte, NC?")) \ No newline at end of file