-
Notifications
You must be signed in to change notification settings - Fork 19
Add SerpApi engine resources #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vladm-serpapi
merged 3 commits into
serpapi:main
from
pranavkafle:feature/engine-resources
Jan 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #!/usr/bin/env python3 | ||
| """Build SerpApi engine parameter data for MCP usage.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import html | ||
| import json | ||
| from pathlib import Path | ||
| from urllib.request import Request, urlopen | ||
|
|
||
| from bs4 import BeautifulSoup | ||
| from markdownify import markdownify | ||
|
|
||
| PLAYGROUND_URL = "https://serpapi.com/playground" | ||
| EXCLUDED_ENGINES = { | ||
| "google_scholar_profiles", | ||
| "google_light_fast", | ||
| "google_lens_image_sources", | ||
| } | ||
| PARAM_KEEP_KEYS = {"html", "type", "options", "required"} | ||
| OUTPUT_DIR = Path("engines") | ||
| TIMEOUT_SECONDS = 30 | ||
| USER_AGENT = "Mozilla/5.0" | ||
|
|
||
|
|
||
| def html_to_markdown(value: str) -> str: | ||
| """Convert HTML to markdown, normalizing whitespace.""" | ||
| md = markdownify(html.unescape(value), strip=["a"]) | ||
| return " ".join(md.split()) | ||
|
|
||
|
|
||
| def normalize_options(options: list[object]) -> list[object]: | ||
| """Normalize option values, simplifying [value, label] pairs where possible.""" | ||
| normalized = [] | ||
| for option in options: | ||
| if isinstance(option, list) and option: | ||
| value = option[0] | ||
| label = option[1] if len(option) > 1 else None | ||
| if ( | ||
| label is not None | ||
| and ( | ||
| isinstance(value, (int, float)) | ||
| or (isinstance(value, str) and value.isdigit()) | ||
| ) | ||
| and value != label | ||
| ): | ||
| normalized.append(option) | ||
| else: | ||
| normalized.append(value) | ||
| else: | ||
| normalized.append(option) | ||
| return normalized | ||
|
|
||
|
|
||
| def fetch_props(url: str) -> dict[str, object]: | ||
| """Fetch playground HTML and extract React props.""" | ||
| req = Request(url, headers={"User-Agent": USER_AGENT}) | ||
| with urlopen(req, timeout=TIMEOUT_SECONDS) as resp: | ||
| page_html = resp.read().decode("utf-8", errors="ignore") | ||
| soup = BeautifulSoup(page_html, "html.parser") | ||
| node = soup.find(attrs={"data-react-props": True}) | ||
| if not node: | ||
| raise RuntimeError("Failed to locate data-react-props in playground HTML.") | ||
| return json.loads(html.unescape(node["data-react-props"])) | ||
|
|
||
|
|
||
| def normalize_engine(engine: str, payload: dict[str, object]) -> dict[str, object]: | ||
| """Normalize engine payload, extracting relevant parameter metadata.""" | ||
| normalized_params: dict[str, dict[str, object]] = {} | ||
| common_params: dict[str, dict[str, object]] = {} | ||
| if isinstance(payload, dict): | ||
| for group_name, group in payload.items(): | ||
| if not isinstance(group, dict): | ||
| continue | ||
| if not isinstance(params := group.get("parameters"), dict): | ||
| continue | ||
| for param_name, param in params.items(): | ||
| if not isinstance(param, dict): | ||
| continue | ||
| filtered = {k: v for k, v in param.items() if k in PARAM_KEEP_KEYS} | ||
| if isinstance(options := filtered.get("options"), list): | ||
| filtered["options"] = normalize_options(options) | ||
| if isinstance(html_value := filtered.pop("html", None), str): | ||
| filtered["description"] = html_to_markdown(html_value) | ||
| if filtered: | ||
| filtered["group"] = group_name | ||
| if group_name == "serpapi_parameters": | ||
| common_params[param_name] = filtered | ||
| else: | ||
| normalized_params[param_name] = filtered | ||
|
|
||
| return { | ||
| "engine": engine, | ||
| "params": normalized_params, | ||
| "common_params": common_params, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Main entry point: fetch playground data and generate engine files.""" | ||
| props = fetch_props(PLAYGROUND_URL) | ||
| if not isinstance(params := props.get("parameters"), dict): | ||
| raise RuntimeError("Playground props missing 'parameters' map.") | ||
|
|
||
| OUTPUT_DIR.mkdir(parents=True, exist_ok=True) | ||
| engines = [] | ||
|
|
||
| for engine, payload in sorted(params.items()): | ||
| if not isinstance(engine, str) or engine in EXCLUDED_ENGINES: | ||
| continue | ||
| if not isinstance(payload, dict): | ||
| continue | ||
| (OUTPUT_DIR / f"{engine}.json").write_text( | ||
| json.dumps(normalize_engine(engine, payload), indent=2, ensure_ascii=False), | ||
| encoding="utf-8", | ||
| ) | ||
| engines.append(engine) | ||
|
|
||
| print(f"Wrote {len(engines)} engine files to {OUTPUT_DIR}") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.