This is a simple tool to use any web search engines like Google, Yandex, Bing, Qwant, DuckDuckGo and so on.
Supports now:
- Unofficial Qwant
- Unofficial DuckDuckGo
- Unofficial Google
- More: Official Google, Yandex, Bing, Yahoo, Baidu, Shenma, Haoso, Sogou etc
go get github.com/the-go-tool/websearch
Then add imports:
import (
"github.com/the-go-tool/websearch"
"github.com/the-go-tool/websearch/provider"
)
web := websearch.New(provider.NewUnofficialDuckDuckGo())
res, err := web.Search("test", 25)
if err != nil {
// ...
}
fmt.Println(res)
// [{
// Title: string,
// Description: string,
// Link: url.URL,
// Provider: string,
// },...]
You will need to import several modules.
websearch
The main package with websearch wrapper.provider
Contains several web search providers for Qwant, DuckDuckGo, Google etc.errs
Contains provider's errors
import (
"github.com/the-go-tool/websearch"
"github.com/the-go-tool/websearch/provider"
"github.com/the-go-tool/websearch/provider/errs"
)
Some providers require configuration. It can be optional or not. If you have a token or any other credentials for official APIs, you can pass them by provider config.
web := websearch.New(provider.NewUnofficialQwant(provider.UnofficialQwantConfig{
Locale: "ru_RU",
}))
The library has several own errors.
Every error in websearch wrapped into websearch.Error,
so you can handle only errors from this library like:
res, err := web.Search("test", 25)
if err != nil {
if errors.As(err, &websearch.Error{}) {
// ...
}
// ...
}
Next, providers have common specific errors.
You can get IP ban when use unofficial API and you can check this case so:
res, err := web.Search("test", 25)
if err != nil {
if errors.As(err, &errs.IPBannedError{}) {
fmt.Println("your are banned by IP")
}
panic(err)
}
Q: Should I use unofficial providers?
A: Maybe. It depends on stability you expect. Official APIs require they token and may take taxes. Unofficial APIs are free, but they are unstable and your IP may be banned for several minutes. So, if you have your personal/home project or you don't want pay then choose unofficial.
If this project doesn't fit.