File size: 1,856 Bytes
f1acfc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import importlib

__all__ = ['WebSearchTool']


class WebSearchTool():
    dependencies = ["googlesearch-python==1.3.0"]

    inputSchema = {
        "name": "WebSearchTool",
        "description": "Searches a specific website for a given query using Google search.",
        "parameters": {
            "type": "object",
            "properties": {
                "website": {
                    "type": "string",
                    "description": "The website domain to search in (e.g., 'stackoverflow.com').",
                },
                "query": {
                    "type": "string",
                    "description": "The query string to search for on the website.",
                }
            },
            "required": ["website", "query"],
        }
    }

    def __init__(self):
        pass

    def run(self, **kwargs):
        print("Running web search")

        website = kwargs.get("website")
        query = kwargs.get("query")

        if not website or not query:
            return {
                "status": "error",
                "message": "Missing required parameters: 'website' and 'query'",
                "output": None
            }

        search_query = f"site:{website} {query}"
        results = []

        googlesearch = importlib.import_module("googlesearch")

        try:
            for result in googlesearch.search(search_query, num_results=10):
                if "/search?num=" not in result:
                    results.append(result)

            return {
                "status": "success",
                "message": "Search completed successfully",
                "output": results,
            }

        except Exception as e:
            return {
                "status": "error",
                "message": f"Search failed: {str(e)}",
                "output": None,
            }