| 29 | } |
| 30 | |
| 31 | def google_search(self, query: str): |
| 32 | url = 'https://google.serper.dev/search' |
| 33 | headers = { |
| 34 | 'X-API-KEY': GOOGLE_SEARCH_KEY, |
| 35 | 'Content-Type': 'application/json', |
| 36 | } |
| 37 | data = { |
| 38 | "q": query, |
| 39 | "num": 10, |
| 40 | "extendParams": { |
| 41 | "country": "en", |
| 42 | "page": 1, |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | for i in range(5): |
| 47 | try: |
| 48 | response = requests.post(url, headers=headers, data=json.dumps(data)) |
| 49 | results = response.json() |
| 50 | except Exception as e: |
| 51 | print(e) |
| 52 | if i == 4: |
| 53 | return f"Google search Timeout, return None, Please try again later." |
| 54 | if response.status_code != 200: |
| 55 | raise Exception(f"Error: {response.status_code} - {response.text}") |
| 56 | |
| 57 | try: |
| 58 | if "organic" not in results: |
| 59 | raise Exception(f"No results found for query: '{query}'. Use a less specific query.") |
| 60 | |
| 61 | web_snippets = list() |
| 62 | idx = 0 |
| 63 | if "organic" in results: |
| 64 | for page in results["organic"]: |
| 65 | idx += 1 |
| 66 | date_published = "" |
| 67 | if "date" in page: |
| 68 | date_published = "\nDate published: " + page["date"] |
| 69 | |
| 70 | source = "" |
| 71 | if "source" in page: |
| 72 | source = "\nSource: " + page["source"] |
| 73 | |
| 74 | snippet = "" |
| 75 | if "snippet" in page: |
| 76 | snippet = "\n" + page["snippet"] |
| 77 | |
| 78 | redacted_version = f"{idx}. [{page['title']}]({page['link']}){date_published}{source}\n{snippet}" |
| 79 | |
| 80 | redacted_version = redacted_version.replace("Your browser can't play this video.", "") |
| 81 | web_snippets.append(redacted_version) |
| 82 | |
| 83 | content = f"A Google search for '{query}' found {len(web_snippets)} results:\n\n## Web Results\n" + "\n\n".join(web_snippets) |
| 84 | return content |
| 85 | except: |
| 86 | return f"No results found for '{query}'. Try with a more general query, or remove the year filter." |
| 87 | |
| 88 | |