Skip to content

hashgraph-online/awesome-codex-plugins / plugins/aklofas/kicad-happy/skills/mouser

mouser

Verified

Search Mouser Electronics for electronic components — secondary source for prototype orders. Find parts, check pricing/stock, download datasheets, analyze specifications. Use with KiCad for BOM creation and part selection. Also supports batch MPN-list seeding (`--mpn-list`) for bulk datasheet workflows without a KiCad project. Use this skill when the user specifically mentions Mouser, when DigiKey is out of stock or has worse pricing, when comparing prices across distributors, or when searching for parts that DigiKey doesn't carry. For package cross-reference tables and BOM workflow, see the `bom` skill.

scripts/fetch_datasheet_mouser.py

View on GitHub
#!/usr/bin/env python3
"""Download a datasheet PDF by searching Mouser for the URL.

Mouser's datasheet URLs (mouser.com/datasheet/...) sometimes block
automated downloads with bot protection. This script works around that
by trying multiple download methods and falling back to alternative
manufacturer URL patterns.

Download methods (tried in order):
  - requests library (redirects, anti-bot headers; some sites time out over urllib)
  - Python urllib (HTTP/1.1 fallback)
  - Playwright headless browser (JS-rendered pages, last resort; only with datasheets.allow_private_hosts)

After download, the PDF is verified by extracting text and checking
for the MPN, manufacturer name, and description keywords.

Usage:
    python3 fetch_datasheet_mouser.py --search <MPN> [--output <path>]
    python3 fetch_datasheet_mouser.py <url> [--output <path>]

Environment:
    MOUSER_SEARCH_API_KEY — required for --search mode

Exit codes:
    0 = success (PDF downloaded)
    1 = download failed after all attempts
    2 = search failed (API error or part not found)

Dependencies:
    - requests (pip install requests) — preferred, handles all manufacturer sites
    - Falls back to urllib if requests is not installed or is older than 2.32.2 (some sites may fail)
    - playwright (optional) — for JS-rendered datasheet pages, only with datasheets.allow_private_hosts
"""

import argparse
import json
import sys
import _core_path  # noqa: F401
from distributor_core.download import (download_pdf, try_alternative_sources,
                                       add_download_args, apply_download_args)
from distributor_core.verify import fetch_mismatch, refuse_fetch_hit, same_part, verify_datasheet
from distributor_core.manifest import _friendly_filename
from provider_mouser import _get_api_key, search_mouser, scrape_product_page


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(
        description="Download a datasheet via Mouser search",
    )
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("url", nargs="?", help="Direct URL to a PDF")
    group.add_argument("--search", metavar="MPN", help="Search Mouser by MPN")
    parser.add_argument("--output", "-o", help="Output file path")
    parser.add_argument("--json", action="store_true", help="JSON output")
    add_download_args(parser)
    args = parser.parse_args()
    apply_download_args(args)

    if args.url:
        # Direct URL mode
        output_path = args.output or "datasheet.pdf"
        if download_pdf(args.url, output_path):
            if args.json:
                json.dump({"success": True, "output": output_path, "source": "direct"}, sys.stdout)
            sys.exit(0)
        else:
            print(f"Failed to download PDF from {args.url}", file=sys.stderr)
            if args.json:
                json.dump({"success": False, "url": args.url, "error": "Download failed"}, sys.stdout)
            sys.exit(1)

    # --search mode
    mpn = args.search
    result = {"mpn": mpn} if args.json else None

    # Search Mouser for the datasheet URL
    api_key = _get_api_key()
    if not api_key:
        if result:
            result.update({"success": False, "error": "MOUSER_SEARCH_API_KEY not set"})
            json.dump(result, sys.stdout)
        sys.exit(2)

    print(f"[Mouser] Searching for {mpn}...", file=sys.stderr)
    mouser_part = search_mouser(mpn, api_key)

    if not mouser_part:
        print(f"[Mouser] No results for '{mpn}'", file=sys.stderr)
        if result:
            result.update({"success": False, "error": f"No Mouser results for '{mpn}'"})
            json.dump(result, sys.stdout)
        sys.exit(2)

    hit_mpn = mouser_part.get("ManufacturerPartNumber", "")
    other = fetch_mismatch(mpn, hit_mpn, [mouser_part.get("MouserPartNumber") or ""])
    if other:
        refuse_fetch_hit("Mouser", mpn, other, args.json)
    verify_pn = mpn if same_part(mpn, hit_mpn) else (hit_mpn or mpn)

    ds_url = mouser_part.get("DataSheetUrl", "")
    desc = mouser_part.get("Description", "")
    mfg = mouser_part.get("Manufacturer", "")

    if result:
        result["manufacturer"] = mfg
        result["description"] = desc
        result["datasheet_url"] = ds_url

    output_path = args.output or (_friendly_filename(mpn, desc) + ".pdf")

    def _finish_success(output_path: str, source: str):
        """Verify the downloaded PDF and output results."""
        vr = verify_datasheet(output_path, verify_pn, desc, mfg)
        if vr["confidence"] == "wrong":
            print(f"[Mouser] WARNING: Downloaded PDF may be wrong datasheet for {mpn}",
                  file=sys.stderr)
            print(f"  {vr['details']}", file=sys.stderr)
        elif vr["confidence"] == "unverified":
            print(f"[Mouser] Verification inconclusive for {mpn}: {vr['details']}",
                  file=sys.stderr)

        print(f"[Mouser] Success: {output_path}", file=sys.stderr)
        if result:
            result.update({
                "success": True, "output": output_path,
                "source": source, "verification": vr,
            })
            json.dump(result, sys.stdout)
        sys.exit(0)

    # Strategy 1: Try Mouser's datasheet URL
    if ds_url:
        print(f"[Mouser] Trying Mouser datasheet URL...", file=sys.stderr)
        if download_pdf(ds_url, output_path):
            _finish_success(output_path, "mouser")
        else:
            print(f"[Mouser] Mouser URL blocked or failed", file=sys.stderr)

    # Strategy 2: Scrape Mouser product page for datasheet link
    product_url = mouser_part.get("ProductDetailUrl", "")
    if product_url:
        print(f"[Mouser] Scraping product page for datasheet link...", file=sys.stderr)
        scraped_url = scrape_product_page(product_url)
        if scraped_url:
            print(f"[Mouser] Found datasheet link on product page", file=sys.stderr)
            if download_pdf(scraped_url, output_path):
                _finish_success(output_path, "mouser_scrape")

    # Strategy 3: Try alternative manufacturer sources
    print(f"[Mouser] Trying alternative sources...", file=sys.stderr)
    if try_alternative_sources(mpn, output_path):
        _finish_success(output_path, "alternative")

    # All methods failed — provide manual URL
    print(f"[Mouser] Failed to download datasheet for {mpn}", file=sys.stderr)
    if product_url:
        print(f"[Mouser] Manual download: {product_url}", file=sys.stderr)
    if result:
        result.update({"success": False, "error": "All download methods failed"})
        if product_url:
            result["manual_url"] = product_url
        json.dump(result, sys.stdout)
    sys.exit(1)


if __name__ == "__main__":
    from kicad_core.cli import utf8_stdio
    utf8_stdio()
    main()
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 321kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 197kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 197kUpdated Claude CodeCodexVerifiedInstall