websearch

package module
v0.0.0-...-dd796f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 1 Imported by: 0

README

GitHub Go version Build Status Go Report Card GoDoc GitHub tag (latest SemVer) GitHub last commit GitHub issues

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

⏩ Fast Start

Get It

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"    
)
Configure It
web := websearch.New(provider.NewUnofficialDuckDuckGo())
Use It
res, err := web.Search("test", 25)
if err != nil {
    // ...
}

fmt.Println(res)
// [{
//		Title: string,
//		Description: string,
//		Link: url.URL,
//		Provider: string,
// },...]

▶ More Detailed Start

Modules Importing

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"
)
Provider Configuration

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",
}))
Error Handling

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/A

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.

⭐ Please, star it if you find it helpful
Similar projects

If this project doesn't fit.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

The main web search error

func NewError

func NewError(err error) Error

Makes a new ProviderError

func (Error) Error

func (e Error) Error() string

type WebSearch

type WebSearch struct {
	// contains filtered or unexported fields
}

The main web search wrapper

func New

func New(provider provider.Provider) *WebSearch

Makes a new web search provider

func (WebSearch) Search

func (webSearch WebSearch) Search(query string, count ...int) (provider.Results, error)

Makes web search

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL