-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
73 lines (57 loc) · 1.97 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Example script demonstrating the usage of the Brave Search Python Client.
For web, image, video and news search.
"""
import asyncio
import os
from dotenv import load_dotenv
from brave_search_python_client import (
BraveSearch,
CountryCode,
ImagesSearchRequest,
LanguageCode,
NewsSearchRequest,
VideosSearchRequest,
WebSearchRequest,
)
# Load .env file and get Brave Search API key from environment
load_dotenv()
api_key = os.getenv("BRAVE_SEARCH_API_KEY")
if not api_key:
msg = "BRAVE_SEARCH_API_KEY not found in environment"
raise ValueError(msg)
async def search() -> None:
"""Run various searches using the Brave Search Python Client (see https://brave-search-python-client.readthedocs.io/en/latest/reference_index.html)."""
# Initialize the Brave Search Python client, using the API key from the environment
bs = BraveSearch()
# Perform a web search
response = await bs.web(WebSearchRequest(q="jupyter"))
# Print results as JSON
# Iterate over web hits and render links in markdown
for _result in response.web.results if response.web else []:
pass
# Advanced search with parameters
response = await bs.web(
WebSearchRequest(
q="python programming",
country=CountryCode.DE,
search_lang=LanguageCode.DE,
),
)
for _result in response.web.results if response.web else []:
pass
# Search and render images
response = await bs.images(ImagesSearchRequest(q="cute cats"))
for _image in response.results or []:
pass
# Search and render videos
response = await bs.videos(VideosSearchRequest(q="singularity is close"))
for _video in response.results or []:
pass
# Search and render news
response = await bs.news(NewsSearchRequest(q="AI"))
for _item in response.results or []:
pass
# Run the async search function
# Alternatively use await search() from an async function
asyncio.run(search())