diff --git a/README.md b/README.md index 248fed9..18a53b1 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ uv add httpx python-dotenv fastmcp - Sign up at [Serper.dev](https://serper.dev/) - Get your API key from the dashboard +3. **TalorData SERP API Key**: +- Sign up at [TalorData](https://talordata.com/?campaignid=Sj1gAnzg46Ph8b6N&utm_source=CMS&utm_term=CMS) +- Get your API key from the dashboard. + + ### Step 2: Environment Variables Create `.env` file in your project root with the following variables: @@ -78,6 +83,7 @@ Create `.env` file in your project root with the following variables: ```env RAPIDAPI_KEY=your_rapidapi_key_here SERPER_API_KEY=your_serper_api_key_here +TALORDATA_API_KEY=your_talordata_api_key_here ``` ## Usage diff --git a/main.py b/main.py index 53c44b5..ca62dd3 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ # Load API keys from .env RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY") SERPER_API_KEY = os.getenv("SERPER_API_KEY") +TALORDATA_API_KEY = os.getenv("TALORDATA_API_KEY") # API hosts LINKEDIN_HOST = "fresh-linkedin-profile-data.p.rapidapi.com" @@ -24,8 +25,8 @@ # Check required keys if not RAPIDAPI_KEY: raise ValueError("RAPIDAPI_KEY is not set in the environment variables") -if not SERPER_API_KEY: - raise ValueError("SERPER_API_KEY is not set in the environment variables") +if not SERPER_API_KEY and not TALORDATA_API_KEY: + raise ValueError("Either SERPER_API_KEY or TALORDATA_API_KEY must be set in the environment variables") # Initialize MCP mcp = FastMCP("social_web_scraper") @@ -181,5 +182,48 @@ async def scrape_website(query: str, gl: str = "in", num: int = 10, page: int = return json.dumps(data, indent=2) # ---- RUN SERVER ---- +# ---- WEBSITE SCRAPER TOOL (TalorData SERP) ---- +async def fetch_talordata_search(query: str) -> dict[str, Any] | None: + """使用 TalorData SERP API 进行搜索""" + if not TALORDATA_API_KEY: + return None + + url = "https://talordata.com" + headers = { + "Authorization": f"Bearer {TALORDATA_API_KEY}", + "Content-Type": "application/json" + } + payload = { + "q": query, + "engine": "google" + } + + async with httpx.AsyncClient() as client: + try: + response = await client.post(url, headers=headers, json=payload, timeout=30.0) + response.raise_for_status() + return response.json() + except Exception as e: + print(f"Error fetching TalorData search data: {e}") + return None + +@mcp.tool() +async def scrape_via_talordata(query: str) -> str: + """Fetch search results for a given query using TalorData SERP API.""" + data = await fetch_talordata_search(query) + if not data: + return "Unable to fetch TalorData search data." + + # 提取 TalorData 的自然搜索结果列表并格式化返回给大模型 + raw_results = data.get("organic_results", []) + results = [] + for item in raw_results: + results.append({ + "title": item.get("title", ""), + "link": item.get("link", item.get("url", "")), + "snippet": item.get("snippet", item.get("description", "")) + }) + return json.dumps({"organic": results}, indent=2) + if __name__ == "__main__": mcp.run(transport="stdio")