Dux Distributed Global Search (DDGS)

repository·main·Indexed 25 days ago

https://github.com/deedy5/ddgs

A metasearch library that aggregates results from diverse web search services including Google, Bing, and DuckDuckGo into a unified interface. It provides a Python API and CLI for text, image, video, news, and book searches, as well as a content extraction tool. The library supports deployment as a FastAPI REST server or a Model Context Protocol (MCP) server for integration with clients like Cursor and Claude Desktop.

Tokens
7.5K
Snippets
16
Records
57
Agent score
83%

What's inside ddgs

  1. Install DDGS

    main

    Install the base library or specific extras for API or MCP server support using pip.

    # Base install
    pip install -U ddgs
    
    # API server (FastAPI)
    pip install -U ddgs[api]
    
    # MCP server (stdio)
    pip install -U ddgs[mcp]
    pip install -U ddgs
  2. Run the DDGS MCP Server

    main

    The Model Context Protocol (MCP) server allows integration with clients like Cursor or Claude Desktop using stdio transport.

    Commands:

    • ddgs mcp: Start MCP server.
    • ddgs mcp -pr socks5h://127.0.0.1:9150: Start with a proxy.

    Available Tools:

    • search_text: Web text search
    • search_images: Image search
    • search_news: News search
    • search_videos: Video search
    • search_books: Book search
    • extract_content: Extract content from a URL

    Client Configuration (e.g., Cursor/Claude Desktop):

    {
      "mcpServers": {
        "ddgs": {
          "command": "ddgs",
          "args": ["mcp"]
        }
      }
    }
    ddgs mcp
  3. Run the DDGS API Server

    main

    The API server provides RESTful endpoints for various search types. It can be run in the foreground, detached mode, or with a proxy.

    Commands:

    • ddgs api: Start server in foreground.
    • ddgs api -d: Start in detached mode (background).
    • ddgs api -s: Stop detached server.
    • ddgs api --host 127.0.0.1 --port 4479: Specify host and port (default port is 4479).
    • ddgs api -pr socks5h://127.0.0.1:9150: Start with a proxy.

    Endpoints:

    EndpointMethodDescription
    /search/textGET, POSTText search
    /search/imagesGET, POSTImage search
    /search/newsGET, POSTNews search
    /search/videosGET, POSTVideo search
    /search/booksGET, POSTBook search
    /extractGET, POSTExtract content from URL
    /healthGETHealth check
    /docsGETSwagger UI
    /redocGETReDoc documentation
    ddgs api
  4. Set up the ddgs MCP Server

    main

    You can run ddgs as a Model Context Protocol (MCP) server for local clients like Cursor or Claude Desktop.

    Installation:

    pip install ddgs[mcp]

    Running the server:

    • Standard (stdio): ddgs mcp
    • With a proxy: ddgs mcp -pr socks5h://127.0.0.1:9150

    Client Configuration (JSON):

    {
      "mcpServers": {
        "ddgs": {
          "command": "ddgs",
          "args": ["mcp"]
        }
      }
    }

    Available MCP Tools:

    • search_text, search_images, search_news, search_videos, search_books, extract_content.
  5. Deploy DDGS API using Docker Compose

    main

    You can deploy the ddgs-api service using Docker Compose. The service builds from the local directory and exposes port 8000.

    Configuration Details

    • Ports: Maps host port 8000 to container port 8000.
    • Environment Variables: Supports DDGS_PROXY for proxy configuration.
    • Volumes: Persists logs from the container at /app/logs to the host directory ./logs.
    • Healthcheck: The service performs a health check via curl -f http://localhost:8000/health every 30 seconds. It allows a 60-second start_period before failing retries.
    services:
      ddgs-api:
        build: .
        ports:
          - "8000:8000"
        environment:
          - DDGS_PROXY
        volumes:
          - ./logs:/app/logs
        restart: unless-stopped
        healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
          interval: 30s
          timeout: 10s
          retries: 3
          start_period: 60s
  6. Configure Proxies and SSL in DDGS

    main

    Proxy Configuration

    • Python API: Pass proxy to the DDGS constructor or set the DDGS_PROXY environment variable.
      • DDGS(proxy="socks5h://127.0.0.1:9150")
    • MCP Server: Use the -pr flag or set DDGS_PROXY environment variable.
      • ddgs mcp -pr socks5h://127.0.0.1:9150
    • CLI: Use the -pr flag or set DDGS_PROXY environment variable.
      • ddgs api -pr socks5h://127.0.0.1:9150

    SSL and Timeouts

    • SSL Verification: Disable verification with DDGS(verify=False) or provide a path to a certificate with DDGS(verify="/path/to/cert.pem").
    • Timeout: Set a custom timeout in seconds with DDGS(timeout=10) (default is 5 seconds).
  7. Search for news using news()

    main

    Use the news() method to perform a news metasearch. It returns a list of dictionaries containing news results.

    Arguments:

    • query (str): The news search query.
    • region (str): Region code (e.g., us-en, uk-en, ru-ru). Defaults to us-en.
    • safesearch (str): Filter level (on, moderate, off). Defaults to moderate.
    • timelimit (str | None): Time filter (d for day, w for week, m for month). Defaults to None.
    • max_results (int | None): Maximum number of results. Defaults to 10.
    • page (int): Page number of results. Defaults to 1.
    • backend (str): A single or comma-delimited backends. Defaults to auto.

    Result Dictionary Keys:

    • date
    • title
    • body
    • url
    • image
    • source
    results = DDGS().news(query="sun", region="us-en", safesearch="off", timelimit="m", page=1, backend="auto")
    print(results)
  8. Search for books using books()

    main

    Use the books() method to perform a books metasearch. It returns a list of dictionaries containing book information.

    Arguments:

    • query (str): The book search query.
    • max_results (int | None): Maximum number of results. Defaults to 10.
    • page (int): Page number of results. Defaults to 1.
    • backend (str): A single or comma-delimited backends. Defaults to auto.

    Result Dictionary Keys:

    • title
    • author
    • publisher
    • info
    • url
    • thumbnail
    results = DDGS().books(query="sea wolf jack london", page=1, backend="auto")
    print(results)
  9. Extract content from a URL using the Python API

    main

    Use the extract() method to retrieve content from a specific URL. You can specify the format using the fmt parameter.

    Available formats:

    • markdown (default): Returns the content as Markdown text in the content key.
    • text_plain: Returns plain text.
    • content: Returns raw bytes.
  10. Extract content from a URL using extract()

    main

    Use the extract() method to fetch a URL and convert its content into various formats.

    Arguments:

    • url (str): The URL to fetch and extract content from.
    • fmt (str): The output format. Supported values:
      • text_markdown: HTML to Markdown (preserves links, headers, and lists). Defaults to this.
      • text_plain: HTML to plain text.
      • text_rich: HTML to rich text (includes headers/lists, but removes link URLs).
      • text: Returns raw HTML.
      • content: Returns raw bytes.

    Returns: A dictionary containing the url and the extracted content.

    # Markdown (default) - preserves links, headers, lists
    result = DDGS().extract("https://example.com")
    
    # Plain text
    result = DDGS().extract("https://example.com", fmt="text_plain")
    
    # Raw bytes
    result = DDGS().extract("https://example.com", fmt="content")
  11. Initialize the DDGS class

    main

    The DDGS class is lazy-loaded and serves as the main entry point for the library. You can configure the HTTP client via the constructor.

    Arguments:

    • proxy (str, optional): Proxy for the HTTP client (supports http, https, socks5). Example: "http://user:pass@example.com:3128".
    • timeout (int, optional): Timeout value for the HTTP client. Defaults to 5.
    • verify (bool | str): True to verify, False to skip, or a string path to a PEM file. Defaults to True.
    from ddgs import DDGS
    
    results = DDGS().text("python programming", max_results=5)
    print(results)
  12. Use the DDGS Python API for web search

    main

    The DDGS class provides methods to perform various types of web searches. All search methods return a list[dict[str, Any]] containing results.

    Common search methods include:

    • text(query, ...): General web text search.
    • images(query, ...): Image search.
    • news(query, ...): News search.
    • videos(query, ...): Video search.
    • books(query, ...): Book search.

    Each method supports parameters like max_results, region, safesearch, and backend.

    from ddgs import DDGS
    
    # Text search
    results = DDGS().text("python async", max_results=5)
    
    # Images, news, videos, books
    images = DDGS().images("butterfly", max_results=5)
    news   = DDGS().news("ai regulation", timelimit="w")
    videos = DDGS().videos("rust programming")
    books  = DDGS().books("machine learning")