-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbrowser_base.py
61 lines (47 loc) · 1.74 KB
/
browser_base.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
"""
browserbase integration module
"""
import asyncio
from typing import List
def browser_base_fetch(
api_key: str,
project_id: str,
link: List[str],
text_content: bool = True,
async_mode: bool = False,
) -> List[str]:
"""
BrowserBase Fetch
This module provides an interface to the BrowserBase API.
Args:
api_key (str): The API key provided by BrowserBase.
project_id (str): The ID of the project on BrowserBase where you want to fetch data from.
link (List[str]): The URLs or links that you want to fetch data from.
text_content (bool): Whether to return only the text content (True) or the full HTML (False).
async_mode (bool): Whether to run the function asynchronously (True) or synchronously (False).
Returns:
List[str]: The results of the loading operations.
"""
try:
from browserbase import Browserbase
except ImportError:
raise ImportError(
"The browserbase module is not installed. Please install it using `pip install browserbase`."
)
# Initialize client with API key
browserbase = Browserbase(api_key=api_key)
# Create session with project ID
session = browserbase.sessions.create(project_id=project_id)
result = []
async def _async_fetch_link(url):
return await asyncio.to_thread(session.load, url, text_content=text_content)
if async_mode:
async def _async_browser_base_fetch():
for url in link:
result.append(await _async_fetch_link(url))
return result
result = asyncio.run(_async_browser_base_fetch())
else:
for url in link:
result.append(session.load(url, text_content=text_content))
return result