bulk

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

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

Go to latest
Published: Jul 3, 2017 License: MIT Imports: 8 Imported by: 0

README

Bulk

URL checking library for Go.

This library helps to provide a simple API for testing multiple URLs for their validness - if they are Valid and where they Redirect, or if they are Invalid. Requests are done by a "HEAD" HTTP method, and every URL checking is done concurrently.

Maintaned by @adrianplavka.

Installation

go get -u github.com/adrianplavka/bulk

Usage

Check one or multiple URLs

For simple usage, simply declare a DefaultBulker:

import "github.com/adrianplavka/bulk"

func main() {
    bulker := bulk.DefaultBulker

    // ...
}

You can now use your Bulker, which has methods pre-defined.

To check a single URL for it's validness, simply:

status := bulker.Check("http://www.google.com")
fmt.Println(status)

Check method returns a single Status, that simply tells if the URL was Valid with Redirections or Not.

type Status struct {
	URL    string
	Valid  bool
	Redirs []redirection
}

To check how many redirections it had, you can iterate over status' Redirs:

fmt.Println(status.Redirs)

To check multiple URLs, you pass a string slice and a status channel to CheckMultiple method:

func main() {
    bulker := bulk.DefaultBulker

    urls := []string{
        "http://google.com",
        "http://github.com",
        "http://stackoverflow.com"}
    progress := make(chan bulk.Status)

    bulker.CheckMultiple(urls, progress)
    for status := range progress {
        fmt.Println(status, status.Redirs)
    }

    // ...
}

This loop blocks until every URL has been checked.

Check URLs from a body

To check URLs from a file or a body, Feed method expects a Decoder interface wih a Status channel. Decoder interface contains only one method Decode, where you specify how you obtain URLs from a source. Decode should return a string slice of URLs and an error, if occured.

type Decoder interface {
	Decode() ([]string, error)
}

Bulk comes with a LineDecoder, that reads a Body line-by-line, URLs are separated by a newline.

type LineDecoder struct {
	Body io.ReadCloser
}

To use this, simply declare a LineDecoder with a Body (that is automatically closed after decoding) and call Feed method:

func main() {
    bulker := bulk.DefaultBulker

    // Open a CSV file for read-only.
    path, _ := filepath.Abs("bulk/example/urls.csv")
    file, err := os.Open(path)
    if err != nil {
        log.Fatalln("failed while opening a file: ", err)
    }

    // Create a LineDecoder with a Status channel.
    decoder := bulk.LineDecoder{Body: file}
    progress := make(chan bulk.Status)

    // Feed the URLs with a decoder.
    bulker.Feed(decoder, progress)
    for status := range progress {
        fmt.Println(status, status.Redirs)
    }

    // ...
}

License

MIT

Documentation

Index

Constants

View Source
const (
	StatusValid   = "URL <{}> is Valid. "
	StatusInvalid = "URL <{}> is Invalid. "
)

URL valid status messages.

Variables

View Source
var DefaultBulker = Bulker{
	http.Client{
		Timeout: time.Duration(time.Second * 5),
	},
}

DefaultBulker is a default implementation of Bulker. The timeout is a maximum of 5 seconds and the amount of redirection it can have is 10 (http.Client default).

Functions

This section is empty.

Types

type Bulker

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

Bulker type, that implements an HTTP client.

func (Bulker) Check

func (b Bulker) Check(url string) (s Status)

Check checks an URL from a raw url string.

func (Bulker) CheckMultiple

func (b Bulker) CheckMultiple(urls []string, status chan<- Status)

CheckMultiple is a concurrent function, that checks multiple URLs.

func (Bulker) Feed

func (b Bulker) Feed(d Decoder, s chan<- Status)

Feed requires a Decoder, from which body it will decode URLs and then reports the information to the Status channel.

type Decoder

type Decoder interface {
	Decode() ([]string, error)
}

Decoder is an interface, that should decode a file and return the URLs. If needed, implement your own type, that returns a string slice of URLs, and an error.

type LineDecoder

type LineDecoder struct {
	Body io.ReadCloser
}

LineDecoder represents a line-by-line body, from which the URLs are being decoded from.

func (LineDecoder) Decode

func (d LineDecoder) Decode() ([]string, error)

Decode a body, that separates each URL with a newline.

type Status

type Status struct {
	URL        string
	Valid      bool
	StatusCode int
	Redirs     []redirection
}

Status is a struct that indicates if an URL was valid, and how many redirects it had.

func (Status) String

func (s Status) String() (msg string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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