plugins

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2016 License: AGPL-3.0 Imports: 9 Imported by: 5

Documentation

Index

Constants

View Source
const (
	FirefoxUserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
	IE11UserAgent    = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
	ChromeUserAgent  = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
	SafariUserAgent  = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A"
)
View Source
const UnknownTotal = 0

Variables

This section is empty.

Functions

func NewGetRequest

func NewGetRequest(url string) *http.Request

Create a new GET request with a Firefox user agent.

func NewGetRequestUA added in v0.4.1

func NewGetRequestUA(url, userAgent string) *http.Request

Create a new GET request with a custom user agent.

func NewHTTPClient

func NewHTTPClient(timeout int) *http.Client

Create an HTTP client with a proper timeout timer.

func NewPostFormRequest

func NewPostFormRequest(url string, data url.Values) *http.Request

Create a new POST request with a Firefox user agent using form data.

func NewPostFormRequestUA added in v0.4.1

func NewPostFormRequestUA(url, userAgent string, data url.Values) *http.Request

Create a new POST request with a Firefox user agent using form data.

func OptionsToMap

func OptionsToMap(opts []Option) map[string]interface{}

Convert a slice of Option into a map[string]interface{}.

func PanicForStatus

func PanicForStatus(resp *http.Response, msg string)

Panic with an ErrHTTPStatusCode if the status code isn't 200.

Types

type BoolOption

type BoolOption struct {
	K                string
	V                bool
	Required, Hidden bool
	C                string
}

An implementation of Option that tries to convert the user input into a bool. Using strconv.ParseBool, it accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.

func (*BoolOption) Comment

func (opt *BoolOption) Comment() string

func (*BoolOption) IsHidden added in v0.4.1

func (opt *BoolOption) IsHidden() bool

func (*BoolOption) IsRequired

func (opt *BoolOption) IsRequired() bool

func (*BoolOption) Key

func (opt *BoolOption) Key() string

func (*BoolOption) Set

func (opt *BoolOption) Set(v string) (err error)

func (*BoolOption) Value

func (opt *BoolOption) Value() interface{}

type Downloader

type Downloader func(int, Reporter) error

type ErrHTTPStatusCode

type ErrHTTPStatusCode struct {
	StatusCode int
}

Implements the error interface.

func (*ErrHTTPStatusCode) String

func (e *ErrHTTPStatusCode) String() string

type FloatOption

type FloatOption struct {
	K                string
	V                float64
	Required, Hidden bool
	C                string
}

An implementation of Option that tries to convert the user input into a float64.

func (*FloatOption) Comment

func (opt *FloatOption) Comment() string

func (*FloatOption) IsHidden added in v0.4.1

func (opt *FloatOption) IsHidden() bool

func (*FloatOption) IsRequired

func (opt *FloatOption) IsRequired() bool

func (*FloatOption) Key

func (opt *FloatOption) Key() string

func (*FloatOption) Set

func (opt *FloatOption) Set(v string) (err error)

func (*FloatOption) Value

func (opt *FloatOption) Value() interface{}

type ForceDisableOption added in v0.4.1

type ForceDisableOption struct {
	BoolOption
}

A special option to disable a plugin. Since special options can be ignored, this becomes useful if a plugin might need to be disabled temporarily, but still allow people who know what they're doing to run it.

func NewForceDisableOption added in v0.4.1

func NewForceDisableOption(comment string) *ForceDisableOption

func (*ForceDisableOption) IsHidden added in v0.4.1

func (opt *ForceDisableOption) IsHidden() bool

func (*ForceDisableOption) IsRequired added in v0.4.1

func (opt *ForceDisableOption) IsRequired() bool

func (*ForceDisableOption) Key added in v0.4.1

func (opt *ForceDisableOption) Key() string

type ForceMaxWorkersOption added in v0.4.1

type ForceMaxWorkersOption struct {
	IntOption
}

An option to force the number of workers used by the download manager.

func NewForceMaxWorkersOption added in v0.4.1

func NewForceMaxWorkersOption(workers int) *ForceMaxWorkersOption

func (*ForceMaxWorkersOption) Comment added in v0.4.1

func (opt *ForceMaxWorkersOption) Comment() string

func (*ForceMaxWorkersOption) IsHidden added in v0.4.1

func (opt *ForceMaxWorkersOption) IsHidden() bool

func (*ForceMaxWorkersOption) IsRequired added in v0.4.1

func (opt *ForceMaxWorkersOption) IsRequired() bool

func (*ForceMaxWorkersOption) Key added in v0.4.1

func (opt *ForceMaxWorkersOption) Key() string

type ForceZipOption added in v0.4.1

type ForceZipOption struct {
	BoolOption
}

An option to force the download manager to either zip or not zip the directories after the download finishes.

func NewForceZipOption added in v0.4.1

func NewForceZipOption(force bool) *ForceZipOption

func (*ForceZipOption) Comment added in v0.4.1

func (opt *ForceZipOption) Comment() string

func (*ForceZipOption) IsHidden added in v0.4.1

func (opt *ForceZipOption) IsHidden() bool

func (*ForceZipOption) IsRequired added in v0.4.1

func (opt *ForceZipOption) IsRequired() bool

func (*ForceZipOption) Key added in v0.4.1

func (opt *ForceZipOption) Key() string

type IntOption

type IntOption struct {
	K                string
	V                int
	Required, Hidden bool
	C                string
}

An implementation of Option that tries to convert the user input into an integer.

func (*IntOption) Comment

func (opt *IntOption) Comment() string

func (*IntOption) IsHidden added in v0.4.1

func (opt *IntOption) IsHidden() bool

func (*IntOption) IsRequired

func (opt *IntOption) IsRequired() bool

func (*IntOption) Key

func (opt *IntOption) Key() string

func (*IntOption) Set

func (opt *IntOption) Set(v string) (err error)

func (*IntOption) Value

func (opt *IntOption) Value() interface{}

type Option

type Option interface {
	Key() string
	Value() interface{}
	// Set the value using user input.
	Set(string) error
	IsRequired() bool
	IsHidden() bool
	Comment() string
}

An option the plugin provides that the user can set. The input is through a string provided by the user.

type Plugin

type Plugin interface {
	// The name of the plugin.
	Name() string
	// The version of the plugin. Do not prefix it with "v" or anything like that.
	// Can return an empty string.
	Version() string
	// Should return whether or not it can deal with a URL.
	// There is no guarantee that DownloadGenerator() will be called later.
	// There is also no guarantee that the last call to this is the URL that
	// will be passed to DownloadGenerator(). In other words, don't store stuff
	// here for later use.
	CanHandle(url string) bool
	// Returns a slice of all the options. If the user wants to, they can
	// be set. Of course, if an option needs a value and the user does not
	// set it, either make sure you have a default value or that IsRequired()
	// returns true. The latter will not allow the plugin to run without setting it.
	Options() []Option
	// A generator function that returns a generator that generates downloaders.
	// Sounds overly convoluted, but when combined with closures, it's pretty damn neat.
	// The outer function could theoretically be dropped and just have it directly generate
	// Downloader functions, but if you have to initialize stuff first, you'd have to check
	// whether or not you've initialized at the start of the function for every call.
	// With this signature, you can just initialize and return the generator as a closure to
	// keep all the local variables and not have to store them anywhere first. This also
	// makes it ideal for having one single object per plugin without ever having to remake it.
	//
	// The "dls" is the number of downloaders that are going to be returned. Use UnknownTotal
	// if it's unknown. You can go over or under the total without it breaking anything important.
	// It's simply used for displaying progress through the interface.
	//
	// See the Dummy plugin for an example implementation.
	DownloadGenerator(url string) (dlgen func() Downloader, dls int)
	// A method called by the download manager at the end. If an error caused the manager
	// to abort, it is passed. Otherwise nil is passed.
	Cleanup(error)
}

The interface all plugins must implement.

A single object is used for multiple URLs that concern the plugin, so the implementation must deal with a call to DownloadGenerator() as a new download and reset any variables and whatnot that could affect the result.

type Reporter

type Reporter interface {
	// Should be used when you have something you need to download, but not
	// actually something you want the manager to save and count as part of
	// the resulting files.
	//
	// This doesn't directly benefit the downloader, but it allows the manager
	// to keep track of download speeds, and more importantly if something were
	// to go wrong in another downloader, it can detect that and stop the downloader
	// from keeping the application from exiting.
	Copy(dst io.Writer, src io.Reader) (written int64, err error)
	// Saves the data/file into a file as a successful download.
	// The path must be relative, as the downloader will take care of where to save files.
	// The report bool determines whether or not it should report download speeds.
	// In other words, whether or not src is getting its data straight from the network.
	SaveData(dst string, src io.Reader, report bool) (written int64, err error)
	// Saves the file as a successful download. The destination path must be relative,
	// as the downloader will take care of where to save files. The file is renamed (moved)
	// to the final destination rather than copied. This only works if the file resides on
	// the same disk drive as the the destination, so use SaveFile() if unsure.
	// Should be safe to use with TempFile() files. src must be closed before using this.
	SaveFile(dst, src string) (size int64, err error)
	// For when you need a temporary file. Should ensure the file resides on the same
	// disk drive as the download directory, allowing for use with SaveFile().
	TempFile() (*os.File, error)
	// Returns a writer to the destination file. The caller must close it.
	// Download completion is reported on close.
	FileWriter(dst string, report bool) (io.WriteCloser, error)
}

An interface passed to Downloader that allows it to get a destination to write downloads to and to get temporary files. Using this interface instead of opening files inside the plugin allows the download manager to control the flow of downloads (e.g. pausing, aborting), track download speeds, stay aware of downloaded files for further processing, and so on.

All paths used to save files with must relative and be in a directory. If the user wants to have the files zipped, all the top-level directories will be zipped.

type StringOption

type StringOption struct {
	K, V             string
	Required, Hidden bool
	C                string
}

A basic Option implementation that keeps all user input as-is instead of trying to convert stuff.

func (*StringOption) Comment

func (opt *StringOption) Comment() string

func (*StringOption) IsHidden added in v0.4.1

func (opt *StringOption) IsHidden() bool

func (*StringOption) IsRequired

func (opt *StringOption) IsRequired() bool

func (*StringOption) Key

func (opt *StringOption) Key() string

func (*StringOption) Set

func (opt *StringOption) Set(v string) error

func (*StringOption) Value

func (opt *StringOption) Value() interface{}

Directories

Path Synopsis
A helper package to make calls to the API served by BinB Reader.
A helper package to make calls to the API served by BinB Reader.

Jump to

Keyboard shortcuts

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