Skip to content

Latest commit

 

History

History
54 lines (45 loc) · 1.48 KB

README.md

File metadata and controls

54 lines (45 loc) · 1.48 KB

duckduckgogo

Go Report Card Go license

Simple DuckDuckGo search api

duckduckgogo implements a search api in go-lang for DuckDuckGo scraping the html search page

Usage

package main

import (
    "fmt"
    "github.com/sap-nocops/duckduckgogo/client"
)

func main() {
    ddg := client.NewDuckDuckGoSearchClient()
    res, err := ddg.SearchLimited("antani", 10)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    for i, r := range res {
        fmt.Printf("*********** RESULT %d\n", i)
        fmt.Printf("url:     %s\n", r.FormattedUrl)
        fmt.Printf("title:   %s\n", r.Title)
        fmt.Printf("snippet: %s\n", r.Snippet)
    }
}

Testing

If you need to mock the ddg results for testing purpose you can use the SearchClient interface and implementing it in your test. For example:

type DdgMock struct {
    results []client.Result
}

func (d *DdgMock) Search(query string) ([]client.Result, error) {
    return d.results, nil
}

func (d *DdgMock) SearchLimited(query string, limit int) ([]client.Result, error) {
    if limit < 0 || limit > len(d.results) {
        return nil, fmt.Errorf("invalid limit")
    }
    return d.results[0:limit], nil
}